[Twisted-Python] multiple mysql results in function

Itamar Shtull-Trauring itamar at itamarst.org
Wed Dec 12 21:12:57 EST 2007

On Wed, 2007-12-12 at 12:26 -0500, Ryan McGuire wrote:
> I am using xmlrpc to send and recieve messages to a "thought board"
> kind of thing, and being COMPLETELY new to python and twisted, I am
> kind of stuck on something.
> I have everything working except for the fact that I want to return
> multiple rows when I run the mysql query.
> Here is the code I have:

>                 return db.runQuery("""
>                         SELECT * FROM thoughts
>                         WHERE tid > (SELECT curtid FROM users
>                         WHERE uid = (SELECT uid FROM users
>                         WHERE username='%s' AND password='%s'))
>                 """ % (uname, pwrd)).addCallback(lambda results:
> results[0][2])
>                 db.stop()

1. Notice you stop the database after its returned from function; i.e.
it won't be.

2. Better to keep around a single database pool instance (e.g. as
attribute of xmlrpc resource).

3. Perhaps you should, once you've familiarized yourself with DB-API and
mysqldb's implementation, look at runInteraction method of the dbpool
that lets you run a whole function instead of just a single SQL query in
the context of a transaction. The adbapi HOWTO covers this.

More information about the Twisted-Python mailing list