sqlite python 編程接口
1. 安裝
sqlite3 可使用 sqlite3 模塊與 python 進行集成。sqlite3 模塊是由 gerhard haring 編寫的。它提供了一個與 pep 249 描述的 db-api 2.0 規(guī)范兼容的 sql 接口。您不需要單獨安裝該模塊,因為 python 2.5.x 以上版本默認自帶了該模塊。
為了使用 sqlite3 模塊,您首先必須創(chuàng)建一個表示數(shù)據(jù)庫的連接對象,然后您可以有選擇地創(chuàng)建光標對象,這將幫助您執(zhí)行所有的 sql 語句。
2. python sqlite3 模塊 api
以下是重要的 sqlite3 模塊程序,可以滿足您在 python 程序中使用 sqlite 數(shù)據(jù)庫的需求。如果您需要了解更多細節(jié),請查看 python sqlite3 模塊的官方文檔。
序號 | api & 描述 |
---|---|
1 | sqlite3.connect(database [,timeout ,other optional arguments])
該 api 打開一個到 sqlite 數(shù)據(jù)庫文件 database 的鏈接。您可以使用 ":memory:" 來在 ram 中打開一個到 database 的數(shù)據(jù)庫連接,而不是在磁盤上打開。如果數(shù)據(jù)庫成功打開,則返回一個連接對象。 當一個數(shù)據(jù)庫被多個連接訪問,且其中一個修改了數(shù)據(jù)庫,此時 sqlite 數(shù)據(jù)庫被鎖定,直到事務提交。timeout 參數(shù)表示連接等待鎖定的持續(xù)時間,直到發(fā)生異常斷開連接。timeout 參數(shù)默認是 5.0(5 秒)。 如果給定的數(shù)據(jù)庫名稱 filename 不存在,則該調用將創(chuàng)建一個數(shù)據(jù)庫。如果您不想在當前目錄中創(chuàng)建數(shù)據(jù)庫,那么您可以指定帶有路徑的文件名,這樣您就能在任意地方創(chuàng)建數(shù)據(jù)庫。 |
2 | connection.cursor([cursorclass])
該例程創(chuàng)建一個 cursor,將在 python 數(shù)據(jù)庫編程中用到。該方法接受一個單一的可選的參數(shù) cursorclass。如果提供了該參數(shù),則它必須是一個擴展自 sqlite3.cursor 的自定義的 cursor 類。 |
3 | cursor.execute(sql [, optional parameters])
該例程執(zhí)行一個 sql 語句。該 sql 語句可以被參數(shù)化(即使用占位符代替 sql 文本)。sqlite3 模塊支持兩種類型的占位符:問號和命名占位符(命名樣式)。 例如:cursor.execute("insert into people values (?, ?)", (who, age)) |
4 | connection.execute(sql [, optional parameters])
該例程是上面執(zhí)行的由光標(cursor)對象提供的方法的快捷方式,它通過調用光標(cursor)方法創(chuàng)建了一個中間的光標對象,然后通過給定的參數(shù)調用光標的 execute 方法。 |
5 | cursor.executemany(sql, seq_of_parameters)
該例程對 seq_of_parameters 中的所有參數(shù)或映射執(zhí)行一個 sql 命令。 |
6 | connection.executemany(sql[, parameters])
該例程是一個由調用光標(cursor)方法創(chuàng)建的中間的光標對象的快捷方式,然后通過給定的參數(shù)調用光標的 executemany 方法。 |
7 | cursor.executescript(sql_script)
該例程一旦接收到腳本,會執(zhí)行多個 sql 語句。它首先執(zhí)行 commit 語句,然后執(zhí)行作為參數(shù)傳入的 sql 腳本。所有的 sql 語句應該用分號 ; 分隔。 |
8 | connection.executescript(sql_script)
該例程是一個由調用光標(cursor)方法創(chuàng)建的中間的光標對象的快捷方式,然后通過給定的參數(shù)調用光標的 executescript 方法。 |
9 | connection.total_changes()
該例程返回自數(shù)據(jù)庫連接打開以來被修改、插入或刪除的數(shù)據(jù)庫總行數(shù)。 |
10 | connection.commit()
該方法提交當前的事務。如果您未調用該方法,那么自您上一次調用 commit() 以來所做的任何動作對其他數(shù)據(jù)庫連接來說是不可見的。 |
11 | connection.rollback()
該方法回滾自上一次調用 commit() 以來對數(shù)據(jù)庫所做的更改。 |
12 | connection.close()
該方法關閉數(shù)據(jù)庫連接。請注意,這不會自動調用 commit()。如果您之前未調用 commit() 方法,就直接關閉數(shù)據(jù)庫連接,您所做的所有更改將全部丟失! |
13 | cursor.fetchone()
該方法獲取查詢結果集中的下一行,返回一個單一的序列,當沒有更多可用的數(shù)據(jù)時,則返回 none。 |
14 | cursor.fetchmany([size=cursor.arraysize])
該方法獲取查詢結果集中的下一行組,返回一個列表。當沒有更多的可用的行時,則返回一個空的列表。該方法嘗試獲取由 size 參數(shù)指定的盡可能多的行。 |
15 | cursor.fetchall()
該例程獲取查詢結果集中所有(剩余)的行,返回一個列表。當沒有可用的行時,則返回一個空的列表。 |
3. 連接數(shù)據(jù)庫
下面的 python 代碼顯示了如何連接到一個現(xiàn)有的數(shù)據(jù)庫。如果數(shù)據(jù)庫不存在,那么它就會被創(chuàng)建,最后將返回一個數(shù)據(jù)庫對象。
#!/usr/bin/python import sqlite3 conn = sqlite3.connect('test.db') print "opened database successfully"
在這里,您也可以把數(shù)據(jù)庫名稱復制為特定的名稱 :memory:,這樣就會在 ram 中創(chuàng)建一個數(shù)據(jù)庫。現(xiàn)在,讓我們來運行上面的程序,在當前目錄中創(chuàng)建我們的數(shù)據(jù)庫 test.db。您可以根據(jù)需要改變路徑。保存上面代碼到 sqlite.py 文件中,并按如下所示執(zhí)行。如果數(shù)據(jù)庫成功創(chuàng)建,那么會顯示下面所示的消息:
$chmod +x sqlite.py $./sqlite.py open database successfully
4. 創(chuàng)建表
下面的 python 代碼段將用于在先前創(chuàng)建的數(shù)據(jù)庫中創(chuàng)建一個表:
#!/usr/bin/python import sqlite3 conn = sqlite3.connect('test.db') print "opened database successfully" c = conn.cursor() c.execute('''create table company (id int primary key not null, name text not null, age int not null, address char(50), salary real);''') print "table created successfully" conn.commit() conn.close()
上述程序執(zhí)行時,它會在 test.db 中創(chuàng)建 company 表,并顯示下面所示的消息:
opened database successfully table created successfully
5. insert 操作
下面的 python 程序顯示了如何在上面創(chuàng)建的 company 表中創(chuàng)建記錄:
#!/usr/bin/python import sqlite3 conn = sqlite3.connect('test.db') c = conn.cursor() print "opened database successfully" c.execute("insert into company (id,name,age,address,salary) \ values (1, 'paul', 32, 'california', 20000.00 )") c.execute("insert into company (id,name,age,address,salary) \ values (2, 'allen', 25, 'texas', 15000.00 )") c.execute("insert into company (id,name,age,address,salary) \ values (3, 'teddy', 23, 'norway', 20000.00 )") c.execute("insert into company (id,name,age,address,salary) \ values (4, 'mark', 25, 'rich-mond ', 65000.00 )") conn.commit() print "records created successfully" conn.close()
上述程序執(zhí)行時,它會在 company 表中創(chuàng)建給定記錄,并會顯示以下兩行:
opened database successfully records created successfully
6. select 操作
下面的 python 程序顯示了如何從前面創(chuàng)建的 company 表中獲取并顯示記錄:
#!/usr/bin/python import sqlite3 conn = sqlite3.connect('test.db') c = conn.cursor() print "opened database successfully" cursor = c.execute("select id, name, address, salary from company") for row in cursor: print "id = ", row[0] print "name = ", row[1] print "address = ", row[2] print "salary = ", row[3], "\n" print "operation done successfully" conn.close()
上述程序執(zhí)行時,它會產(chǎn)生以下結果:
opened database successfully id = 1 name = paul address = california salary = 20000.0 id = 2 name = allen address = texas salary = 15000.0 id = 3 name = teddy address = norway salary = 20000.0 id = 4 name = mark address = rich-mond salary = 65000.0 operation done successfully
7. update 操作
下面的 python 代碼顯示了如何使用 update 語句來更新任何記錄,然后從 company 表中獲取并顯示更新的記錄:
#!/usr/bin/python import sqlite3 conn = sqlite3.connect('test.db') c = conn.cursor() print "opened database successfully" c.execute("update company set salary = 25000.00 where id=1") conn.commit() print "total number of rows updated :", conn.total_changes cursor = conn.execute("select id, name, address, salary from company") for row in cursor: print "id = ", row[0] print "name = ", row[1] print "address = ", row[2] print "salary = ", row[3], "\n" print "operation done successfully" conn.close()
上述程序執(zhí)行時,它會產(chǎn)生以下結果:
opened database successfully total number of rows updated : 1 id = 1 name = paul address = california salary = 25000.0 id = 2 name = allen address = texas salary = 15000.0 id = 3 name = teddy address = norway salary = 20000.0 id = 4 name = mark address = rich-mond salary = 65000.0 operation done successfully
8. delete 操作
下面的 python 代碼顯示了如何使用 delete 語句刪除任何記錄,然后從 company 表中獲取并顯示剩余的記錄:
#!/usr/bin/python import sqlite3 conn = sqlite3.connect('test.db') c = conn.cursor() print "opened database successfully" c.execute("delete from company where id=2;") conn.commit() print "total number of rows deleted :", conn.total_changes cursor = conn.execute("select id, name, address, salary from company") for row in cursor: print "id = ", row[0] print "name = ", row[1] print "address = ", row[2] print "salary = ", row[3], "\n" print "operation done successfully" conn.close()
上述程序執(zhí)行時,它會產(chǎn)生以下結果:
opened database successfully total number of rows deleted : 1 id = 1 name = paul address = california salary = 20000.0 id = 3 name = teddy address = norway salary = 20000.0 id = 4 name = mark address = rich-mond salary = 65000.0 operation done successfully