Problem Solution Discussion Database-Independent Methods of Obtaining Table Information

info[default] = row[4] return info The following exam ple shows one way t o access and display each elem ent of t he dict ionary value ret urned by get_enumorset_info : info = get_enumorset_info conn, tbl_name, col_name print Information for + tbl_name + . + col_name + : if info == None: print No information available not an ENUM or SET column? else: print Name: + info[name] print Type: + info[type] print Legal values: + string.join info[values], , if info[nullable]: print Nullable else: print Not nullable if info[default] == None: print Default value: NULL else: print Default value: + info[default] That code produces t he following out put for t he item t able colors colum n: Information for item.colors: Type: set Legal values: chartreuse,mauve,lime green,puce Nullable Default value: puce Equivalent funct ions for ot her API s are sim ilar. Theyll com e in handy in t he cont ext of generat ing list elem ent s in web form s. See Recipe 18.3 and Recipe 18.4 .

9.8 Database-Independent Methods of Obtaining Table Information

9.8.1 Problem

You want a way t o get t able inform at ion t hat doesnt use MySQL-specific queries like SHOW COLUMNS .

9.8.2 Solution

This isnt possible for all API s. One except ion is JDBC, which provides a st andard int erface t o t able m et adat a.

9.8.3 Discussion

The preceding m et hods for obt aining t able inform at ion used specific SHOW or SELECT queries and showed how t o process t hem using each API . These t echniques are MySQL- specific. JDBC provides a way t o access t his inform at ion t hrough a st andard int erface t hat m akes no reference t o part icular queries, so you can use it port ably wit h dat abase engines ot her t han MySQL. Wit h t his int erface, you use your connect ion obj ect t o obt ain a dat abase m et adat a obj ect , t hen invoke t he getColumns m et hod of t hat obj ect t o ret rieve colum n inform at ion. getColumns ret urns a result set cont aining one row per colum n nam e, so you m ust run a fet ch loop t o ret rieve inform at ion about successive colum ns. Elem ent s of result set rows t hat are relevant for MySQL are: I n de x M e a n in g 3 Table nam e 4 Colum n nam e 6 Colum n t ype nam e 7 Colum n size for num eric colum ns, t his is t he precision 8 Num ber of decim al places, for num eric colum ns 18 Whet her or not colum n values can be NULL Heres an exam ple t hat shows how t o use getColumns t o print a list of colum n nam es and t ypes: DatabaseMetaData md = conn.getMetaData ; ResultSet rs = md.getColumns dbName, , tblName, ; int i = 0; while rs.next { i++; System.out.println --- Column + i + ---; System.out.println Name: + rs.getString 4; System.out.println Type: + rs.getString 6; } rs.close ; I f t he value of t he tblName variable is item , t he out put looks like t his: --- Column 1 --- Name: id Type: int --- Column 2 --- Name: name Type: char --- Column 3 --- Name: colors Type: enum The four argum ent s t o getColumns are t he nam es of t he cat alog, schem a, and t able, followed by a SQL pat t ern t hat colum n nam es m ust m at ch t o be select ed. For MySQL, t hese argum ent s have t he following m eanings: • The cat alog nam e is t he dat abase nam e. To use t he current dat abase, pass an em pt y st ring. • MySQL has no concept of schem a, so t he schem a nam e argum ent is irrelevant and can be t he em pt y st ring. • The t able nam e argum ent is a st ring nam ing t he t able. • The colum n nam e pat t ern is analogous t o using t he LIKE clause in a SHOW COLUMNS st at em ent . The exam ple shown above uses , which m at ches all colum n nam es. You can pass a specific colum n nam e t o get inform at ion for a single colum n. Rem em ber t o escape any and _ charact ers wit h a backslash if you want t o m at ch t hem lit erally.

9.9 Applying Table Structure Information