0

是否可以在打开 SQLObject 连接后手动关闭它?我试图在使用数据库文件后删除它,但似乎与数据库文件的打开连接阻止了我这样做。

例如:

from sqlobject import *
import os

# Create and open connection to a database file.
sqlhub.processConnection = connectionForURI('sqlite:path_to_db')
SomeObject.createTable()

# ...

# Delete database when finished.
os.remove('path_to_db')

给出以下错误:

WindowsError: [Error 32] The process cannot access the file because 
              it is being used by another process: 'path_to_db'
4

1 回答 1

3

似乎只是调用.close()数据库连接似乎可以解决问题:

from sqlobject import *
import os

# Create and open connection to a database file.
sqlhub.processConnection = connectionForURI('sqlite:path_to_db')

#do something with connection
pass

#close connection
sqlhub.processConnection.close()

#delete database
os.remove(path_to_db)

我只能在这里找到一点关于该close方法的信息,但可以公平地说,您可以像对待任何其他对象一样对待它。不过我没有太多经验,在解释器中,您仍然可以在分配后立即删除数据库,而无需关闭它,所以谁知道。filesqlobjectprocessConnection

于 2013-03-31T23:26:59.747 回答