[问题]pyhotn - MySQLdb, 为什么MySQLdb的select值 始终不变?

2013-09-19 15:58:03 +08:00
 1989922yan
这是我在 python 中的操作:
>>> import MySQLdb
>>> connect = MySQLdb.connect(host='localhost',
... user='root',
... passwd='admin',
... charset='utf8',
... db='test',
... connect_timeout=365*24*3600)

>>> cur = connect.cursor()
>>> cur.execute('select max(Logid) from Log;')
1L
>>> cur.fetchone()
(1044122L,)
>>> cur.execute('select max(Logid) from Log;')
1L
>>> cur.fetchone()
(1044122L,)

—————————————————————————我是线—————————————————————————————

这是我在mysql中的操作:
mysql> select max(Logid) from Log;
+------------+
| max(Logid) |
+------------+
| 1044116 |
+------------+
1 row in set (0.00 sec)

mysql> select max(Logid) from Log;
+------------+
| max(Logid) |
+------------+
| 1044123 |
+------------+
1 row in set (0.00 sec)


——————————————————————————我是线————————————————————————————

我一开始注意到,fetchone()的值 与 mysql 不一致,
一开始我以为是查询的时间差,
值本身变了。

后来,我发现,fetchone()的值,根本就不会变。
有什么原因吗?
是mysql的原因吗??
还是 MySQLdb的原因???

THX!!
3111 次点击
所在节点    Python
2 条回复
VYSE
2013-09-19 16:24:04 +08:00
cursor.close()再新建查呢?
hit9
2013-12-13 15:17:03 +08:00
connect.autocommit(True) 试试,

或者把每次查询放在事务之中

这是一个专为移动设备优化的页面(即为了让你能够在 Google 搜索结果里秒开这个页面),如果你希望参与 V2EX 社区的讨论,你可以继续到 V2EX 上打开本讨论主题的完整版本。

https://www.v2ex.com/t/83021

V2EX 是创意工作者们的社区,是一个分享自己正在做的有趣事物、交流想法,可以遇见新朋友甚至新机会的地方。

V2EX is a community of developers, designers and creative people.

© 2021 V2EX