Python Issuing Queries and Retrieving Results

Dont Use count to Get a Column Count in PHP 3 PHP program m ers som et im es fet ch a result set row and t hen use countrow t o det erm ine how m any values t he row cont ains. I t s preferable t o use mysql_num_fields inst ead, as you can see for yourself by execut ing t he following fragm ent of PHP code: if result_id = mysql_query SELECT 1, 0, NULL, conn_id die Cannot issue query\n; count = mysql_num_fields result_id; print The row contains count columns\n; if row = mysql_fetch_row result_id die Cannot fetch row\n; count = count row; print The row contains count columns\n; I f you run t he code under PHP 3, youll find t hat count ret urns 2. Wit h PHP 4, count ret urns 3. These differing result s occur because count count s array values t hat correspond t o NULL values in PHP 4, but not in PHP 3. By cont rast , mysql_field_count uniform ly ret urns 3 for bot h versions of PHP. The m oral is t hat count wont necessarily give you an accurat e value. Use mysql_field_count if you want t o know t he t rue colum n count .

2.5.7 Python

The Pyt hon DB- API int erface does not have dist inct calls for queries t hat ret urn a result set and t hose t hat do not . To process a query in Pyt hon, use your dat abase connect ion obj ect t o get a cursor obj ect . [ 6] Then use t he cursors execute m et hod t o send t he query t o t he server. I f t here is no result set , t he query is com plet ed, and you can use t he cursors rowcount at t ribut e t o det erm ine how m any records were changed: [ 7] [ 6] I f youre fam iliar wit h t he t erm cursor as provided on t he server side in som e dat abases, MySQL doesnt really provide cursors t he sam e way. I nst ead, t he MySQLdb m odule em ulat es cursors on t he client side of query execut ion. [ 7] Not e t hat rowcount is an at t ribut e, not a funct ion. Refer t o it as rowcount , not rowcount , or an except ion will be raised. try: cursor = conn.cursor cursor.execute UPDATE profile SET cats = cats+1 WHERE name = Fred print d rows were updated cursor.rowcount except MySQLdb.Error, e: print Oops, the query failed print e I f t he query does ret urn a result set , fet ch it s rows and close t he set . DB- API provides a couple of m et hods for ret rieving rows. fetchone ret urns t he next row as a sequence or None when t here are no m ore rows : try: cursor = conn.cursor cursor.execute SELECT id, name, cats FROM profile while 1: row = cursor.fetchone if row == None: break print id: s, name: s, cats: s row[0], row[1], row[2] print d rows were returned cursor.rowcount cursor.close except MySQLdb.Error, e: print Oops, the query failed print e As you can see from t he preceding exam ple, t he rowcount at t ribut e is useful for SELECT queries, t oo; it indicat es t he num ber of rows in t he result set . Anot her row- fet ching m et hod, fetchall , ret urns t he ent ire result set as a sequence of sequences. You can it erat e t hrough t he sequence t o access t he rows: try: cursor = conn.cursor cursor.execute SELECT id, name, cats FROM profile rows = cursor.fetchall for row in rows: print id: s, name: s, cats: s row[0], row[1], row[2] print d rows were returned cursor.rowcount cursor.close except MySQLdb.Error, e: print Oops, the query failed print e Like DBI , DB-API doesnt provide any way t o rewind a result set , so fetchall can be convenient when you need t o it erat e t hrough t he rows of t he result set m ore t han once or access individual values direct ly. For exam ple, if rows holds t he result set , you can access t he value of t he t hird colum n in t he second row as rows[1][2] indexes begin at 0, not 1 . To access row values by colum n nam e, specify t he DictCursor cursor t ype when you creat e t he cursor obj ect . This causes rows t o be ret urned as Pyt hon dict ionary obj ect s wit h nam ed elem ent s: try: cursor = conn.cursor MySQLdb.cursors.DictCursor cursor.execute SELECT id, name, cats FROM profile for row in cursor.fetchall : print id: s, name: s, cats: s \ row[id], row[name], row[cats] print d rows were returned cursor.rowcount cursor.close except MySQLdb.Error, e: print Oops, the query failed print e

2.5.8 Java