简体   繁体   English

python 中字符串格式的替代方案是什么

[英]What is the alternative of string format in python

I have below python code as sql query to update a record in table:我有以下 python 代码作为 sql 查询来更新表中的记录:

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:现在因为我在格式中包含send_dt ,所以它正在将类型 datetime 设置为字符串,因此我得到以下错误:

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.使用 to_date function 转换为日期格式。 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.或者使用 sql 中预定义的 function 代替计算日期。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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