sqlite3 - 创建游标并执行

时间:2013-01-14 20:16:08

标签: sqlite

  

您好! SQLite3的新手

     

有一些示例程序之前创建了“游标”   执行..和其他不创建游标的程序。

     

问题:我是否需要创建“光标”或者可以省略它?

     

没有光标的示例:

    con = sqlite3.connect(":memory:")

    con.execute("create table person(firstname, lastname)")
  

以下示例使用游标:

    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))

1 个答案:

答案 0 :(得分:1)

在没有execute的情况下使用cursor是一种非标准的快捷方式,即其他数据库驱动程序可能无法使用此快捷方式。 但是如果你没有执行需要输出的SELECT,它就可以正常工作。

相关问题