【发布时间】:2023-03-10 23:00:01
【问题描述】:
我正在尝试使用 psycopg2 和 faker 库在 postgres SQL 的“虚拟”表中插入数据。这是我创建的仅用于学习目的的表。它只有一列 Student_name,其类型为 char[]。下面是我的 Python 脚本
import psycopg2
from faker import Faker
fake = Faker()
conn = psycopg2.connect(database="kreiotdb", user="****", password="*****", host="127.0.0.1", port="5432")
print("Connected Successfuly")
cur = conn.cursor()
for i in range (10):
name = fake.name()
cur.execute(""" INSERT INTO "Dummy" ("Student_name") VALUES (%s);""",[name])
运行脚本时出现以下错误。连接成功
Fri Nov 02 12:16:07 gaurav ~ $ python3 /Users/gaurav/Desktop/populate.py
Connected Successfuly
Traceback (most recent call last):
File "/Users/gaurav/Desktop/populate.py", line 11, in <module>
cur.execute(""" INSERT INTO "Dummy" ("Student_name") VALUES (%s);""",[name])
psycopg2.DataError: malformed array literal: "Brent Allison"
LINE 1: INSERT INTO "Dummy" ("Student_name") VALUES ('Brent Allison...
^
DETAIL: Array value must start with "{" or dimension information.
为什么它给我这个错误,我应该怎么做? 请帮忙。
【问题讨论】:
-
Postgres 中的
char[]不是字符串,而是单个字符的数组。请改用text或varchar。
标签: python-3.x postgresql psycopg2