gpt4 book ai didi

mysql - pymysql3cursor.lastrowid 有时不起作用

转载 作者:行者123 更新时间:2023-11-29 11:36:00 26 4
gpt4 key购买 nike

我对 pymysql3cursor.lastrowid 属性有一个奇怪的问题。我正在编写一个线程 python3 应用程序。它在主线程中有一个打开的 pymysql3 连接器,以及许多可以以随机方式执行选择/插入/更新的函数。我在应用程序的线程中运行这些函数。每个函数都有以下构造:

def function...(link_to_connector) 
....
cur = link_to_connector.cursor(pymysql.cursors.DictCursor)
cur.execute(...)
id = cur.lastrowid #Used only in INSERT constructions
cur.close()
...

连接器通过以下方式打开:

self.connector = pymysql.connect(host, user, pass, db, charset='utf8')
self.connector.autocommit(True)

一切正常,但有时在新插入操作后 cur.lastrowid 等于 0。数据库中实际插入的行具有非零 ID。数据库是带有 InnoDB 表的 MySQL。

谁能帮帮我

  1. 这种情况出了什么问题?

  2. 在每个函数中打开和关闭光标是一个好方法吗?

最佳答案

这个问题我已经解决了。这是一个典型的竞争条件,因为 pymysql.connector() 对象不是线程安全的东西。

关于mysql - pymysql3cursor.lastrowid 有时不起作用,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36584115/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com