2013-01-14 21 views
0

您好!新SQLite3sqlite3 - 創建遊標並執行

有一些示例程序,其中「 執行..之前創建一個」光標「和其他程序不創建一個遊標。

問題:我需要創建一個「遊標」還是可以省略?

實施例下面沒有光標:下面用光標

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

使用execute未經cursor是非標準的快捷方式,即這可能不適用於其他數據庫驅動程序。 但是,如果您未執行需要輸出的SELECT,則它工作得很好。

+0

謝謝!將使用光標使其更加標準:-) – Sylvain