Python的Mysqldb中的字典变量格式

2024-04-26 07:49:00 发布

您现在位置:Python中文网/ 问答频道 /正文

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

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")

Tags: keyexecutesqlyourfloatcursordictsyntax
1条回答
网友
1楼 · 发布于 2024-04-26 07:49:00

insert语句中的每个%s后面都需要逗号:

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

相关问题 更多 >