How should I build a simple database package for my python application?

Posted by Carson Myers on Stack Overflow See other posts from Stack Overflow or by Carson Myers
Published on 2010-05-18T02:56:50Z Indexed on 2010/05/18 3:00 UTC
Read the original article Hit count: 167

I'm building a database library for my application using sqlite3 as the base. I want to structure it like so:

db/
    __init__.py
    users.py
    blah.py
    etc.py    

So I would do this in Python:

import db
db.users.create('username', 'password')

I'm suffering analysis paralysis (oh no!) about how to handle the database connection. I don't really want to use classes in these modules, it doesn't really seem appropriate to be able to create a bunch of "users" objects that can all manipulate the same database in the same ways -- so inheriting a connection is a no-go.

Should I have one global connection to the database that all the modules use, and then put this in each module:

#users.py
from db_stuff import connection

Or should I create a new connection for each module and keep that alive?

Or should I create a new connection for every transaction?

How are these database connections supposed to be used? The same goes for cursor objects: Do I create a new cursor for each transaction? Create just one for each database connection?

© Stack Overflow or respective owner

Related posts about python

Related posts about sqlite3