Phillip Pearson - web + electronics notes

tech notes and web hackery from a new zealander who was vaguely useful on the web back in 2002 (see: python community server, the blogging ecosystem, the new zealand coffee review, the internet topic exchange).

2005-2-15

Optimising how you write your code so SQL doesn't hurt so much to use

I used to not like using SQL, because it meant so many lines of code to do anything. Now I have some helper functions, and just about anything is very easy.

In Python:

z, = db.query_one("SELECT a FROM foo WHERE id=%d", (myid,)) for a,b,c in db.execute("SELECT a,b,c FROM foo"): ...

In PHP:

list($z) = query_one("SELECT a FROM foo WHERE id=?", array($myid)); $sth = query("SELECT a,b,c FROM foo"); while ($r = qrow($sth)) { list($a, $b, $c) = $r; ... }

I'll leave the implementation of the execute, query and qrow functions to the reader; they're not hard. But setting your code up so that you can do SQL queries with as little work as possible will completely change how it "feels" to work with an SQL database.

... more like this: [, ]