简体   繁体   中英

Error with INSERT QUERY from python script to insert data into MySQL database

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

Error message:

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

The fields in the database are datetime , float and float respectively.

water['time'] is not interpolated as you expected. Parameterize your variables like this:

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM