使用mysql python在表中插入列表时发生编程错误

2024-09-22 20:27:37 发布

您现在位置: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

enter image description here


Tags: importparser密码sqlconnectordatetimemysqlres
1条回答
网友
1楼 · 发布于 2024-09-22 20:27:37

完整列表未设置为asd列,因此我们使用循环herwe is代码:

 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.")

相关问题 更多 >