1

我正在使用它来创建表:

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'...
4

1 回答 1

1

不应该是这样吗?

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

来自psycopg 文档的示例:

>>> cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)",
...      (100, "abc'def"))
于 2013-08-15T07:39:19.087 回答