2017-04-07 91 views
0

我想要使用MySQLdb的INSERT语句解释变量在Python 2.7:字典可变格式化

cursor.execute("INSERT INTO DCPOWERSTATS(TS,TOTALPOWER,ITPOWER,AVAILABLEPOWER,PuE) VALUES (%s %s %s %s %s)",(dict_timestamp[key], dict_ABFeeds[key], dict_ABITLOAD[key], 1400, PuE)) 

和表:

sql_createpowertable = '''CREATE TABLE IF NOT EXISTS DCPOWERSTATS (TS DATETIME ,TOTALPOWER FLOAT,ITPOWER FLOAT, AVAILABLEPOWER FLOAT, PuE FLOAT)''' 

cursor.execute(sql_createpowertable) . 

当我尝试这个,我得到错误:

_mysql_exceptions.ProgrammingError: (1064, "You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '483.9 334.4 1400 1.44706937799043)' at line 1") 

回答

1

在插入语句中,每个%s之后需要逗号:

INSERT INTO DCPOWERSTATS(TS,TOTALPOWER,ITPOWER,AVAILABLEPOWER,PuE) VALUES (%s, %s, %s, %s, %s) 
+0

我这方面很愚蠢的错误。谢谢@Daniel – sach20