cx_Oracle 中游标的 cursor.excute() 语句的首选数量

Preferable number of cursor.excute() statement for a cursor in cx_Oracle

我是 cx_Oracle 的新手,我推荐了 Mastering Oracle+Python, Part 1: Querying Best Practices 。现在,在 Cursor Objects 部分下,文档说,我引用

"You can define an arbitrary number of cursors using the cursor() method of the Connection object. Simple programs will do fine with just a single cursor, which can be used over and over again. Larger projects might however require several distinct cursors."

现在这看起来很主观,有人请​​指出有多少 cursor.execute() 语句可以用单个游标,如果每个游标 连接并关闭每个执行语句更安全,像这样。

cursor = connection.cursor()
cursor.execute('some query - insert,delete,upate,select')
#result = cursor.fetchall() # or some other way to obtain result if required
cursor.close() 

或者像每个函数的游标之类的东西,并重复使用该游标来执行该特定函数中的语句。

查询的类型是否也会影响选择。

你引用的说法是对的,你需要多少就定义多少,但如果你的代码只需要一个就使用一个。 请记住,游标正在使用内存,因此使用的越少越好。 如果您需要同时打开 2 个游标,则执行此操作,如果一个就足够,则使用 1.

您可以在单个游标上执行任意数量的 cursor.execute() 方法调用,并且无需在每次执行后关闭游标。每个游标都需要一定数量的内存,open/close 个游标需要一定数量的计算时间,但这些都是相对较小的数量。在大多数程序中,这不是问题。

希望这能回答您的问题!