What's the difference between database object and cursor object in pysqlite2?

517 views Asked by At

In Python one may interact with an sqlite database using the pysqlite2 class.

from pysqlite2 import dbapi2 as sqlite

One way to send commands to the database is through the database object:

db = sqlite.connect('mydb.sqlite')
db.execute('CREATE TABLE IF NOT EXISTS t1(a, b, c)')

another way is through a cursor:

cur = db.cursor()
cur.execute('CREATE TABLE IF NOT EXISTS t2(x, y, z)')

Both the ways work and do the job, however I suspect that there are cases where one way is peferred over the another. What are those cases?

1

There are 1 answers

0
Glenn Maynard On BEST ANSWER

The connection.execute method is an SQLite API extension; it's not specified by DB-API: http://www.python.org/dev/peps/pep-0249/. It's just shorthand for creating a cursor and calling execute on it, returning the cursor.

You shouldn't use it if you want to be sure that your code will work with other DB-API-based database libraries.