views:

39

answers:

1

I'm using sqlobject in Python. I connect to the database with

conn = connectionForURI(connStr)
conn.makeConnection()

This succeeds, and I can do queries on the connection:

g_conn = conn.getConnection()
cur = g_conn.cursor()
cur.execute(query)
res = cur.fetchall()

This works as intended. However, I also defined some classes, e.g:

class User(SQLObject):
    class sqlmeta:
        table = "gui_user"
    username = StringCol(length=16, alternateID=True)
    password = StringCol(length=16)
    balance = FloatCol(default=0)

When I try to do a query using the class:

User.selectBy(username="foo")

I get an exception:

...
  File "c:\python25\lib\site-packages\SQLObject-0.12.4-py2.5.egg\sqlobject\main.py", line 1371, in selectBy
    conn = connection or cls._connection
  File "c:\python25\lib\site-packages\SQLObject-0.12.4-py2.5.egg\sqlobject\dbconnection.py", line 837, in __get__
    return self.getConnection()
  File "c:\python25\lib\site-packages\SQLObject-0.12.4-py2.5.egg\sqlobject\dbconnection.py", line 850, in getConnection
    "No connection has been defined for this thread "
AttributeError: No connection has been defined for this thread or process

How do I define a connection for a thread? I just realized I can pass in a connection keyword which I can give conn to to make it work, but how do I get it to work if I weren't to do that?

+1  A: 

Do:

from sqlobject import sqlhub, connectionForURI

sqlhub.processConnection = connectionForURI(connStr)
Ian Bicking