0

How can you fix this SQL-code?

My Python code:

import os, pg, sys, re, psycopg2                                              

conn = psycopg2.connect("dbname=tk user=masi password=123")
cur = conn.cursor() 
cur.execute("""INSERT INTO courses ('course_nro')
    VALUES ( `:1` )""", ['hen'])

I get:

Traceback (most recent call last):                                            
  File "<stdin>", line 13, in <module>
psycopg2.ProgrammingError: syntax error at or near "'course_nro'"
LINE 1: INSERT INTO courses ('course_nro')
                             ^
2
  • did you tried with without quotes? Commented Nov 22, 2009 at 14:05
  • 2
    What do you think the ^ is pointing at? hehe Commented Nov 22, 2009 at 14:09

2 Answers 2

2

You made 3 different errors in the same query:

  1. Field names should not be quoted.
  2. psycopg2 uses tuples, not lists for arguments.
  3. Positional arguments like ":1" are not supported.

Change your query into:

cur.execute("""INSERT INTO courses (course_nro) VALUES (%s)""", ('hen',))

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

Comments

1

Remove the quotes around the fieldname:

INSERT INTO courses (course_nro)

1 Comment

You also need to remove the backticks around the :1 parameter.

Your Answer

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