简体   繁体   中英

What is the alternative of string format in python

I have below python code as sql query to update a record in table:

query = "UPDATE mytable SET data='{}' where id='{}'".format(value, id)

This updated the record in table. Now I have another query which include datetime:

send_dt = datetime.utcnow()
query = "UPDATE mytable SET data='{}', send_datetime='{}' where id='{}'".format(value, send_dt, id)

Now because I have include send_dt inside format, it is making the type datetime to string and thus I am getting below error:

Conversion failed when converting date and/or time from character string.

How can I update my query so that datetime remains datetime and not string. Thanks

use to_date function to convert into date format. Somthing like this.

send_dt = datetime.utcnow()
query = "UPDATE mytable SET data='{}', send_datetime=to_date('{}','YYYY-MM-DD hh24:mi:ss.FF9') where id='{}'".format(value, str(send_dt), id)

or instead of calculating the date use the predefined function in the sql.

query = "UPDATE mytable SET data='{}', send_datetime=sysdate where id='{}'".format(value, id)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM