簡體   English   中英

ProgrammingError while insert list in table using mysql python

[英]ProgrammingError while insert list in table using mysql python

這是我的代碼:

import mysql.connector
import datetime
import dateutil.parser
import soundfile as sf

mydb = mysql.connector.connect(
host="localhost",
user="py",
password="12345678",
database="mydatabase"
               )
mycursor = mydb.cursor()
print(res )
val = res
sql = """INSERT INTO customers (adress) VALUES (%s) """

mycursor.executemany(sql, val)
mydb.commit()
print(mycursor.rowcount, "record inserted.")

列表的值為

['2021-05-21 04:00:32.071', '2021-05-21 04:00:33.071', '2021-05-21 04:00:34.071', '2021-05-21 04:00:35.061', '2021-05-21 04:00:36.071', '2021-05-21 04:00:37.071', '2021-05-21 04:00:38.071', '2021-05-21 04:00:39.061', '2021-05-21 04:00:40.071', '2021-05-21 04:00:41.071', '2021-05-21 04:00:42.071', '2021-05-21 04:00:43.061', '2021-05-21 04:00:44.071', '2021-05-21 04:00:45.071']

這是錯誤:

ProgrammingError: Not all parameters were used in the SQL statement

在此處輸入圖像描述

完整列表未設置 asd 列,因此我們使用 for 循環我們是代碼:

 import mysql.connector
 import datetime
 import dateutil.parser
 import soundfile as sf

 mydb = mysql.connector.connect(
  host="localhost",
  user="py",
    password="12345678",
   database="mydatabase"
           )
   mycursor = mydb.cursor()
   for i in range(len(res)):    
   val = res[i]
   sql = "INSERT INTO timestamps ({}) VALUES ('{}') ".format(colname,val)
   mycursor.execute(sql, val)
   mydb.commit()
     print(mycursor.rowcount, "record inserted.")

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM