2

I am having trouble inserting a geometry, queried from one table, into a geom column in a new table in postgis using python (psycopg2). The code:

> cur.execute("select * from a_schema.a_table limit " + str(limit) + "
> offset " + str(offset) + ";")
>     features = cur.fetchall()
> 
>     for feature in features:
>             cur.execute("INSERT INTO a_schema.a_table (gid, id, class, geom, country, state, updated) VALUES (nextval('seq_atable_gid'), " +
> str(feature[1]) + ", " + str(feature[2]) + ", "+ feature[3] + ", " +
> str(feature[4]) + ", " + str(feature[5]) + ", " + str(feature[6]) +
> ");")

The error:

Traceback (most recent call last): File "E:/add_data_postgres_v3.py", line 79, in cur.execute("INSERT INTO a_schema.a_table (gid, id, class, geom, country, state, updated) VALUES (nextval('seq_atables_gid'), " + str(feature1) + ", " + str(feature[2]) + ", " + feature[3] + ", " + str(feature[4]) + ", " + str(feature[5]) + ", " + str(feature[6]) + ");") ProgrammingError: syntax error at or near "B000000F06DFAB31F491AC0C6BFCFB870104040F05014E813491AC07AA702EE791040400CC85EEFFE481AC06808C72C7B1040400D71AC8BDB481AC0EBE6E26F7B104040A609DB4FC6481AC032C7F2AE7A1040407347FFCBB5481AC07061DD787710404040BFEFDFBC481AC08FE21C7574104040BD1AA034D4481AC037FFAF3A72104040570A815CE2481AC0FC00A4367110404023F609A018491AC05680EF366F104040F06DFAB31F491AC0C6BFCFB870104040" LINE 1: ...age, 0106000020E6100000010000000103000000010000000B000000F06...

The geom column is of type geometry(MultiPolygon,4326). How would this be rectified?

EDIT: kttii answer solved the geom issue and I accepted the answer. However, I ran into a date issue afterwards and resorted to using this method of insert as described by the psycopg2 docs.

>>> cur.execute(
...     """INSERT INTO some_table (an_int, a_date, a_string)
...         VALUES (%s, %s, %s);""",
...     (10, datetime.date(2005, 11, 18), "O'Reilly"))

This method solved the geometry and date issues encoutered.

3

The error states there is a "syntax error at or near [the geometry value]", so something is wrong with what you are passing in as the geometry value.

The geometry value is being passed in as if the value were an integer (without quotes). You need to pass in the value casted as geometry (also note the quotes (') I added) :

cur.execute("select * from a_schema.a_table limit " + str(limit) + " offset " + str(offset) + ";") features = cur.fetchall()

for feature in features:
        cur.execute("INSERT INTO a_schema.a_table (gid, id, class, geom, country, state, updated) VALUES (nextval('seq_atable_gid'), " +

str(feature[1]) + ", " + str(feature[2]) + ", "+ feature[3] + ", '" + str(feature[4]) + "'::geometry, " + str(feature[5]) + ", " + str(feature[6]) + ");")

  • thanks. That solved the geometry issue. However, I ran into a date issue afterwards. I then followed the insert example given in psycopg2's docs, basic module use, which solved the geometry issue and the date issue. – PyMapr Sep 7 '16 at 7:21

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.