[英]What is the alternative of string format in python
我有以下 python 代碼作為 sql 查詢來更新表中的記錄:
query = "UPDATE mytable SET data='{}' where id='{}'".format(value, id)
這更新了表中的記錄。 現在我有另一個查詢,其中包括日期時間:
send_dt = datetime.utcnow()
query = "UPDATE mytable SET data='{}', send_datetime='{}' where id='{}'".format(value, send_dt, id)
現在因為我在格式中包含send_dt
,所以它正在將類型 datetime 設置為字符串,因此我得到以下錯誤:
Conversion failed when converting date and/or time from character string.
如何更新我的查詢以使日期時間保持日期時間而不是字符串。 謝謝
使用 to_date function 轉換為日期格式。 像這樣的東西。
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)
或者使用 sql 中預定義的 function 代替計算日期。
query = "UPDATE mytable SET data='{}', send_datetime=sysdate where id='{}'".format(value, id)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.