Ivan Pozniak
Ivan Pozniak

Reputation: 121

Insertion psycopg2.errors.SyntaxError

I try to insert data in exist table:

img_query = "INSERT INTO images (img_name) VALUES ({}) RETURNING id".format(img)
img_id = self.engine.execute(img_query)

I use sqlalchemy engine for execution. As a result received this type of mistakes:

sqlalchemy.exc.ProgrammingError: (psycopg2.errors.SyntaxError) syntax error at or near "_Group_Large_Group_12_Group_Large_Group_12_15"
LINE 1: INSERT INTO images (img_name) VALUES (12_Group_Large_Group_1...

I tried to change "12_" on "12" or replace all "_" on "-". Result not changed I received same error.

Upvotes: 0

Views: 129

Answers (1)

mechanical_meat
mechanical_meat

Reputation: 169274

You need to allow psycopg2 to do the quoting for you:

img_query = "INSERT INTO images (img_name) VALUES (%s) RETURNING id;"
img_id = self.engine.execute(img_query,(img,))

Upvotes: 1

Related Questions