我通过将所有“插入”事件添加到名为“sqlcommand”表的单独表中来制作自动化脚本的日志。我有两个单独的表,btureading 表和 sqlcommand 表。btureading 将记录一个数字的所有输出,而 sqlcommand 将记录插入命令。
但是,我遇到了 SQL 语法错误
import mysql.connector as sql
import random as rd
from datetime import datetime
def conndev():
conn_dev = sql.connect(
host = 'localhost',
user = 'root',
passwd = '',
auth_plugin='mysql_native_password',
db = 'test_db'
)
return conn_dev
def insertsqlcommand(sql_insert):
mydb = conndev()
mycursor = mydb.cursor()
insert_sql_table = "INSERT INTO sqlcommand(sqlStatement) value('" + sql_insert + "')"
mycursor = mydb.cursor()
mycursor.execute(insert_sql_table)
mydb.commit()
def insertTodbMeter(myVal):
mydb = conndev()
mycursor = mydb.cursor()
sql_insert = "INSERT INTO btureading(sensorId,dt,flowRate,velocity,netTotalizer)VALUES('"+myVal+"')"
mycursor= mydb.cursor()
mycursor.execute(sql_insert)
mydb.commit()
return sql_insert
ts = str(datetime.now())[0:19]
flowRate= str(rd.random())
velocity= str(rd.random())
netTotalizer= str(rd.random())
for i in range(20):
sample_data = str(i+1)+",'"+ts+"',"+flowRate+","+velocity+","+netTotalizer+","+posTotalizer+","+negTotalizer
x = insertTodbMeter(sample)
insertsqlcommand(x)
德玛西亚99
翻翻过去那场雪
相关分类