我用它来创建表:
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'...
答案 0 :(得分:1)
不应该是这样吗?
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"))