简体   繁体   English

Python MySQLdb:connection.close()VS. cursor.close()

[英]Python MySQLdb: connection.close() VS. cursor.close()

If I use MySQLdb to connect to MySQL-Server through Python. 如果我使用MySQLdb通过Python连接到MySQL-Server。 I create a connection and a cursor like this: 我创建一个connection和一个像这样的cursor

connection = MySQLdb.connect(...)
cursor = connection.cursor()
# process

When the MySQL-processing is done one should close the connection . 当MySQL处理完成后,应该关闭connection Now I was wondering: Is it sufficient to close the connection by doing: 现在我想知道:通过执行以下操作来关闭connection是否足够:

connection.close()

or do I have to close the cursor first and then the connection ? 或者我必须先关闭cursor然后再connection Like this: 像这样:

cursor.close()
connection.close()

Closing the cursor as soon as you are done with it is probably the best bet, since you have no use for it anymore. 完成后关闭光标可能是最好的选择,因为你不再使用它了。 However, I haven't seen anything where it's harmful to close it after the db connection. 但是,我没有看到在数据库连接后关闭它有害的任何事情。 But since you can set it as: 但是因为您可以将其设置为:

cursor = conn.cursor()

I recommend closing it before, in case you accidentally assign it again and the DB connection is closed as this would throw an error. 我建议之前关闭它,以防你不小心再次分配它并关闭数据库连接,因为这会引发错误。 So you may want to close it first in order to prevent an accidental reassignment with a closed connection. 因此,您可能需要先将其关闭,以防止意外重新分配已关闭的连接。

(Some don't even close it at all though as it gets collected by the garbage collector (see: In Python with sqlite is it necessary to close a cursor? )) (有些甚至根本没有关闭它,因为它被垃圾收集器收集(参见: 在Python中使用sqlite是否需要关闭游标? ))

References: When to close cursors using MySQLdb 参考: 何时使用MySQLdb关闭游标

In Python with sqlite is it necessary to close a cursor? 在Python中使用sqlite是否需要关闭游标?

I will re-iterate the best practice at everyone who comes across the sql connection using MySQLdb or any other package to connect python2/3 needs to know this 我将重新尝试每个遇到sql连接的人使用MySQLdb或任何其他包来连接python2 / 3需要知道这个

(Following mock run assumes that you have a table named tablename in your sql database. It has got 4 columns/fields with names field1,field2,field3,field4). (以下模拟运行假定您的sql数据库中有一个名为tablename的表。它有4个列/字段,名称为field1,field2,field3,field4)。 If your connection is local (same machine) then it is 127.0.0.1 also known as "localhost". 如果您的连接是本地的(同一台机器),则它是127.0.0.1,也称为“localhost”。

The process is to be simple 7 steps 这个过程应该是简单的7个步骤

  1. Create connection 创建连接
  2. Create cursor 创建游标
  3. Create Query string 创建查询字符串
  4. Execute the query 执行查询
  5. Commit to the query 提交查询
  6. Close the cursor 关闭光标
  7. Close the connection 关闭连接

Here is a simple step by stem mock run 这是干模拟运行的一个简单步骤

mydb = MySQLdb.connect(host=host, user=user, passwd=passwd, db=database, charset="utf8")
cursor = mydb.cursor()
query = "INSERT INTO tablename (text_for_field1, text_for_field2, text_for_field3, text_for_field4) VALUES (%s, %s, %s, %s)"
cursor.execute(query, (field1, field2, field3, field4))
mydb.commit()
cursor.close()
mydb.close()

Connection and cursor are different. 连接和光标不同。 connection is at the SQL level while cursor can be considered as a data element. 连接在SQL级别,而游标可以被视为数据元素。 You can have multiple cursors on the same data within single connection. 您可以在单个连接中对同一数据使用多个游标。 It is an unusual occurrence to have multiple connections to same data from the same computer. 从同一台计算机到同一数据的多个连接是不常见的。

More has been described here "The cursor paradigm is not specific to Python but are a frequent data structure in databases themselves. 这里有更多描述“游标范例不是特定于Python,而是数据库本身的常见数据结构。

Depending on the underlying implementation it may be possible to generate several cursors sharing the same connection to a database. 根据底层实现,可以生成与数据库共享相同连接的多个游标。 Closing the cursor should free resources associated to the query, including any results never fetched from the DB (or fetched but not used) but would not eliminate the connection to the database itself so you would be able to get a new cursor on the same database without the need to authenticate again." 关闭游标应释放与查询关联的资源,包括从未从数据库中获取的任何结果(或已获取但未使用的结果),但不会消除与数据库本身的连接,因此您可以在同一数据库上获取新游标无需再次验证。“

Closing a connection should be good enough here in this particular context. 在这个特定的上下文中,关闭连接应该足够好。 If you are working with multiple cursors etc. you need to care about proper resource management. 如果您正在使用多个游标等,则需要关注正确的资源管理。

Use with , this tool allows you to create a temporary cursor that will be closed once you return to your previous indentation level. 使用with ,这个工具允许您创建一个临时的光标,一旦你恢复到以前的缩进级别将被关闭。

from contextlib import closing
with closing( connection.cursor() ) as cursor:
    (indented) use the cursor

(non-indented) cursor is closed.
connection.close()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM