gpt4 book ai didi

python - 无法插入到 postgresql 表中

转载 作者:行者123 更新时间:2023-11-29 13:33:36 24 4
gpt4 key购买 nike

我正在使用它来创建表:

dbconn=psycopg2.connect("dbname='postgres' host='localhost' port='5432' user='postgres' password='123456'")
cur=dbconn.cursor()
cur.execute("""
CREATE TABLE Person
(
LastName varchar(255),
FirstName varchar(255),
Address varchar(255),
City varchar(255)
);
""")

但是当我尝试用这个插入数据库时​​:

>>> cur.execute("""INSERT INTO Persons (LastName,FirstName,Address,City) VALUES ('%s','%s','%s','%s');""",("aa","bb","cc","dd"))

这是我得到的:

Traceback (most recent call last):
File "<pyshell#42>", line 1, in <module>
cur.execute("""INSERT INTO Persons (LastName,FirstName,Address,City) VALUES ('%s','%s','%s','%s');""",("aa","bb","cc","dd"))
ProgrammingError: syntax error at or near "aa"
LINE 1: ...rsons (LastName,FirstName,Address,City) VALUES (''aa'',''bb'...

最佳答案

不应该是这样吗?

cur.execute("INSERT INTO Persons (LastName,FirstName,Address,City) VALUES (%s, %s, %s, %s);", ("aa", "bb", "cc", "dd"))

示例来自 psycopg documentation :

>>> cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)",
... (100, "abc'def"))

关于python - 无法插入到 postgresql 表中,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18248198/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com