Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/312.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/python-3.x/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/windows/16.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 如果在关闭连接之前发生异常,并且datasbe被锁定,该如何处理sqlite连接?_Python_Python 3.x_Sqlite - Fatal编程技术网

Python 如果在关闭连接之前发生异常,并且datasbe被锁定,该如何处理sqlite连接?

Python 如果在关闭连接之前发生异常,并且datasbe被锁定,该如何处理sqlite连接?,python,python-3.x,sqlite,Python,Python 3.x,Sqlite,因此,如果我们运行上述代码并发生异常,则数据库将被锁定。那我们该怎么办呢 编辑:我知道这很容易受到sql注入的攻击。我发布这个示例只是为了快速提问。这是with的一个很好的用例: import sqlite3 location = 'data' table_name = 'table_name' try: conn = sqlite3.connect(location) c = conn.cursor() sql = 'insert into ' + table_na

因此,如果我们运行上述代码并发生异常,则数据库将被锁定。那我们该怎么办呢


编辑:我知道这很容易受到sql注入的攻击。我发布这个示例只是为了快速提问。

这是
with
的一个很好的用例:

import sqlite3

location = 'data'
table_name = 'table_name'
try:
    conn = sqlite3.connect(location)
    c = conn.cursor()

    sql = 'insert into ' + table_name + ' (id) values (%d)' % (1)
    c.execute(sql) # Some exception occurs here
    conn.commit() # This line is not reached, so database is locked.
except Exception as e:
    pass:
使用上下文对象。这些对象定义发生异常时清理的行为。在这种情况下,如果发生异常,将释放光标并解锁数据库

请注意,我们需要。这是因为
sqlite3
没有将其游标设置为上下文(即使它应该这样做,其他数据库(如mysql和Postgre)也会为它们的数据库绑定这样做)<代码>关闭允许带的
在您离开带
块时知道如何处理光标

类似的模式是Python中处理文件的正确方式:

from contextlib import closing
conn = sqlite3.connect(location)

with closing(conn.cursor()) as c:
    # Do anything that may raise an exception here
正如@IljaEverilä在评论中提到的,连接本身也是上下文管理器,您肯定应该以这种方式使用它们,以确保正确清理连接:

with open('my_file.txt', 'r') as f:
    # Do anything you want here, even something that may
    # raise an exception

# the with will automatically f.close() whenever control
# leaves the with statement (whether by natural flow or
# exception)

sql=”。。。(%d)‘%(1)
是SQL注入的道路,如果您养成了这种习惯,有一天会以这种方式传递用户可控制的数据。如果您的变量是静态的,为什么不首先在SQL中编写它呢。如果使用变量,请分别使用占位符和传递数据。@IljaEverilä感谢我知道这一点。这只是我复制粘贴的一些代码,因此我可以问上面的问题。提交/回滚,这应该释放锁等。是的,您还应该使用连接作为上下文管理器,以确保它被正确处理。但是您可以通过使用
closing
包装游标来获得相同的行为。如果没有异常,这会自动关闭连接吗?如中所示,我是否必须在某个位置显式添加conn.close()?确实是的。同样的事情也发生在文件上。无论关闭了哪个文件(无论是否存在异常,或者整个
块完成)。对于游标,将发生正确的提交/回滚。
with sqlite3.connect(location) as conn:
    # Do whatever you need with conn