Thread: [SQLObject] Close a Connection
SQLObject is a Python ORM.
Brought to you by:
ianbicking,
phd
From: <ra...@ra...> - 2006-03-17 02:59:46
|
Hi, I'm creating a simple accounting system and I let users have multiple databases for different companies (mainly just so I can create a demo database and users can create their own test systems). I'm wanting functionality so I can create, open and close a database. The problem I have is with the close! How do I close a connection? I'm using SQLObject 0.7.0 and to open a SQLite database I do ... filename=filename.replace(':', '|') connection_string = 'sqlite:/' + filename connection = connectionForURI(connection_string) sqlhub.processConnection = connection Everything from now on uses this connection. I can run this again with a different filename and everything from then on will use the new connection. But the original connection stays "open" (is that the correct word) until I terminate the instance of Python I'm running. Note: I started using straight SQLite (which is still really nice) but using SQLOject has simplified code and increased robustness. It's a nice library ;) Any help very much appreciated. Thanks, Ray Smith |
From: Oleg B. <ph...@ma...> - 2006-03-17 09:13:29
|
On Thu, Mar 16, 2006 at 08:59:43PM -0600, ra...@ra... wrote: > But the original connection stays "open" (is that the correct word) until I > terminate the instance of Python I'm running. Does connection.close() help? Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |
From: <ra...@ra...> - 2006-03-22 04:32:55
|
Quoting Oleg Broytmann <ph...@ma...>: > On Thu, Mar 16, 2006 at 08:59:43PM -0600, ra...@ra... wrote: >> But the original connection stays "open" (is that the correct word) until I >> terminate the instance of Python I'm running. > > Does connection.close() help? > > Oleg. > -- > Oleg Broytmann http://phd.pp.ru/ ph...@ph... > Programmers don't die, they just GOSUB without RETURN. Hi Oleg, This works to some degree but the original file still isn't completely free'd. (I'm using SQLite) The case that fails is: * User creates a new database * User closes database * User creates a new database with the same name as the old database (I check for file exists and delete it before creating a new database) SQLObject(SQLite?) returns that the database isn't open???? Example code: --------------------------------------------------------------- from sqlobject import * import os filename='c:/test123.db' # table to test with class Test(SQLObject): desc=StringCol() # delete filename if it exists if os.path.exists(filename): os.unlink(filename) # setup and create connection connection_string='sqlite:/' + filename.replace(':', '|') connection = connectionForURI(connection_string) sqlhub.processConnection = connection # create table / add record get record Test.createTable() Test(desc='testing 1 2 3') print Test.get(1) # close connection connection.close() sqlhub.processConnection = connection # is this required? # delete file os.unlink(filename) # do it all again connection_string='sqlite:/' + filename.replace(':', '|') connection = connectionForURI(connection_string) sqlhub.processConnection = connection Test.createTable() # crashes here Test(desc='testing 1 2 3') print Test.get(1) connection.close() sqlhub.processConnection = connection --------------------------------------------------------------- Traceback: --------------------------------------------------------------- <Test 1 desc='testing 1 2 3'> Traceback (most recent call last): File "db_test.py", line 35, in ? Test.createTable() # crashes here File "d:\dev\python24\lib\site-packages\SQLObject-0.7.0-py2.4.egg\sqlobject\main.py", line 1308, in createTable conn.createTable(cls) File "d:\dev\python24\lib\site-packages\SQLObject-0.7.0-py2.4.egg\sqlobject\dbconnection.py", line 516, in createTable self.query(self.createTableSQL(soClass)) File "d:\dev\python24\lib\site-packages\SQLObject-0.7.0-py2.4.egg\sqlobject\dbconnection.py", line 303, in query return self._runWithConnection(self._query, s) File "d:\dev\python24\lib\site-packages\SQLObject-0.7.0-py2.4.egg\sqlobject\dbconnection.py", line 219, in _runWithConnection self.releaseConnection(conn) File "d:\dev\python24\lib\site-packages\SQLObject-0.7.0-py2.4.egg\sqlobject\dbconnection.py", line 261, in releaseConnection conn.commit() pysqlite2.dbapi2.ProgrammingError: Cannot operate on a closed database. > Execution finished. --------------------------------------------------------------- Any help is very much appreciated. Thanks, Ray Smith |
From: Oleg B. <ph...@ma...> - 2006-03-22 07:39:29
|
On Tue, Mar 21, 2006 at 10:32:20PM -0600, ra...@ra... wrote: > # close connection > connection.close() > sqlhub.processConnection = connection # is this required? > > # do it all again > connection_string='sqlite:/' + filename.replace(':', '|') > connection = connectionForURI(connection_string) dbconnection.TheURIOpener objects caches the connections by URIs. If you want to reopen another connection with the same URI you have to clean .cachedURIs mapping first. (I got the idea from the code, but I never tested it.) Oleg. -- Oleg Broytmann http://phd.pp.ru/ ph...@ph... Programmers don't die, they just GOSUB without RETURN. |
From: <ra...@ra...> - 2006-03-22 14:13:27
|
Quoting Oleg Broytmann <ph...@ma...>: > On Tue, Mar 21, 2006 at 10:32:20PM -0600, ra...@ra... wrote: >> # close connection >> connection.close() >> sqlhub.processConnection = connection # is this required? >> >> # do it all again >> connection_string='sqlite:/' + filename.replace(':', '|') >> connection = connectionForURI(connection_string) > > dbconnection.TheURIOpener objects caches the connections by URIs. If you > want to reopen another connection with the same URI you have to clean > .cachedURIs mapping first. (I got the idea from the code, but I never > tested it.) > > Oleg. Thanks Oleg, I added a: dbconnection.TheURIOpener.cachedURIs={} before opening the new connection and everything works great. Thanks for all your help. It is very much appreciated. Regards, Ray Smith |