This post originated from an RSS feed registered with Python Buzz
by Phillip Pearson.
Original Post: Optimising how you write your code so SQL doesn't hurt so much to use
Feed Title: Second p0st
Feed URL: http://www.myelin.co.nz/post/rss.xml
Feed Description: Tech notes and web hackery from the guy that brought you bzero, Python Community Server, the Blogging Ecosystem and the Internet Topic Exchange
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.