[Twisted-Python] First demo of Twisted Enterprise HOWTO. Twisted RDBMS support.
marjan cinober
marjancinober at hotmail.com
Sat Jan 3 13:18:44 EST 2004
I´ve been trying to start twisted RDBMS by following howto.
After a couple of hours it's finaly working. I came up to this...
It's maybe worth updating howto with working examples to save some time. I
know that missing lines are completly obvious to most of experienced users
but I was hopeing howto will make me run some demo tests immediately, even
thow I'm just a dumb beginner.
#!/usr/bin/python
# EnterpriseDemo.py
"""First demo of Twisted Enterprise HOWTO. Twisted RDBMS support."""
from twisted.python import log
from twisted.internet import reactor
from twisted.enterprise import adbapi
# use this line for postgresql test
dbpool = adbapi.ConnectionPool("pyPgSQL.PgSQL", database='mydb')
# equivalent of cursor.execute(statement), return cursor.fetchall():
def getAge(user):
return dbpool.runQuery( """SELECT age FROM users
WHERE name = '%s'""" % user)
def printResult(l):
if l:
print l[0][0], "years old"
else:
print "No such user"
reactor.stop()
getAge("joe").addCallback(printResult).addErrback( log.err)
reactor.run()
############### Setup on Debian Testing
## su
## su - postgres
## createuser -d -a _MyUserName_
## exit
## su - _MyUserName_
## createdb -U _MyUserName_ mydb "Demo Database mydb"
## psql mydb
##
## drop table users;
## CREATE TABLE users (
## name varchar(64),
## age int
## );
##
## insert into users values( 'joe', 35);
## insert into users values( 'jonas', 25);
## insert into users values( 'josefa', 16);
## ### QUICK TEST ###
## select * from users;
## SELECT age FROM users WHERE name = 'joe';
#############
_________________________________________________________________
STOP MORE SPAM with the new MSN 8 and get 2 months FREE*
http://join.msn.com/?page=features/junkmail
More information about the Twisted-Python
mailing list