Python的Mysqldb中的字典变量格式

时间:2017-04-07 13:32:10

标签: python formatting mysql-python

我试图在python 2.7中使用mysqldb insert语句中的字典变量:

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 个答案:

答案 0 :(得分:1)

在insert语句中的每个%s之后需要逗号:

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