cx_Oracle execute/executemany 更新时出错 table
cx_Oracle execute/executemany error when updating table
我需要从我的 Python 代码有条件地更新 Oracle table。这是一段简单的代码,但我遇到了 cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number 并进行了以下尝试
id_as_list = ['id-1', 'id-2'] # list of row IDs in the DB table
id_as_list_of_tuples = [('id-1'), ('id-2')] # the same as list of tuples
sql_update = "update my_table set processed = 1 where object_id = :1"
# then when I tried any of following commands, result was "illegal variable name/number"
cursor.executemany(sql_update, id_as_list) # -> ends with error
cursor.executemany(sql_update, id_as_list_of_tuples) # -> ends with error
for id in id_as_list:
cursor.execute(sql_update, id) # -> ends with error
正确的解决方案是在 SQL 语句中使用字典列表和键名:
id_as_list_of_dicts = [{'id': 'id-1'}, {'id': 'id-2'}]
sql_update = "update my_table set processed = 1 where object_id = :id"
cursor.executemany(sql_update, id_as_list_of_dicts) # -> works
for id in id_as_list_of_dicts:
cursor.execute(sql_update, id) # -> also works
我找到了一些帮助和教程,例如 this,它们都使用“:1, :2,...”语法(但另一方面,我还没有找到任何更新示例和 cx_Oracle)。虽然我的问题已经在字典的帮助下得到解决,但我想知道这是否是常见的更新方式,或者我是否在“:1, :2,...”语法中做错了什么。
甲骨文 12c,Python3.7,cx_Oracle7.2.1
您确实可以使用字典进行绑定,但是创建字典的开销可能是不受欢迎的。您需要确保在使用 executemany()
时创建序列列表。所以在你的情况下,你想要这样的东西:
id_as_list = [['id-1'], ['id-2']] # list of row IDs in the DB table
id_as_list_of_tuples = [('id-1',), ('id-2',)] # the same as list of tuples
首先你有一个字符串列表。字符串本身就是序列,因此在这种情况下 cx_Oracle 需要 4 个绑定变量(每个字符串中的字符数)。
在第二个实例中,您拥有与第一个实例相同的数据——因为您只是在字符串周围添加了括号,而不是创建元组!您需要像我的示例中所示的尾随逗号来创建您认为正在创建的元组!
我需要从我的 Python 代码有条件地更新 Oracle table。这是一段简单的代码,但我遇到了 cx_Oracle.DatabaseError: ORA-01036: illegal variable name/number 并进行了以下尝试
id_as_list = ['id-1', 'id-2'] # list of row IDs in the DB table
id_as_list_of_tuples = [('id-1'), ('id-2')] # the same as list of tuples
sql_update = "update my_table set processed = 1 where object_id = :1"
# then when I tried any of following commands, result was "illegal variable name/number"
cursor.executemany(sql_update, id_as_list) # -> ends with error
cursor.executemany(sql_update, id_as_list_of_tuples) # -> ends with error
for id in id_as_list:
cursor.execute(sql_update, id) # -> ends with error
正确的解决方案是在 SQL 语句中使用字典列表和键名:
id_as_list_of_dicts = [{'id': 'id-1'}, {'id': 'id-2'}]
sql_update = "update my_table set processed = 1 where object_id = :id"
cursor.executemany(sql_update, id_as_list_of_dicts) # -> works
for id in id_as_list_of_dicts:
cursor.execute(sql_update, id) # -> also works
我找到了一些帮助和教程,例如 this,它们都使用“:1, :2,...”语法(但另一方面,我还没有找到任何更新示例和 cx_Oracle)。虽然我的问题已经在字典的帮助下得到解决,但我想知道这是否是常见的更新方式,或者我是否在“:1, :2,...”语法中做错了什么。
甲骨文 12c,Python3.7,cx_Oracle7.2.1
您确实可以使用字典进行绑定,但是创建字典的开销可能是不受欢迎的。您需要确保在使用 executemany()
时创建序列列表。所以在你的情况下,你想要这样的东西:
id_as_list = [['id-1'], ['id-2']] # list of row IDs in the DB table
id_as_list_of_tuples = [('id-1',), ('id-2',)] # the same as list of tuples
首先你有一个字符串列表。字符串本身就是序列,因此在这种情况下 cx_Oracle 需要 4 个绑定变量(每个字符串中的字符数)。
在第二个实例中,您拥有与第一个实例相同的数据——因为您只是在字符串周围添加了括号,而不是创建元组!您需要像我的示例中所示的尾随逗号来创建您认为正在创建的元组!