如果您遇到错误,请提供错误(尽可能完整的堆栈跟踪)。但是,即使没有这些,您也可能会遇到或将遇到错误处理插入的问题。
sample_data = str(i+1)+",'"+ts+"',"+flowRate+","+velocity+","+netTotalizer+","+posTotalizer+","+negTotalizer
此处的粗体显示您在何处添加单引号sample_data。然后您使用insertTodbMeter包含单引号的值调用,但这些值并非全部引用,这意味着这将中断:
sql_insert = "INSERT INTO btureading(sensorId,dt,flowRate,velocity,netTotalizer)VALUES('"+myVal+"')"
正确的方法是使用准备好的语句/参数化查询。不要尝试构建 SQL 字符串;传递您的值并让数据库库处理它。这是基于此示例的起点:
def insert_to_db_meter(sensor_id, dt, flow_rate, velocity, net_totalizer):
mydb = conndev()
mycursor= mydb.cursor(prepared=True)
sql_insert = """
INSERT INTO btureading (
sensorId,
dt,
flowRate,
velocity,
netTotalizer
)
VALUES (
%s,
%s,
%s,
%s,
%s
)
"""
mycursor.execute(
sql_insert,
(
sensor_id,
dt,
flow_rate,
velocity,
net_totalizer,
),
)
mydb.commit()
return sql_insert
SMILET
萧十郎
茅侃侃
相关分类