SQL语句参数不足错误

发布于 2025-01-11 07:52:37 字数 509 浏览 4 评论 0原文

我正在尝试将 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

零時差 2025-01-18 07:52:37

假设 lycee.csv 有 4 列,

当您这样做时,

my_cursor.execute("INSERT INTO lycee(date,etudiant,universite,age) 
    VALUES(%d,%s,%s,%d)", row)

您实际上传递了 1 个数组参数,但预期是 4 个。您需要做的就是解开数组

my_cursor.execute("INSERT INTO lycee(date,etudiant,universite,age) 
  VALUES(%d,%s,%s,%d)", *row)

Asssuming lycee.csv has 4 columns

When you do

my_cursor.execute("INSERT INTO lycee(date,etudiant,universite,age) 
    VALUES(%d,%s,%s,%d)", row)

you actually pass 1 array argument, but 4 expected. All you need to do is to unwrap your array

my_cursor.execute("INSERT INTO lycee(date,etudiant,universite,age) 
  VALUES(%d,%s,%s,%d)", *row)
罗罗贝儿 2025-01-18 07:52:37
my_cursor.execute("INSERT INTO lycee(date,etudiant,universite,age) 
  VALUES(%d,%s,%s,%d)", row)

Row 在这里是一个列表或字典。您要求提供 4 个参数,但只提供 1 个参数将它们放在那里。

行将是这样的:

[2020-01-01,me,universityname,32]

所以类似:

row[0]

可以提供日期,假设 csv.reader 的输出是列表的列表。对其他值也执行相同的操作。

my_cursor.execute("INSERT INTO lycee(date,etudiant,universite,age) 
  VALUES(%d,%s,%s,%d)", row)

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:

[2020-01-01,me,universityname,32]

So something like:

row[0]

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文