开发者

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

开发者 https://www.devze.com 2023-02-08 01:30 出处:网络
In Python one may interact with an sqlite database using the pysqlite2 class. from pysqlite2 import dbapi2 as sqlite

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('CRE开发者_如何学编程ATE 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?


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.

0

精彩评论

暂无评论...
验证码 换一张
取 消

关注公众号