Threading issue with SQLite

Alan Harris-Reid aharrisreid at googlemail.com
Fri Jan 29 11:37:39 EST 2010


Hi,

I am creating a web application (using Python 3.1 and CherryPy 3.2) 
where a SQLite connection and cursor object are created using the 
following code (simplified from the original):

class MainSite:
    con = sqlite.connect('MyDatabase.db')
    cursor = con.cursor()

    def index_page():
       some HTML code
       cursor.execute(some SQL statement)
       more HTML code
      
    def another_page():
       some HTML code
       cursor.execute(anotherSQL statement)
       more HTML code

When I call a URL which launches the another_page() method I get the 
error message "sqlite3.ProgrammingError: SQLite objects created in a 
thread can only be used in that same thread."

Questions...
1.  Is there a large overhead in opening a new SQLite connection for 
each thread (ie. within each method)?
2.  Is there any way to use the same connection for the whole class (or 
should I forget that idea completely?)
3.  When a method returns to the calling method, is the connection 
automatically closed (assuming the object is local, of course) or does 
it have to be done explicitly using connection.close()?

TIA,
Alan Harris-Reid




More information about the Python-list mailing list