python用sqlite3模組操作sqlite資料庫

pythontab發表於2014-07-03

SQLite是一個包含在C庫中的輕量級資料庫。它並不需要獨立的維護程式,並且允許使用非標準變體(nonstandard variant)的SQL查詢語句來訪問資料庫。

一些應用可是使用SQLite儲存內部資料。它也可以在構建應用原型的時候使用,以便於以後轉移到更大型的資料庫。

SQLite的主要優點:

1. 一致性的檔案格式:

在SQLite的官方文件中是這樣解釋的,我們不要將SQLite與Oracle或PostgreSQL去比較,與我們自定義格式的資料檔案相比,SQLite不僅提供了很好的

移植性,如大端小端、32/64位等平臺相關問題,而且還提供了資料訪問的高效性,如基於某些資訊建立索引,從而提高訪問或排序該類資料的效能,SQLite提供的事務功能,也是在操作普通檔案時無法有效保證的。

 

2. 在嵌入式或移動裝置上的應用:

由於SQLite在執行時佔用的資源較少,而且無需任何管理開銷,因此對於PDA、智慧手機等

移動裝置來說,SQLite的優勢毋庸置疑。

 

3. 內部資料庫:

在有些應用場景中,我們需要為插入到資料庫伺服器中的資料進行資料過濾或資料清理,以保證最終插入到資料庫伺服器中的資料有效性。有的時候,資料是否有效,不能透過單一一條記錄來進行判斷,而是需要和之前一小段時間的歷史資料進行特殊的計算,再透過計算的結果判斷當前的資料是否合法。

在這種應用中,我們可以用SQLite緩衝這部分歷史資料。還有一種簡單的場景也適用於SQLite,即統計資料的預計算。比如我們正在執行資料實時採集的服務程式,我們可能需要將每10秒的資料彙總後,形成每小時的統計資料,該統計資料可以極大的減少使用者查詢時的資料量,從而大幅提高前端程式的查詢效率。在這種應用中,我們可以將1小時內的採集資料均快取在SQLite中,在達到整點時,計算快取資料後清空該資料。

 

4. 資料分析:

可以充分利用SQLite提供SQL特徵,完成簡單的資料統計分析的功能。這一點是yaml,csv檔案無法比擬的。

 

用我的話來說,他很小,很適合做臨時的資料庫,遷移資料很簡單,直接傳遞檔案就可以了。 其實我一開是是選用leveldb的,但是他的特性像nosql,一些稍微複雜的查詢,就有些麻煩了。  

1、建立一個新的資料庫:sqlite3     檔名

這個test.db 存放著所有的資料。

sqlite3  rui.db

 

2、開啟一個已經存在的資料庫:sqlite3      已經存在的檔名

建立一個新資料庫和開啟一個已經存在的資料庫命令是一模一樣的,如果檔案在當前目錄下不存在,則新建;如果存在,則開啟。

 

3、匯入資料:.read     資料檔案

開啟記事本,並將下列 SQL 語句複製到記事本中,儲存為 test.sql 到上面說到的 Db 目錄下,在命令列環境中輸入

.read   test.sql

即將所有的資料匯入到 rui.db 資料庫中。

 4、列出所有的資料表: .tables

完成上面所有的工作以後,我們就可以列出所有的資料表了

[root@devops-ruifengyun /tmp ]$ sqlite3 rui.db 
SQLite version 3.7.17 2013-05-20 00:56:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite> .tables
ceshi  tbl1 
sqlite> 
sqlite>

5、顯示資料庫結構:.schema

其實就是一些 SQL 語句,他們描述了資料庫的結構,如圖

sqlite> .schema
CREATE TABLE tbl1(one varchar(10), two smallint);
CREATE TABLE ceshi (user text, note text);

6、顯示錶的結構:.schema    表名

sqlite> .schema ceshi
CREATE TABLE ceshi (user text, note text)

7、匯出某個表的資料: .dump    表名

sqlite> .dump tbl1
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE tbl1(one varchar(10), two smallint);
INSERT INTO "tbl1" VALUES('goodbye',20);
INSERT INTO "tbl1" VALUES('hello!',10);
COMMIT;

再來講解下python sqlite3的用法,其實和mysqldb很像吧,他的語法和mysql差不多

import sqlite3
#原文: xiaorui.cc 
#連結資料庫文,sqlite都是以檔案的形式存在的。
#如果資料庫檔案不存在,回新建一個,如果存在則開啟此檔案
conn = sqlite3.connect('example')
c = conn.cursor()
#建立table
c.execute('''create table ceshi (user text, note text)''')
  
# 插入資料,執行SQL語句
c.execute('''insert into ceshi (user,note)  values('mPfiJRIH9T','mPfiJRIH9T')''')
c.execute('''insert into ceshi (user,note)  values('7IYcUrKWbw','7IYcUrKWbw')''')
c.execute('''insert into ceshi (user,note)  values('bXB9VcPdnq','bXB9VcPdnq')''')
c.execute('''insert into ceshi (user,note)  values('2JFk7EWcCz','2JFk7EWcCz')''')
c.execute('''insert into ceshi (user,note)  values('QeBFAlYdPr','QeBFAlYdPr')''')
c.execute('''insert into ceshi (user,note)  values('bDL4T69rsj','bDL4T69rsj')''')
c.execute('''insert into ceshi (user,note)  values('BOxPqmkEd9','BOxPqmkEd9')''')
c.execute('''insert into ceshi (user,note)  values('rvBegjXs16','rvBegjXs16')''')
c.execute('''insert into ceshi (user,note)  values('CWrhA2eSmQ','CWrhA2eSmQ')''')
c.execute('''insert into ceshi (user,note)  values('qQicfV2gvG','qQicfV2gvG')''')
c.execute('''insert into ceshi (user,note)  values('s3vg1EuBQb','s3vg1EuBQb')''')
c.execute('''insert into ceshi (user,note)  values('Lne4xj3Xpc','Lne4xj3Xpc')''')
c.execute('''insert into ceshi (user,note)  values('PH3R86CKDT','PH3R86CKDT')''')
c.execute('''insert into ceshi (user,note)  values('HEK7Ymg0Bw','HEK7Ymg0Bw')''')
c.execute('''insert into ceshi (user,note)  values('lim2OCxhQp','lim2OCxhQp')''')
c.execute('''insert into ceshi (user,note)  values('kVFfLljBJI','kVFfLljBJI')''')
c.execute('''insert into ceshi (user,note)  values('Hpbs3VOXNq','Hpbs3VOXNq')''')
c.execute('''insert into ceshi (user,note)  values('f5ubmznBIE','f5ubmznBIE')''')
c.execute('''insert into ceshi (user,note)  values('beJCQA2oXV','beJCQA2oXV')''')
c.execute('''insert into ceshi (user,note)  values('JyPx0iTBGV','JyPx0iTBGV')''')
c.execute('''insert into ceshi (user,note)  values('4S7RQTqw2A','4S7RQTqw2A')''')
c.execute('''insert into ceshi (user,note)  values('ypDgkKi27e','ypDgkKi27e')''')
c.execute('''insert into ceshi (user,note)  values('Anrwx8SbIk','Anrwx8SbIk')''')
c.execute('''insert into ceshi (user,note)  values('k5ZJFrd8am','k5ZJFrd8am')''')
c.execute('''insert into ceshi (user,note)  values('KYcTv54QVC','KYcTv54QVC')''')
c.execute('''insert into ceshi (user,note)  values('Jv6OyfMA0g','Jv6OyfMA0g')''')
c.execute('''insert into ceshi (user,note)  values('kpSLsQYzuV','kpSLsQYzuV')''')
c.execute('''insert into ceshi (user,note)  values('u2zkJQWdOY','u2zkJQWdOY')''')
c.execute('''insert into ceshi (user,note)  values('D0aspFbW8c','D0aspFbW8c')''')
c.execute('''insert into ceshi (user,note)  values('CwqhvDOrWZ','CwqhvDOrWZ')''')
c.execute('''insert into ceshi (user,note)  values('Tdy5LA9sWO','Tdy5LA9sWO')''')
c.execute('''insert into ceshi (user,note)  values('76HnRVbLX0','76HnRVbLX0')''')
c.execute('''insert into ceshi (user,note)  values('B3aoFibRPV','B3aoFibRPV')''')
c.execute('''insert into ceshi (user,note)  values('7Q6lNdL5JP','7Q6lNdL5JP')''')
c.execute('''insert into ceshi (user,note)  values('Hsob6Jyv4A','Hsob6Jyv4A')''')
#將變動儲存到資料庫檔案,如果沒有執行詞語句,則前面的insert 語句操作不會被儲存
conn.commit()
c.execute('''select * from ceshi ''').fetchall()
#得到所有的記錄
rec = c.execute('''select * from ceshi''')
print c.fetchall()


相關文章