我想在甲骨文数据库里写个约会
sql = """INSERT INTO app_mobile_scout
(type_event, date_event, version_app, UUID, name_event, description, device_model, IMEI, ip_device).
values ('%s', to_date('%s', "yyyy/mm/dd hh24:mi:ss"), '%s', '%s', '%s', '%s', '%s', '%s', '%s')"""%(type_event, date_event, version_app, UUID, name_event, description, device_mod
res = cur.execute(sql)我有个错误:
RuntimeError: "mi" not found for replace in "INSERT INTO app_mobile
(type_event, date_event, version_app, UUID, name_event, description, device_model, IMEI, ip_device).
values ('2', to_date('2017/03/16 11:46:06', "yyyy/mm/dd hh24:mi:ss"), '4.0.4',......我做错了什么?
发布于 2017-03-17 09:37:36
首先,在SQL中您应该使用对字符串使用单引号。双引号用于标识符。
values ('%s', to_date('%s', 'yyyy/mm/dd hh24:mi:ss')
# ^ ^# Note: Doesn't work yet.
cursor.execute("""
INSERT INTO app_mobile_scout (
type_event,
date_event,
version_app,
-- etc
) VALUES (
:type, -- <-- use the variable 'type' here.
to_date(:date, 'YYYY/MM/DD HH24:MI:SS'),
:version,
-- etc
);
""", {
'type': type_event, # <-- bind `type_event` to the variable 'type'
'date': date_event,
'version': version,
# etc.
})现在,由于一些未知的原因,Oracle数据库将字符串中的:MI和:SS解释为占位符,从而导致OP所看到的错误。我认为这是甲骨文方面的一个错误。正如OP所证实的,它似乎可以通过加倍“转义”冒号来解决。
to_date(:date, 'YYYY/MM/DD HH24::MI::SS'),发布于 2017-03-17 09:32:52
与Python不同,Oracle不像双引号'那样解释单引号"。
在您的情况下,日期格式写在双引号之间,这是错误的。
换句话说,更改:[...], to_date('2017/03/16 11:46:06', "yyyy/mm/dd hh24:mi:ss"), [...]到[...], to_date('2017/03/16 11:46:06', 'yyyy/mm/dd hh24:mi:ss'), [...]
关于Oracle中的单引号和双引号:https://community.oracle.com/message/3853568#3853568
https://stackoverflow.com/questions/42853638
复制相似问题