Hello! new to SQLite3
there are some example programs where a “cursor” is created before
executing.. and other programs that do not create a cursor.QUESTION: do I need to create a “cursor” or it can be omitted?
Example below without a cursor:
con = sqlite3.connect(":memory:")
con.execute("create table person(firstname, lastname)")
Example below WITH a cursor:
db = sqlite3.connect('test.db')
db.row_factory = sqlite3.Row
db.execute('drop table if exists test')
db.execute('create table test(t1 text, i1 int)')
db.execute('insert into test (t1, i1) values (?,?)', ('one', 1))
db.execute('insert into test (t1, i1) values (?,?)', ('two', 2))
db.commit()
cursor = db.execute('select * from test order by t1')
for row in cursor:
print(dict(row))
Using
executewithout acursoris a non-standard shortcut, i.e., this might not be available with other database drivers.But if you aren’t executing a
SELECTwhere you need the output, it works just fine.