Pass dynamic field name to query #803
-
Hey, is it possible to have an insert/update query with dynamic field names like: async with pool.acquire() as connection:
async with connection.cursor() as cursor:
await cursor.execute(
"INSERT INTO logs (id, %s) VALUES (%s, %s)",
('field_name', value1, value2),
) or async with pool.acquire() as connection:
async with connection.cursor() as cursor:
await cursor.execute(
"update logs set %s=%s", ('field_name', value)
) I tried it but I get a
|
Beta Was this translation helpful? Give feedback.
Answered by
Nothing4You
Jun 14, 2022
Replies: 1 comment 3 replies
-
values are escaped before being passed to the server as strings: cursor.mogrify("INSERT INTO logs (id, %s) VALUES (%s, %s)", ('field_name', 'foo', 'bar')) INSERT INTO logs (id, 'field_name') VALUES ('foo', 'bar') therefore you can't pass column names as args. |
Beta Was this translation helpful? Give feedback.
3 replies
Answer selected by
Nothing4You
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
values are escaped before being passed to the server as strings:
therefore you can't pass column names as args.