[Twisted-Python] multiple mysql results in function

Ryan McGuire RDMcGuire01 at indianatech.net
Wed Dec 12 10:26:00 MST 2007


 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:

from twisted.web import xmlrpc, server
import MySQLdb
from twisted.enterprise import adbapi

class itr(xmlrpc.XMLRPC):

    def xmlrpc_thought(self, uname, pwrd, arg, msg):
        """Return the most recent thought"""
        if arg == "get":
                db=adbapi.ConnectionPool("MySQLdb",host='192.168.0.220',user='redbeardmcg',passwd='xxxxx',db='itr')
                db.start()
                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()
        if arg == "post":
                db=adbapi.ConnectionPool("MySQLdb",host='192.168.0.220',user='redbeardmcg',passwd='xxxxx',db='itr')
                db.start()
                db.runQuery("""
                        INSERT INTO thoughts(uid,thought)
                        VALUES((SELECT uid FROM users WHERE
                        username='%s' AND password='%s'),
                        '%s')
                """ % (uname, pwrd, msg))
                x='Thought added'
                return x

if __name__ == '__main__':
    from twisted.internet import reactor
    r = itr()
    reactor.listenTCP(8899, server.Site(r))
    reactor.run()

I believe something needs to be changed in the callback, but I very well may be wrong.

Thanks in advance!

RDMcGuire01 at indianatech.net

-------------- next part --------------
An HTML attachment was scrubbed...
URL: </pipermail/twisted-python/attachments/20071212/07ad6c41/attachment.html>


More information about the Twisted-Python mailing list