Python MySQLdb:connection.close() VS。光标.close()

发布于 2024-10-27 23:28:35 字数 429 浏览 1 评论 0原文

如果我使用MySQLdb通过Python连接到MySQL-Server。我创建一个连接和一个光标,如下所示:

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

当MySQL处理完成后,应该关闭连接。现在我想知道:通过执行以下操作关闭连接是否足够:

connection.close()

或者我是否必须先关闭光标,然后再关闭连接?像这样:

cursor.close()
connection.close()

If I use MySQLdb to connect to MySQL-Server through Python. I create a connection and a cursor like this:

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

When the MySQL-processing is done one should close the connection. Now I was wondering: Is it sufficient to close the connection by doing:

connection.close()

or do I have to close the cursor first and then the connection? Like this:

cursor.close()
connection.close()

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

把时间冻结 2024-11-03 23:28:35

我将重申最佳实践,每个遇到使用 MySQLdb 或任何其他包连接 python2/3 的 sql 连接的人都需要知道这一点

(以下模拟运行假设您的 sql 数据库中有一个名为 tablename 的表。它有有 4 个列/字段,名称为 field1、field2、field3、field4)。
如果您的连接是本地的(同一台机器),那么它是 127.0.0.1 也称为“localhost”。

过程很简单 7步

  1. 创建连接
  2. 创建游标 创建
  3. 查询字符串
  4. 执行查询
  5. 提交查询
  6. 关闭游标
  7. 关闭连接

这里简单的一步是干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()

连接和游标是不同的。
连接处于SQL级别,而游标可以被视为一个数据元素。您可以在单个连接中的相同数据上有多个游标。来自同一台计算机的相同数据有多个连接是不常见的情况。

此处描述了更多内容
“游标范例并不是 Python 特有的,而是数据库本身中常见的数据结构。

根据底层实现,可能会生成多个共享与数据库相同连接的游标。关闭游标应该释放与查询相关的资源,包括从未从数据库中获取(或获取但未使用)的任何结果,但不会消除与数据库本身的连接,因此您将能够在同一数据库上获取新游标,而无需再次进行身份验证。”

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

(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).
If your connection is local (same machine) then it is 127.0.0.1 also known as "localhost".

The process is to be simple 7 steps

  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. 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.

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."

み格子的夏天 2024-11-03 23:28:35

使用完后立即关闭光标可能是最好的选择,因为您不再需要它了。但是,我还没有看到任何在数据库连接后关闭它是有害的。但由于您可以将其设置为:

cursor = conn.cursor()

我建议您之前关闭它,以防您不小心再次分配它并且数据库连接被关闭,因为这会引发错误。因此,您可能需要首先关闭它,以防止在关闭连接的情况下意外重新分配。

(有些甚至根本不关闭它,因为它被垃圾收集器收集(请参阅:在使用 sqlite 的 Python 中是否有必要关闭游标?))

参考文献:
何时使用 MySQLdb 关闭游标

在带有 sqlite 的 Python 中是否有必要关闭游标?

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?))

References:
When to close cursors using MySQLdb

In Python with sqlite is it necessary to close a cursor?

水溶 2024-11-03 23:28:35

在这个特定的上下文中,关闭连接应该足够了。
如果您正在使用多个游标等,您需要关心正确的资源管理。

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.

风吹过旳痕迹 2024-11-03 23:28:35

使用with,该工具允许您创建一个临时光标,一旦您返回到之前的缩进级别,该临时光标将关闭。

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

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

Use with, this tool allows you to create a temporary cursor that will be closed once you return to your previous indentation level.

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

(non-indented) cursor is closed.
connection.close()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文