Python Obtaining Result Set Metadata

printf max_length: s\n, col_info-max_length; printf multiple_key: s\n, col_info-multiple_key; printf not_null: s\n, col_info-not_null; printf numeric: s\n, col_info-numeric; printf primary_key: s\n, col_info-primary_key; printf table: s\n, col_info-table; printf type: s\n, col_info-type; printf unique_key: s\n, col_info-unique_key; printf unsigned: s\n, col_info-unsigned; printf zerofill: s\n, col_info-zerofill; } if ncols 0 dispose of result set, if there is one mysql_free_result result_id; The out put from t he program looks like t his: Query: SELECT name, foods FROM profile Number of rows: 10 Number of columns: 2 --- Column 0 name --- blob: 0 max_length: 7 multiple_key: 0 not_null: 1 numeric: 0 primary_key: 0 table: profile type: string unique_key: 0 unsigned: 0 zerofill: 0 --- Column 1 foods --- blob: 0 max_length: 21 multiple_key: 0 not_null: 0 numeric: 0 primary_key: 0 table: profile type: string unique_key: 0 unsigned: 0 zerofill: 0

9.3.6 Python

Pyt hons DB- API is m ore lim it ed t han t he ot her API s in providing result set m et adat a. The row and colum n count s are available, but t he inform at ion about individual colum ns is not as ext ensive. To get t he row count for a result set , access t he cursors rowcount at t ribut e. The colum n count is not available direct ly, but aft er calling fetchone or fetchall , you can det erm ine t he count as t he lengt h of any result set row t uple. I t s also possible t o det erm ine t he colum n count wit hout fet ching any rows by using cursor.description . This is a t uple cont aining one elem ent per colum n in t he result set , so it s lengt h t ells you how m any colum ns are in t he set . However, be aware t hat if t he query generat es no result set , such as for an UPDATE st at em ent , t he value of description is None . Each elem ent of t he description t uple is anot her t uple t hat represent s t he m et adat a for t he corresponding colum n of t he result . There are seven m et adat a values per colum n; t he following code shows how t o access t hem and w hat t hey m ean: query = SELECT name, foods FROM profile print Query: , query cursor = conn.cursor cursor.execute query metadata information becomes available at this point ... print Number of rows:, cursor.rowcount if cursor.description == None: no result set ncols = 0 else: ncols = len cursor.description print Number of columns:, ncols if ncols == 0: print Note: query has no result set for i in range ncols: col_info = cursor.description[i] print name, then other information print --- Column d s --- i, col_info[0] print Type: , col_info[1] print Display size: , col_info[2] print Internal size:, col_info[3] print Precision: , col_info[4] print Scale: , col_info[5] print Nullable: , col_info[6] cursor.close The out put from t he program looks like t his: Query: SELECT name, foods FROM profile Number of rows: 10L Number of columns: 2 --- Column 0 name --- Type: 254 Display size: 7 Internal size: 20 Precision: 20 Scale: 0 Nullable: 0 --- Column 1 foods --- Type: 254 Display size: 21 Internal size: 42 Precision: 42 Scale: 0 Nullable: 1

9.3.7 Java