2014-06-25 33 views
0

我有一个表格,名为日期,正面,负面,中性的情绪。mysql在sql插入语句中写变量

我该如何写我的sql查询,以便我可以在下面的代码中插入日期值?

我认为问题在于日期时间值的%s,因为日期以外的列的所有其他%s值都可以插入。 下面的代码返回一个sql语法错误。

 for item in list_of_dates: 
    d['Period'].append(datetime.datetime.fromtimestamp(int(item)/1000).strftime('%Y-%m-%d    %H:%M:%S')) 

    combined_list=zip(d['period'],d['Positive'],d['Negative'],d['Neutral']) 


    with con: 
     cur = con.cursor() 

    for x,y,w,z in combined_list: 
     cur.execute("INSERT INTO sentiments(%s,%s,%s,%s) VALUES(%s,%s,%s,%s)" 
     %("Date","Positive","Negative","Neutral",str(x),str(y),str(w),str(z))) 

回答

0

查询的问题在于值。它应该有如下所示的所有%s的小引号。

VALUES('%s','%s','%s','%s')