[英]Fail to insert data into mysql table using python script
我正在嘗試將我的 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
可能是有意義的。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.