Type Casting while passing parameters to query #289
Unanswered
jturnercode
asked this question in
Q&A
Replies: 1 comment 6 replies
-
I'm not sure I understand correctly what you are asking, but in the second query you are filtering WHERE "ts"::date='2020-03-03 12:00:00' What you are doing is pretty ambiguous in terms of types. I think you should specify your conversions more strictly, such as: WHERE "ts"::DATE=%(ts)s::DATE; |
Beta Was this translation helpful? Give feedback.
6 replies
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
-
Hello,
i am using below:
latest 3.0.11 binary
python 3.10
postgres 13
looking to see if below can be done using passing parameter features
Below works fine while passing raw sql query to cur.execute():
Note the type cast on ts (timestamp to date) to filter qry by date.
qry = """UPDATE "mytable" SET "num"=300, "name"='Fred', "ts"='2020-03-03 12:00:00 WHERE "ts"::date='2020-03-03';"""
The below does not work the same while passing as parameters into built statement:
qry = UPDATE "mytable" SET "num"=%(num)s, "name"=%(name)s, "ts"=%(ts)s
WHERE "ts"::DATE=%(ts)s;
passing dictionary:
data = {
"num": 300,
"name": 'Fred,
"ts": '2022-03-03 12:00:00',
}
cur.execute(query=qry, params=data)
ts updates existing record with say ts='2022-03-03 11:00:00' the time, ie "2022-03-03 00:00:00"
am i doing something wrong or is this unexpected due to type casting?
thank you in advance for help
Beta Was this translation helpful? Give feedback.
All reactions