0
try:
    conn = psycopg2.connect("dbname='test1' user='postgres' host='localhost' password='123'")
    cur = conn.cursor()
    cur.execute("""INSERT INTO product_info (product_name) VALUES (%s)""", 'xxx')
except:
    print "error happens"

The above is my code snippet, I have no trouble connecting to the database, but I have some problem inserting the value into it.

I execute the same query in postgres and it works, so i think it's a syntax problem.

Can someone show me what is the right way to do insertion?

1
  • still get the same error Commented Mar 9, 2016 at 15:43

1 Answer 1

1
cur.execute("""
    insert into product_info (product_name) VALUES (%s)
""", ('xxx',))
conn.commit()

Notice that the value is passed to the method wrapped in an iterable.

Sign up to request clarification or add additional context in comments.

Comments

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.