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

时间:2015-01-09 13:07:18

标签: python mysql datetime mysql-python

我一直在尝试使用针对不同客户每分钟进行的测试来填写我的数据库,但我们无法将我的日期格式化为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

1 个答案:

答案 0 :(得分:0)

你必须现在提供' to cur.execute调用,%s cur.execute周围没有引号(" INSERT INTO下载(时间,client1,client2,client3,client4)VALUES(%s,1500,2500,70000,100000)&#34 ;,现在)