一個小時內學習 SQLite 資料庫

來源:互聯網
上載者:User

1. 介紹

SQLite 是一個開源的嵌入式關聯式資料庫,實現自包容、零配置、支援事務的SQL資料庫引擎。 其特點是高度便攜、使用方便、結構緊湊、高效、可靠。 與其他資料庫管理系統不同,SQLite 的安裝和運行非常簡單,在大多數情況下 - 只要確保SQLite的二進位檔案存在即可開始建立、串連和使用資料庫。如果您正在尋找一個嵌入式資料庫專案或解決方案,SQLite是絕對值得考慮。

2. 安裝

SQLite on Windows

  1. 進入 SQL 下載頁面:http://www.sqlite.org/download.html
  2. 下載 Windows 下的先行編譯二進位檔案包:
    • sqlite-shell-win32-x86-<build#>.zip
    • sqlite-dll-win32-x86-<build#>.zip
    注意: <build#> 是 sqlite 的編譯版本號碼
  3. 將 zip 檔案解壓到你的磁碟,並將解壓後的目錄添加到系統的 PATH 變數中,以方便在命令列中執行 sqlite 命令。
  4. 可選: 如果你計劃發布基於 sqlite 資料庫的應用程式,你還需要下載源碼以便編譯和利用其 API
    • sqlite-amalgamation-<build#>.zip

SQLite on Linux

在 多個 Linux 發行版提供了方便的命令來擷取 SQLite:

 
1 /* For Debian or Ubuntu /*
2 $ sudo apt-get install sqlite3 sqlite3-dev
3   
4 /* For RedHat, CentOS, or Fedora/*
5 $ yum install SQLite3 sqlite3-dev

SQLite on Mac OS X

如果你正在使用 Mac OS 雪豹或者更新版本的系統,那麼系統上已經裝有 SQLite 了。

3. 建立首個 SQLite 資料庫

現在你已經安裝了 SQLite 資料庫,接下來我們建立首個資料庫。在命令列視窗中輸入如下命令來建立一個名為  test.db 的資料庫。

 
1 sqlite3 test.db

建立表:

 
1 sqlite> create table mytable(id integer primary key, value text);
2   
3 2 columns were created.

該表包含一個名為 id 的主鍵欄位和一個名為 value 的文字欄位。

注意: 最少必須為建立的資料庫建立一個表或者視圖,這麼才能將資料庫儲存到磁碟中,否則資料庫不會被建立。

接下來往表裡中寫入一些資料:

 
1 sqlite> insert into mytable(id, value) values(1, 'Micheal');
2 sqlite> insert into mytable(id, value) values(2, 'Jenny');
3 sqlite> insert into mytable(value) values('Francis');
4 sqlite> insert into mytable(value) values('Kerk');

查詢資料:  
1 sqlite> select * from test;
2 1|Micheal
3 2|Jenny
4 3|Francis
5 4|Kerk
設定格式化查詢結果:  
1 sqlite> .mode column;
2 sqlite> .header on;
3 sqlite> select * from test;
4 id          value
5 ----------- -------------
6 1           Micheal
7 2           Jenny
8 3           Francis
9 4           Kerk

.mode column 將設定為列顯示模式,.header 將顯示列名。

修改表結構,增加列:

 
1 sqlite> alter table mytable add column email text not null '' collate nocase;;

建立視圖:  
1 sqlite> create view nameview as select * from mytable;
建立索引:  
1 sqlite> create index test_idx on mytable(value);

4. 一些有用的 SQLite 命令

顯示表結構:
sqlite> .schema [table]

擷取所有表和視圖: sqlite > .tables

擷取指定表的索引列表: sqlite > .indices [table ]
匯出資料庫到 SQL 檔案: sqlite > .output [filename ]
sqlite > .dump
sqlite > .output stdout
從 SQL 檔案匯入資料庫: sqlite > .read [filename ]
格式化輸出資料到 CSV 格式: sqlite >.output [filename.csv ]
sqlite >.separator ,
sqlite > select * from test;
sqlite >.output stdout
從 CSV 檔案匯入資料到表中: sqlite >create table newtable ( id integer primary key, value text );
sqlite >.import [filename.csv ] newtable

備份資料庫:
/* usage: sqlite3 [database] .dump > [filename] */
sqlite3 mytable.db .dump > backup.sql

恢複資料庫: /* usage: sqlite3 [database ] < [filename ] */
sqlite3 mytable.db < backup.sql

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.