我在表中插入一行,当我使用相同的光标从中选择数据时,它工作正常。但是当我尝试通过其他方式查看它或创建一个新光标时,它似乎神奇地消失了。这就是我的意思:
>>> cur = MySQLdb.connect(user='alex', db='testing').cursor()
>>> target = 'Alex'
>>> cur.execute("""INSERT INTO user_data (nick, points) VALUES (%s, 100);""", (target.lower()))
1L
>>> cur.execute("""SELECT points FROM user_data WHERE nick = %s;""", (target.lower()))
1L
>>> total = str(cur.fetchone()[0])
>>> print total
100
>>> cur = MySQLdb.connect(user='alex', db='testing').cursor()
>>> cur.execute("""SELECT points FROM user_data WHERE nick = %s;""", (target.lower()))
0L
>>> total = str(cur.fetchone()[0])
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
TypeError: 'NoneType' object has no attribute '__getitem__'
>>>
谁能告诉我为什么 MySQLdb 会这样做?