【发布时间】:2020-08-04 10:51:29
【问题描述】:
我正在尝试获取 Oracle 序列的下一个值。
def get_change_id():
# get a sequence number for each change, this will be used to group before and after changes
id = cursor_analytics.var(cx_Oracle.NUMBER)
sql = "select SNAPSHOT_GENERAL_SEQ.nextval into :next_id from sys.dual"
cursor_analytics.execute(sql, {"next_id":id})
change_id = id.getvalue()
return change_id
我收到此错误消息:
Traceback (most recent call last): File "C:/ariel_deltas/main.py", line 93, in <module> print(get_change_id()) File "C:\ariel_deltas\snapshot.py", line 125, in get_change_id cursor_analytics.execute(sql, {"next_id":id}) cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number
如果我在 SQL 周围加上 begin 和 end,我会得到:
def get_change_id():
# get a sequence number for each change, this will be used to group before and after changes
id = cursor_analytics.var(cx_Oracle.NUMBER)
sql = """"
BEGIN
select SNAPSHOT_GENERAL_SEQ.nextval into :next_id from sys.dual;
END
"""
cursor_analytics.execute(sql, {"next_id":id})
change_id = id.getvalue()
return change_id
Traceback(最近一次调用最后一次):文件“C:/ariel_deltas/main.py”, 第 93 行,在 print(get_change_id()) 文件“C:\ariel_deltas\snapshot.py”,第 130 行,在 get_change_id cursor_analytics.execute(sql, {"next_id":id}) cx_Oracle.DatabaseError: ORA-01740: 在标识符中缺少双引号
这样做可行,但感觉不对:
def get_change_id():
# get a sequence number for each change, this will be used to group before and after changes
sql = 'select SNAPSHOT_GENERAL_SEQ.nextval from sys.dual'
cursor_analytics.execute(sql)
for row in cursor_analytics.fetchall():
r = reg(cursor_analytics, row, False)
change_id = r.NEXTVAL
return change_id
【问题讨论】: