从python脚本插入INSERT QUERY以将数据插入MySQL数据库时出错

时间:2017-09-11 00:52:03

标签: python mysql

import MySQLdb
import datetime

water = {}
water['time'] = 1500379234.16
water['resistance'] = 18.20
water['temperature'] = 21.9
water['time'] = datetime.datetime.fromtimestamp(water['time']).strftime('%Y-%m-%d %H:%M:%S') #imports to datetime 

db = MySQLdb.connect("localhost", "monitor", "password","WQMS_database")
curs = db.cursor()

curs.execute ("INSERT INTO water_data values(water['time'], water['resistance'], water['temperature'])")

错误讯息:

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 '['time'],
water['resistance'], water['temperature']' at line 1")

数据库中的字段分别为datetimefloatfloat

1 个答案:

答案 0 :(得分:0)

water['time']未按预期插值。 像这样参数化你的变量:

curs.execute ("INSERT INTO water_data values(%s, %s, %s)", (water['time'], water['resistance'], water['temperature']))