1

I am using this to create table:

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)
);
""")

But when I try to insert into the database with this:

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

This is what I get:

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'...
2
  • You need to commit your transaction with dbconn.commit() otherwise your table won't get created. Commented Aug 15, 2013 at 7:47
  • @BurhanKhalid tried! but it does not work! Commented Aug 15, 2013 at 17:33

1 Answer 1

1

Shouldn't it be like this?

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

example from psycopg documentation:

>>> cur.execute("INSERT INTO test (num, data) VALUES (%s, %s)",
...      (100, "abc'def"))
Sign up to request clarification or add additional context in comments.

1 Comment

Still it does not work! :( Traceback (most recent call last): File "<pyshell#13>", line 1, in <module> cur.execute("INSERT INTO Person (LastName,FirstName,Address,City) VALUES (%s, %s, %s, %s);", ("aa", "bb", "cc", "dd")) InternalError: current transaction is aborted, commands ignored until end of transaction block

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.