mysql.connector for python commit()不起作用

mysql.connector for python commit()不起作用,python,mysql,mysql-connector,Python,Mysql,Mysql Connector,我想转换sql表中的日期格式,但我不知道为什么这不起作用: import mysql.connector from mysql.connector import errorcode from dateutil.parser import parse appname = "dropbox" # connect to the database # Add your DB connection information try: database = mysql.connector.co

我想转换sql表中的日期格式,但我不知道为什么这不起作用:

import mysql.connector
from mysql.connector import errorcode
from dateutil.parser import parse

appname = "dropbox"

# connect to the database
# Add your DB connection information

try:

    database = mysql.connector.connect(user='root', password='root',

                              host='localhost',

                              database='google_play')

except mysql.connector.Error as err:

    if err.errno == errorcode.ER_ACCESS_DENIED_ERROR:
        print("Something is wrong with your user name or password")

    elif err.errno == errorcode.ER_BAD_DB_ERROR:
        print("Database does not exist")

    else:
        print(err)

DBcursor = database.cursor(buffered=True)
DBcursor2 = database.cursor(buffered=True)

# DB connection established. Now get the table:

query = ("SELECT * FROM googleplay_%s_test" % appname)

DBcursor.execute(query)

# Execute the date conversion:

for (id, user_name, date, url, rating, title, text, reply_date, reply_text) in DBcursor:

    date_string = parse(date)
    date_string.strftime("%Y-%m-%d")

    conversion = ("UPDATE googleplay_%s_test SET date='date_string' WHERE id='id'" % appname)

    DBcursor2.execute(conversion)

    database.commit()

    print("Convertet to: ", date_string)

# close the database connection

DBcursor.close()
DBcursor2.close()
database.close()
这种转变似乎奏效了。输出为:

Convertet to:  2016-12-02 00:00:00
Convertet to:  2016-11-25 00:00:00
Convertet to:  2016-11-16 00:00:00
Convertet to:  2016-12-04 00:00:00
这很好。但是,它不会将新值写入表中。首先,我认为commit()命令缺失,但它就在那里

这是:

conversion = ("UPDATE googleplay_%s_test SET date='date_string' WHERE id='id'" % appname)
DBcursor2.execute(conversion)
显然不会将
googleplay\u test
设置为
date\u字符串
变量的值-它会尝试将其设置为普通的
日期字符串
字符串。MySQL很有可能只是默默地跳过这个操作(好吧,最多可能发出一个警告),假装一切正常,就像默认的MySQL设置一样

编辑:这同样适用于
where
子句:

WHERE id='id'
将仅尝试更新id为普通字符串的记录
'id'

你想要:

 conversion = "UPDATE googleplay_%s_test SET date=%%s WHERE id=%%s" % appname
DBcursor2.execute(conversion, [date_string, id])
FWIW如果您只需要两个字段,最好只检索这两个字段:

query = "SELECT id, date FROM googleplay_%s_test" % appname
DBcursor.execute(query)

for id, date in DBcursor:
    # code here
当我们这样做的时候:

  • cursor.execute()
    返回查询影响的行数(选择、更新、删除等)
  • 您可能希望将您的
    数据库.commit()
    从循环中删除—一次提交要快得多,还可以确保所有更改都已应用或未应用,从而避免数据库处于半备份状态
  • 还要注意,这里作为
    date\u string
    传递的实际上不是字符串,而是
    datetime.datetime
    对象,因为您放弃了对
    date\u string.strftime()的调用结果。但这应该没问题,dbapi连接器应该知道如何在db和python类型之间转换


    最后:一个合适的数据库模式将有一个
    googleplay\u test
    表,其中
    appname
    作为字段

    如果您需要使用参数来确定表应该是什么,这意味着基础表设计中存在一个大问题。谢谢您的帮助。不幸的是,它没有改变很多。它仍然不会将新值写入表中。您的where子句中也存在同样的问题,请参见我的编辑。你本可以从我的回答中发现的……太好了!现在它开始工作了。非常感谢你的帮助!