Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
155 views
in Technique[技术] by (71.8m points)

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

question from:https://stackoverflow.com/questions/65860583/what-is-the-alternative-of-string-format-in-python

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

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)

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...