SQL语句参数不足错误
我正在尝试将 csv 文件导入 SQL 表,但收到此错误“SQL 语句参数不足”,我认为这是 SQL 查询中的某些内容...
import mysql.connector as msql
import csv
conn = msql.connect(host='localhost', user='root', password='', database='test_database')
my_cursor = conn.cursor()
csv_data = csv.reader(open('lycee.csv'))
header = next(csv_data)
print('Importing the CSV Files')
for row in csv_data:
print(row)
my_cursor.execute("INSERT INTO lycee(date,etudiant,universite,age)
VALUES(%d,%s,%s,%d)", row)
conn.commit()
i'm trying to import csv file into SQL table but i received this error "Not enough parameters for the SQL statement" i think that it's something in the SQL query ...
import mysql.connector as msql
import csv
conn = msql.connect(host='localhost', user='root', password='', database='test_database')
my_cursor = conn.cursor()
csv_data = csv.reader(open('lycee.csv'))
header = next(csv_data)
print('Importing the CSV Files')
for row in csv_data:
print(row)
my_cursor.execute("INSERT INTO lycee(date,etudiant,universite,age)
VALUES(%d,%s,%s,%d)", row)
conn.commit()
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
假设 lycee.csv 有 4 列,
当您这样做时,
您实际上传递了 1 个数组参数,但预期是 4 个。您需要做的就是解开数组
Asssuming
lycee.csv
has 4 columnsWhen you do
you actually pass 1 array argument, but 4 expected. All you need to do is to unwrap your array
Row 在这里是一个列表或字典。您要求提供 4 个参数,但只提供 1 个参数将它们放在那里。
行将是这样的:
所以类似:
可以提供日期,假设 csv.reader 的输出是列表的列表。对其他值也执行相同的操作。
Row is an list or dictionary here. You're asking for 4 parameters to be provided but only provide 1 to place them on there.
Row would be something like this:
So something like:
could provide the date, assuming the output of the csv.reader is a list of lists. Do the same for the other values as well.