如何格式化我的日期以使用 MySQL 和 Python (MySQLdb) 填写日期时间字段?

How can I format my date to fill in a datetime field with MySQL and Python (MySQLdb)?

我一直在尝试用每分钟针对不同客户进行的测试来填充我的数据库,但无法将我的日期格式化为进入名为时间的 datetime 字段。

格式应该是YYYY-mm-dd HH:MM:SS

import MySQLdb as mdb 
import datetime                                                
con = mdb.connect('localhost', 'testuser', 'test623', 'xymon');
now = str((datetime.datetime.now()).strftime("%Y-%m-%d %H:%M:%S"))
print '#'+now+'#'
with con:
    cur = con.cursor()
    cur.execute("INSERT INTO download(time, client1,client2,client3,client4) VALUES('%s',1500,2500,70000,100000)") , (now)
    con.commit()

发生的事情是值进入数据库但错误只是进入 0000-00-00 00:00:00

您必须向 cur.execute 调用提供 now,并且 %s 周围没有引号,这给出:

cur.execute("INSERT INTO download(time, client1,client2,client3,client4) VALUES(%s,1500,2500,70000,100000)", now)