使用python脚本将数据插入mysql表失败



我正试图将python脚本中的数据插入到sql表中。脚本运行时没有任何错误,但是当我签入表时,实际上没有插入任何数据。我在网上搜索了一下,仍然找不出问题出在哪里。如有任何帮助,我们将不胜感激。下面的代码是我迄今为止尝试过的。

ts = time.time()
timestamp = datetime.datetime.fromtimestamp(ts).strftime('%Y-%m-%d %H:%M:%S')
try:
mydb = mysql.connector.connect(host="localhost", user="root", passwd="", database="dummy_monke")
mycursor = mydb.cursor()  # to point at database table
queries = "INSERT INTO monitoring(id,lp,time) values (%s, %s, %s)"
mycursor.execute(queries,spliced, timestamp)
mydb.commit()
print(mycursor.rowcount, "record updated successfully")
except:
mydb.rollback()
print("record fail to update")

基于此帖子,您可以进行

queries = "INSERT INTO monitoring(lp,time) values (%s, %s)"
mycursor.execute(queries, (spliced, timestamp))

动态生成一个元组,并为您完成自动增量。

旁注:将queries重命名为单数query可能是有意义的。

最新更新