SQL语句多一个逗号引起的骚乱(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 ‘)’ at line 1)
多一个逗号的SQL语句
INSERT INTO start_recording (RunTime,RunTime_int,Test_Batch,Test_Version,Total_Type,Sum_Numbers,Completed)VALUES ('2020/07/10/15:03:02',1594364582,'20200619_1','v1.0_201912181512',297,16548,16548,);
报错信息
INSERT INTO start_recording (RunTime,RunTime_int,Test_Batch,Test_Version,Total_Type,Sum_Numbers,Completed)VALUES ('2020/07/10 15:03:02',1594364582,'20200619_1','v1.0_201912181512',297,16548,16548,)
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 ')' at line 1
正确的写入
def insert_Start_recording(self, table_name, dicdata): # 插入启动记录
# 打开数据库连接
db = pymysql.connect(host, user, password, DB)
# 使用cursor()方法获取操作游标
cursor = db.cursor()
dic_value = list(dicdata.values())
RunTime,RunTime_int,Test_Batch,Test_Version,Total_Type,Sum_Numbers,Completed= dic_value
# logger.info([test_batch, test_version, test_time, template, test_chart, expected_range, test_type, test_value, Timeconsuming, result, Color, Template_path, TestChart_path])
# SQL 插入语句
sql = "INSERT INTO %s (RunTime,RunTime_int,Test_Batch,Test_Version,Total_Type,Sum_Numbers,Completed) \
VALUES ('%s',%s,'%s','%s',%s,%s,%s)" % \
(table_name, RunTime,RunTime_int,Test_Batch,Test_Version,Total_Type,Sum_Numbers,Completed)
try:
# 执行sql语句
print(sql)
cursor.execute(sql)
# 执行sql语句
db.commit()
# cursor.connection.commit() # 执行commit操作,插入语句才能生效
except:
# 发生错误时回滚
db.rollback()
# cursor.close()
# 关闭数据库连接
db.close()