Reputation: 99
I am new to cx_Oracle and I referred Mastering Oracle+Python, Part 1: Querying Best Practices . Now, under the section Cursor Objects , the doc says, and I quote
"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."
Now this seems very subjective, may someone please point how many cursor.execute() statements are okay with a single cursor, should every cursor be connected and closed for every execute statement to be on safer side, something like this.
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()
OR something like a cursor for every function and reuse that cursor to execute statements in that particular function.
And does the type of query also affect the choice.
Upvotes: 0
Views: 983
Reputation: 7096
You can perform as many cursor.execute() method calls as desirable on a single cursor, and you do not need to close the cursor after each execution. Each cursor requires a certain amount of memory and there is a certain amount of compute time required to open/close cursors, but these are relatively small amounts. In most programs this is not an issue.
Hope that answers your questions!
Upvotes: 1
Reputation: 2376
The statement you quoted is right, define as many as you need, but if your code need just one then use one. Keep in mind that a cursor is using memory, so the less you use the better. If you need to have 2 cursors opened at the same time then do it, if one is sufficient then use 1.
Upvotes: 1