mysql - 如何使用值填充 MySQL 表中的一列而不出现“未使用所有参数”编程错误?
问题描述
这是我使用的 python 3 代码:
import mysql.connector
mydb = mysql.connector.connect(
host="localhost",
user="username", #I inserted the actual username here.
passwd="password", #I inserted the actual password here.
database="mydatabase"
)
mycursor = mydb.cursor()
#degrees is the table name, degreename is the column name.
#There are a total of two columns in the table, one for the id with should auto-increment, and the 'degreename' column that I'm trying to populate.
sql = "INSERT INTO degrees (degreename) VALUES (%s)"
val = [
('Pharmacy'),
('Comp Sci'),
('Art'),
('History'),
('Psychology'),
('Medicine'),
('Sports'),
('Pottery')
]
mycursor.executemany(sql,val)
mydb.commit()
这是我得到的错误:
Traceback (most recent call last):
File "newest.py", line 45, in <module>
mycursor.executemany(sql,val)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mysql/connector/cursor.py", line 668, in executemany
stmt = self._batch_insert(operation, seq_params)
File "/Library/Frameworks/Python.framework/Versions/3.8/lib/python3.8/site-packages/mysql/connector/cursor.py", line 613, in _batch_insert
raise errors.ProgrammingError(
mysql.connector.errors.ProgrammingError: Not all parameters were used in the SQL statement
我在编写 SQL 语句时缺少什么参数(或我犯的任何其他导致此错误的错误)?
谢谢你。
解决方案
您的元组中需要一个逗号才能将它们变成元组(请参阅MySQL 连接器手册)。这将起作用:
val = [
('Pharmacy',),
('Comp Sci',),
('Art',),
('History',),
('Psychology',),
('Medicine',),
('Sports',),
('Pottery',)
]
推荐阅读
- python - 使用 ping 确定远程机器的操作系统
- ios - 如何在导航栏中设置两种颜色?
- json - 由于 JSON::ParserError 随机失败:822:意外令牌
- jquery - Safari 不加载一张图片
- c# - 无法在 C# 中读取 PDF 控件
- c# - System.Net.Mail.Attachment 文件 url 包含 #
- android - 在 Google 日历中为“一天两次”、“一天三次”等重复事件创建提醒时出现问题
- android - 为什么在我删除由 Create New Project 向导创建的代码 setSupportActionBar(toolbar) 后,选项卡式 Activity 运行良好?
- objective-c - 10.9 中的 NSScrollView 辅助功能问题
- php - PHP 如何让我的 csv 表正确显示?