PHP Python Java Handling NULL Values in Result Sets

printf name: s, birth: s, foods: s\n, defined ref-{name} ? ref-{name} : NULL, defined ref-{birth} ? ref-{birth} : NULL, defined ref-{foods} ? ref-{foods} : NULL; } Unfort unat ely, all t hat t est ing of colum n values is ponderous, and becom es worse t he m ore colum ns t here are. To avoid t his, you can t est and set undefined values in a loop prior t o print ing t hem . Then t he am ount of code t o perform t he t est s is const ant , not proport ional t o t he num ber of colum ns t o be t est ed. The loop also m akes no reference t o specific colum n nam es, so it can be copied and past ed t o ot her program s m ore easily, or used as t he basis for a ut ilit y rout ine: my sth = dbh-prepare SELECT name, birth, foods FROM profile; sth-execute ; while my ref = sth-fetchrow_hashref { foreach my key keys {ref} { ref-{key} = NULL unless defined ref-{key}; } printf name: s, birth: s, foods: s\n, ref-{name}, ref-{birth}, ref-{foods}; } I f you fet ch rows int o an array rat her t han int o a hash, you can use map t o convert any undef values: my sth = dbh-prepare SELECT name, birth, foods FROM profile; sth-execute ; while my val = sth-fetchrow_array { val = map { defined _ ? _ : NULL } val; printf name: s, birth: s, foods: s\n, val[0], val[1], val[2]; }

2.9.5 PHP

PHP represent s NULL values in result set s as unset values, so you can use t he isset funct ion t o det ect NULL values in query result s. The following code shows how t o do t his: result_id = mysql_query SELECT name, birth, foods FROM profile, conn_id; if result_id die Oops, the query failed\n; while row = mysql_fetch_row result_id { while list key, value = each row { if isset row[key] test for unset value row[key] = NULL; } print name: row[0], birth: row[1], foods: row[2]\n; } mysql_free_result result_id; PHP 4 has a special value NULL t hat is like an unset value. I f you can assum e your script s will run under PHP 4, you can t est for NULL values like t his: if row[key] === NULL test for PHP NULL value row[key] = NULL; Not e t he use of t he === t riple- equal operat or, which in PHP 4 m eans exact ly equal t o. The usual == equal t o com parison operat or is not suit able here; wit h == , t he PHP NULL value, t he em pt y st ring, and 0 all com pare equal t o each ot her.

2.9.6 Python

Pyt hon DB-API program s represent NULL values in result set s using None . The following exam ple shows how t o det ect NULL values: try: cursor = conn.cursor cursor.execute SELECT name, birth, foods FROM profile for row in cursor.fetchall : row = list row convert non-mutable tuple to mutable list for i in range 0, len row: if row[i] == None: is the column value NULL? row[i] = NULL print name: s, birth: s, foods: s row[0], row[1], row[2] cursor.close except: print Oops, the query failed The inner loop checks for NULL colum n values by looking for None and convert s t hem t o t he st ring NULL . Not e how t he exam ple convert s row t o a m ut able obj ect prior t o t he loop; t hat is done because fetchall ret urns rows as sequence values, which are non-m ut able read- only .

2.9.7 Java

For JDBC program s, if it s possible for a colum n in a result set t o cont ain NULL values, it s best t o check for t hem explicit ly. The way t o do t his is t o fet ch t he value and t hen invoke wasNull , which ret urns t rue if t he colum n is NULL and false ot herwise. For exam ple: Object obj = rs.getObject index; if rs.wasNull { the values a NULL } The preceding exam ple uses getObject , but t he principle holds for ot her get XXX calls as w ell. Heres an exam ple t hat print s each row of a result set as a com m a-separat ed list of values, w it h each NULL value print ed as t he st ring NULL : Statement s = conn.createStatement ; s.executeQuery SELECT name, birth, foods FROM profile; ResultSet rs = s.getResultSet ; ResultSetMetaData md = rs.getMetaData ; int ncols = md.getColumnCount ; while rs.next loop through rows of result set { for int i = 0; i ncols; i++ loop through columns { String val = rs.getString i+1; if i 0 System.out.print , ; if rs.wasNull System.out.print NULL; else System.out.print val; } System.out.println ; } rs.close ; close result set s.close ; close statement

2.10 Writing an Object-Oriented MySQL Interface for PHP