SQLite 資料庫入門基礎教程

來源:互聯網
上載者:User

SQLite 資料庫入門基礎教程

1. 介紹

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

SQLite3 安裝、基本操作

Ubuntu 12.04下SQLite資料庫簡單應用

Ubuntu 12.04下安裝 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 的編譯版本號碼

將 zip 檔案解壓到你的磁碟,並將解壓後的目錄添加到系統的 PATH 變數中,以方便在命令列中執行 sqlite 命令。

可選: 如果你計劃發布基於 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. /* For RedHat, CentOS, or Fedora/*
  4. $ yum install SQLite3 sqlite3-dev

SQLite on Mac OS X

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

3. 建立首個 SQLite 資料庫

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

  1. sqlite3 test.db

建立表:

  1. sqlite> createtable mytable(id integerprimarykey, value text);
  2. 2 columns were created.

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

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

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

  1. sqlite> insertinto mytable(id, value) values(1, 'Micheal');
  2. sqlite> insertinto mytable(id, value) values(2, 'Jenny');
  3. sqlite> insertinto mytable(value) values('Francis');
  4. sqlite> insertinto 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> altertable mytable addcolumn email text notnull''collate nocase;;

建立視圖:

  1. sqlite> createview nameview asselect * from mytable;

建立索引:

  1. sqlite> createindex test_idx on mytable(value);

4. 一些有用的 SQLite 命令

顯示表結構:

  1. sqlite> .schema [table]

擷取所有表和視圖:

  1. sqlite > .tables

擷取指定表的索引列表:

  1. sqlite > .indices [table ]

匯出資料庫到 SQL 檔案:

  1. sqlite > .output [filename ]
  2. sqlite > .dump
  3. sqlite > .output stdout

從 SQL 檔案匯入資料庫:

  1. sqlite > .read [filename ]

格式化輸出資料到 CSV 格式:

  1. sqlite >.output [filename.csv ]
  2. sqlite >.separator ,
  3. sqlite > select * from test;
  4. sqlite >.output stdout

從 CSV 檔案匯入資料到表中:

  1. sqlite >createtable newtable ( id integerprimarykey, value text );
  2. sqlite >.import [filename.csv ] newtable

備份資料庫:

  1. /* usage: sqlite3 [database] .dump > [filename] */
  2. sqlite3 mytable.db .dump > backup.sql

恢複資料庫:

  1. /* usage: sqlite3 [database ] < [filename ] */
  2. sqlite3 mytable.db < backup.sql

SQLite 的詳細介紹:請點這裡
SQLite 的:請點這裡

本文永久更新連結地址:

相關文章

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.