【发布时间】:2019-12-05 10:04:34
【问题描述】:
我试图从我的 BME280 传感器获取读数,并使用 python 脚本将它们作为一行写入 Mysql 数据库中。我敢肯定答案很简单,但是多年来我一直在努力解决这个问题。
当前错误是:Failed inserting record into table_sensor_log table 1064 (42000): 你的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 2 行的“09:29:03.708933,3fa924c8-73b1-4986-a9bc-f13fdc15163f,25.7461863119)”附近使用正确的语法 MySQL 连接已关闭
我的传感器读数正确。
几乎尝试了我能想到的所有方法 - 多次重新编写代码。
import smbus2
import bme280
port = 1
address = 0x76
bus = smbus2.SMBus(port)
calibration_params = bme280.load_calibration_params(bus, address)
# the sample method will take a single reading and return a
# compensated_reading object
data = bme280.sample(bus, address, calibration_params)
# the compensated_reading class has the following attributes
timest = data.timestamp
id = data.id
temperature = data.temperature
# there is a handy string representation too
print(data)
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect(host='localhost',
database='sensor_log',
# the compensated_reading class has the following attributes
timest = data.timestamp
id = data.id
temperature = data.temperature
# there is a handy string representation too
print(data)
import mysql.connector
from mysql.connector import Error
try:
connection = mysql.connector.connect(host='localhost',
database='sensor_log',
user='superadmin',
password='Chillipo3')
sql_insert_query = """ INSERT INTO `table_sensor_log`
(`timestamp`, `sensor_id`, `sensor_value`) VALUES (%s,%s,%s);""" % (timest,id,temperature,)
cursor = connection.cursor()
result = cursor.execute(sql_insert_query)
connection.commit()
print ("Record inserted successfully into table_sensor_log table")
except mysql.connector.Error as error :
connection.rollback() #rollback if any exception occured
print("Failed inserting record into table_sensor_log table {}".format(error))
finally:
#closing database connection.
读数正常 - 我把它们留在里面以证明我的传感器没有问题。
compensated_reading(id=3fa924c8-73b1-4986-a9bc-f13fdc15163f,时间戳=2019-07-27 09:29:03.708933,温度=25.746 °C,压力=1009.16 hPa,湿度=55.00 % rH) 将记录插入 table_sensor_log 表 1064 (42000) 失败:您的 SQL 语法有错误;检查与您的 MariaDB 服务器版本相对应的手册,以在第 2 行的“09:29:03.708933,3fa924c8-73b1-4986-a9bc-f13fdc15163f,25.7461863119)”附近使用正确的语法 MySQL 连接已关闭 2019-07-27 09:29:03.708933
【问题讨论】: