I am inserting a row into my table and while I select data from it using the same cursor, it works fine. But when I try view it by other means or create a new cursor, it seems to magically disappear. Here is what I mean:
>>> 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__'
>>>
Can anyone tell me why MySQLdb is doing this?
I believe that your problem is that you’re not committing your transaction. Try to call
commit()after your insert.That is, you have to do