使用Python更新MySQL表时出现问题';s MySQLdb

使用Python更新MySQL表时出现问题';s MySQLdb,python,sql,mysql,Python,Sql,Mysql,我正在尝试使用Python的MySQLdb模块更新MySQL表。尽管查询看起来相当简单,但它不会更新信息。这是我的密码: 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

我正在尝试使用Python的MySQLdb模块更新MySQL表。尽管查询看起来相当简单,但它不会更新信息。这是我的密码:

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: 0
Username: antuan; Rowcount: 0
Username: tuyo; Rowcount: 0
Username: angela; Rowcount: 0
Username: felipe; Rowcount: 0
Username: Meni; Rowcount: 0
Username: tronco; Rowcount: 0
Username: queque; Rowcount: 0
Username: 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, -1), 'tuyo': (28672, 1024000), 'angela': (45056, 2048000), 'felipe': (53248, 307200), 'Meni': (86016, 4096000), 'tronco': (3067904, 1024000), 'queque': (61440, 4608000), 'cerel': (110592, 5632000), 'carok': (-1, -1), 'niu': (-1, -1)}
“用户”是一本包含以下内容的词典:

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, -1), 'tuyo': (28672, 1024000), 'angela': (45056, 2048000), 'felipe': (53248, 307200), 'Meni': (86016, 4096000), 'tronco': (3067904, 1024000), 'queque': (61440, 4608000), 'cerel': (110592, 5632000), 'carok': (-1, -1), 'niu': (-1, -1)}
我认为这个问题可能与用户名有关,因为如果我删除它,更新就会生效。但我当然需要使用它

如有任何提示/建议,将不胜感激

非常感谢你

乌奈·罗德里格斯

--------------------------更新-------------------------

伙计们,我正在使用以下“丑陋”的解决方法。。。这是有效的:

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, month, year))
        cursor.execute("INSERT INTO " + dbtable + " (id, username, month, year, bytes, quota_in_mb) VALUES (NULL, %s, %s, %s, %s, %s)", \
                                            (username, month, year, info[0], info[1]))

我仍然有兴趣知道更新(第一次实现)有什么问题。我现在就不写剧本了。非常感谢。

您是否在更新后尝试了提交命令,如下所示

cursor.execute("UPDATE animals SET species=%s WHERE name=%s",('TEST', 'Rollo'))

cursor.connection.commit();

您能否检查表中的
username
和查询中的
username
是否具有相同的长度?如果表中有任何填充(带空格字符),它可能不会显示在SQL命令提示符中,并将导致更新匹配零行


您还可以将表的主键(
id
)与
一起传递,而不是
用户名

这个答案可能有点晚,但为了子孙后代:

在您的
游标.execute()
之后,确保调用
连接.commit()

适当的方法:

import MySQLbd
from contextlib import closing

connection = MySQLdb.connect( host="localhost",
                              user="root",
                              passwd="root",
                              db="tableName")

with closing( connection.cursor() ) as cursor:
    try:
        cursor.execute( "SQL UPDATE CODE" )
        connection.commit()
    except:
        connection.rollback()

我发现由于某种原因python不读取%s。那么使用什么呢?而不是SQL代码中的%S

最后这对我起了作用

  cur.execute ("update tablename set columnName = (?) where ID = (?) ",("test4","4"))

谢谢你的帮助!!仍然不起作用,这是我尝试的:cursor.execute(““”更新用户\u磁盘集字节=%s,配额\u in_mb=%s其中用户名=%s和月份=%s和年份=%s”“,(信息[0],信息[1],用户名,月份,年份))cursor.connection.commit();打印“用户名:%s;行数:%d”%(用户名,游标。行数)@UnaiRodriguez为什么是“;”在python中?我检查过了,没有空格。我也没有id,我只是在脚本上有用户名。我现在做的有点难看,但很有效,就是先删除行,然后插入行(而不是更新)。看起来不错,但第一行有一个打字错误(bd而不是db)