SQL Execution
After database initialization and assignment to the Registry.DBPOOL
variable, you can always utilize the twisted.enterprise.adapi.ConnectionPool
's
API directly:
1 2 3 4 5 6 7 8
from twisted.enterprise import adbapi from twistar.registry import Registry def done(result): print "I just made a table" Registry.DBPOOL = adbapi.ConnectionPool('MySQLdb', user="twistar", passwd="apass", db="twistar") Registry.DBPOOL.runQuery("CREATE DATABASE users (id INT ...").addCallback(done)
The alternative, described below, might be much more useful for basic CRUD operations.
CRUD Interface
In the InteractionBase
you'll find all of the create/read/update/delete (CRUD)
methods supported by the internal configuration class. To use these methods, use the Registry to get the current configuration
object:
1 2 3 4 5 6 7
from twisted.enterprise import adbapi from twistar.registry import Registry Registry.DBPOOL = adbapi.ConnectionPool('MySQLdb', user="twistar", passwd="apass", db="twistar") dbconfig = Registry.getConfig() d = dbconfig.select("users", where=['first_name = ?', 'Bob'], orderby='last_name DESC', limit=100) d.addCallback(someResultHandlerFunction)
Each of the CRUD methods gives you more control on the query that is generated. For more information, see the following
methods in InteractionBase
:
- select
- update
- delete
- insert
- insertMany (a more efficient manner of inserting many rows)