Python和Mysql问题

2024-05-13 10:45:48 发布

您现在位置:Python中文网/ 问答频道 /正文

我将Mysql数据库连接到python项目,但不起作用…如何解决这个错误?(我从学校学到了这个代码)

我的代码:

import mysql.connector

Con = mysql.connector.connect(host='localhost', user='root', passwd='', database='python_test')
Cu = Con.cursor()
sql = "INSERT INTO clerk VALUES (%s, %d)"
val = [
 ('Peter', 100),
 ('Amy', 300),
 ('Hannah', 250),
 ('Michael', 1500),
 ('Sandy', 2500),
]

Cu.executemany(sql, val)

Con.commit()

错误:

Traceback (most recent call last):
  File "C:/Users/Ramin/PycharmProjects/First program/Mysql.py", line 23, in <module>
    Cu.executemany(sql, val)
  File "C:\Users\Ramin\AppData\Local\Programs\Python\Python36-32\lib\site-packages\mysql\connector\cursor.py", line 652, in executemany
    stmt = self._batch_insert(operation, seq_params)
  File "C:\Users\Ramin\AppData\Local\Programs\Python\Python36-32\lib\site-packages\mysql\connector\cursor.py", line 597, in _batch_insert
    "Not all parameters were used in the SQL statement")
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement

Tags: inpysqlconnectorlinemysqlvalcon