我想日期格式轉換成SQL表,我不知道爲什麼這不工作:mysql.connector爲蟒蛇的commit()不工作
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()命令丟失,但它在那裏。
如果您需要使用參數來確定您的表應該是什麼,這意味着底層表設計中存在一個大問題。 – e4c5