[Tutor] Python sqlite with implicit cursor open and close

Dinesh B Vadhia dineshbvadhia at hotmail.com
Fri May 18 11:34:26 CEST 2012


In the Python documentation (http://docs.python.org/library/sqlite3.html?highlight=sqlite#using-shortcut-methods) it says:
"Using the nonstandard execute(), executemany() and executescript() methods of the Connection object, your code can be written more concisely because you don't have to create the (often superfluous) Cursor objects explicitly. Instead, the Cursor objects are created implicitly and these shortcut methods return the cursor objects. This way, you can execute a SELECT statement and iterate over it directly using only a single call on the Connection object."

Here is pseudo-code:

db = sqlite.connect(path, ...)
cursor = db.cursor()

# normal method
def doDBStuff():
    cursor = db.cursor()
    cursor.execute(somestatement) or cursor.executemany(somestatement) or cursor.executescript(somestatement)
    cursor.close()
    return

# suggested method
def doDbStuffCorrectly():
    cursor.execute(somestatement) or cursor.executemany(somestatement) or cursor.executescript(somestatement)
    return

Does the implicit method close the cursor?  This would be important in a multi-user environment with read/write access.


-------------- next part --------------
An HTML attachment was scrubbed...
URL: <http://mail.python.org/pipermail/tutor/attachments/20120518/8e8b51b8/attachment.html>


More information about the Tutor mailing list