Psycopg2 SyntaxError: invalid syntax on "INSERT INTO" database

John Gordon gordon at
Thu Jan 10 01:08:05 CET 2013

In <b1c7ea47-47b4-4784-8037-e0856fdaa7cd at> andydtaylor at writes:

> I'm a bit stuck on this "INSERT INTO" syntax error. I have no idea why it's

What syntax error?  It's always helpful if you can post the actual error

> not working actually... I've tried changing column types to char but that
> didn't work. I've gone a bit blind looking at it, but hopefully you can set
> me right. With the '#'d out lines instead the file does work.

> #!/usr/bin/python
> import psycopg2
> import sys

> def main():
>    db = psycopg2.connect(
>       host = 'localhost',
>       database = 'gisdb',
>       user = 'postgres',
>       password = '######'
>    )
>    cursor = db.cursor()
>    cursor.execute("DROP TABLE IF EXISTS tubecross")
>    cursor_to.execute("CREATE TABLE tubecross (id serial PRIMARY KEY, station_code char, SAJ interval, SPB interval, SOQ interval);")
>    #cursor.execute("CREATE TABLE tubecross (id serial PRIMARY KEY, num integer, data varchar);")
>    #cursor.execute("INSERT INTO tubecross (num, data) VALUES (%s, %s)",(900, "9abc'def"))
>    cursor_to.execute("INSERT INTO tubecross (station_code, SAJ, SPB, SOQ) VALUES (%s, %s, %s, %s)",(SAJ, 00:00, 00:22, 00:27))
>    db.commit()

> if __name__ == "__main__":
> 	main()

You appear to have two very different versions of the tubecross table.
One version has three fields (id, num, data) and the other version has at
least four (station_code, SAJ, SPB, SOQ).  Which one is correct?

Also, what is the 'cursor_to' variable?  It doesn't appear to be defined

John Gordon                   A is for Amy, who fell down the stairs
gordon at              B is for Basil, assaulted by bears
                                -- Edward Gorey, "The Gashlycrumb Tinies"

More information about the Python-list mailing list