pysqlite multiple connections
Gerhard Häring
gh at ghaering.de
Wed May 7 19:01:31 EDT 2003
* Tertius Cronje <tcronj at yahoo.co.uk> [2003-05-08 00:24 +0200]:
> Gerhard, thank you so much.
>
> This gives me a few more options to consider. The application I want
> to use it for will probably not be used by more than 10 users. I'll
> have to build some extra logic into the code to enable single use of a
> cursor.
You can have multiple cursors per connection, that's no problem.
Transactions are started implicitely by opening the first cursor for a
connection. Then you use .commit() and .rollback() on the connection
object. Unless you turn off transactions by setting autocommit=True, of
course.
> I was looking for some simple code examples but without any luck.
My small apps usually look like this:
There is a module for the database stuff that looks approx. like:
#v+
# Module db.py
con = None
def open_con():
con = sqlite.connect(...)
def getcon():
return con
#v-
Now somewhere in your app you can have code like:
#v+
# Module webui.py
import db
def get_users():
cursor = db.getcon().cursor()
cursor.execute("SELECT ...")
return cursor.fetchall()
def format_users():
buf = StringIO.StringIO()
for row in get_users():
print >>buf, ...
return str(buf)
#v-
> One example that caught my attention is the round-up project but I
> have not yet managed decipher the code.
Another prominent example is PyPI, from the same author.
Gerhard
--
mail: gh at ghaering.de
web: http://ghaering.de/
More information about the Python-list
mailing list