for username,info in users.iteritems(): if info[0] > 0 and info[1] > 0: month = 8 year = 2010 cursor.execute(""" UPDATE users_disk SET bytes = %s,quota_in_mb = %s WHERE username = %s AND month = %s AND year = %s """,(info[0],info[1],username,month,year)) print "Username: %s; Rowcount: %d" % (username,cursor.rowcount)
输出如下所示:
Username: niu666; Rowcount: 0Username: antuan; Rowcount: 0Username: tuyo; Rowcount: 0Username: angela; Rowcount: 0Username: felipe; Rowcount: 0Username: Meni; Rowcount: 0Username: tronco; Rowcount: 0Username: queque; Rowcount: 0Username: cerel; Rowcount: 0
这意味着没有任何行已更新!该表具有以下内容:
MysqL> select * from users_disk;+----+----------+-------+------+---------+-------------+| ID | username | month | year | bytes | quota_in_mb |+----+----------+-------+------+---------+-------------+| 1 | niu666 | 8 | 2010 | 28672 | 1024000 | | 2 | antuan | 8 | 2010 | 77824 | 4608000 | | 3 | tuyo | 8 | 2010 | 28672 | 1024000 | | 4 | angela | 8 | 2010 | 45056 | 2048000 | | 5 | felipe | 8 | 2010 | 53248 | 307200 | | 6 | Meni | 8 | 2010 | 86016 | 4096000 | | 7 | tronco | 8 | 2010 | 3067904 | 1024000 | | 8 | queque | 8 | 2010 | 61440 | 4608000 | | 9 | cerel | 8 | 2010 | 110592 | 5632000 | +----+----------+-------+------+---------+-------------+9 rows in set (0.00 sec)
用户是一本包含以下内容的字典:
{'niu666': (28672,1024000),'tutk': (-1,-1),'antuan': (77824,4608000),'tolin': (-1,'tuyo': (28672,'angela': (45056,2048000),'felipe': (53248,307200),'Meni': (86016,4096000),'tronco': (3067904,'queque': (61440,'cerel': (110592,5632000),'carok': (-1,'niu': (-1,-1)}
我认为该问题可能与用户名有关,因为如果删除它,则更新有效.但当然我需要用它……
任何指针/建议将受到高度赞赏.
非常感谢,
Unai Rodriguez
————————–更新———————– –
伙计们,我正在使用以下“丑陋”的解决方法…有效:
for username,info in users.iteritems(): if info[0] > 0 and info[1] > 0: # The user has positive values,its valID! cursor.execute("DELETE FROM " + dbtable + " WHERE username = %s AND month = %s AND year = %s",\ (username,year)) cursor.execute("INSERT INTO " + dbtable + " (ID,year,bytes,quota_in_mb) VALUES (NulL,%s,%s)",\ (username,info[0],info[1]))
我仍然有兴趣知道UPDATE(第一次实现)有什么问题.我现在就会这样离开剧本.非常感谢.
解决方法 您是否在更新后尝试COMMIT命令如下?cursor.execute("UPDATE animals SET specIEs=%s WHERE name=%s",('TEST','Rollo'))cursor.connection.commit();总结
以上是内存溢出为你收集整理的使用Python的MySQLdb更新MySQL表的问题全部内容,希望文章能够帮你解决使用Python的MySQLdb更新MySQL表的问题所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)