【发布时间】:2019-10-27 08:06:40
【问题描述】:
这是我的 cs50w 项目,我正在尝试将 books.csv 文件导入 postgresql 数据库,但我遇到了一些错误,我认为我的脚本有问题,有人可以纠正它...
import psycopg2
import csv
#For connecting to the database
conn = psycopg2.connect("host=hostname_here port=5432 dbname=dbname_here user=username_here password=pass_here")
cur = conn.cursor()
#importing csv file
with open('books.csv', 'r') as f:
reader = csv.reader(f)
next(reader)
for row in reader:
cur.execute("INSERT INTO book VALUES (%s, %s, %s, %s)",
row
)
conn.commit()
Traceback (most recent call last):
File "import.py", line 15, in <module>
row
psycopg2.errors.SyntaxError: INSERT has more expressions than target columns
LINE 1: INSERT INTO book VALUES ('0380795272', 'Krondor: The Betraya...
csv 文件样本: sample of csv file :
【问题讨论】:
-
请显示您的 CSV 文件中的几行。
-
我添加了我的 csv 文件的样本
标签: python python-3.x postgresql flask psycopg2