SQLite數(shù)據(jù)庫(kù)安裝及基本操作指南
1. 介紹
SQLite 是一個(gè)開(kāi)源的嵌入式關(guān)系數(shù)據(jù)庫(kù),實(shí)現(xiàn)自包容、零配置、支持事務(wù)的SQL數(shù)據(jù)庫(kù)引擎。 其特點(diǎn)是高度便攜、使用方便、結(jié)構(gòu)緊湊、高效、可靠。 與其他數(shù)據(jù)庫(kù)管理系統(tǒng)不同,SQLite 的安裝和運(yùn)行非常簡(jiǎn)單,在大多數(shù)情況下 - 只要確保SQLite的二進(jìn)制文件存在即可開(kāi)始創(chuàng)建、連接和使用數(shù)據(jù)庫(kù)。如果您正在尋找一個(gè)嵌入式數(shù)據(jù)庫(kù)項(xiàng)目或解決方案,SQLite是絕對(duì)值得考慮。
2. 安裝
SQLite on Windows
1)進(jìn)入 SQL 下載頁(yè)面:http://www.sqlite.org/download.html
2)下載 Windows 下的預(yù)編譯二進(jìn)制文件包:
sqlite-shell-win32-x86-<build#>.zip
sqlite-dll-win32-x86-<build#>.zip
注意: <build#> 是 sqlite 的編譯版本號(hào)
將 zip 文件解壓到你的磁盤,并將解壓后的目錄添加到系統(tǒng)的 PATH 變量中,以方便在命令行中執(zhí)行 sqlite 命令。
可選: 如果你計(jì)劃發(fā)布基于 sqlite 數(shù)據(jù)庫(kù)的應(yīng)用程序,你還需要下載源碼以便編譯和利用其 API
sqlite-amalgamation-<build#>.zip
SQLite on Linux
在 多個(gè) Linux 發(fā)行版提供了方便的命令來(lái)獲取 SQLite:
/* For Debian or Ubuntu /* $ sudo apt-get install sqlite3 sqlite3-dev /* For RedHat, CentOS, or Fedora/* $ yum install SQLite3 sqlite3-dev SQLite on Mac OS X
如果你正在使用 Mac OS 雪豹或者更新版本的系統(tǒng),那么系統(tǒng)上已經(jīng)裝有 SQLite 了。
3. 創(chuàng)建首個(gè) SQLite 數(shù)據(jù)庫(kù)
現(xiàn)在你已經(jīng)安裝了 SQLite 數(shù)據(jù)庫(kù),接下來(lái)我們創(chuàng)建首個(gè)數(shù)據(jù)庫(kù)。在命令行窗口中輸入如下命令來(lái)創(chuàng)建一個(gè)名為 test.db 的數(shù)據(jù)庫(kù)。
sqlite3 test.db
創(chuàng)建表:
sqlite> create table mytable(id integer primary key, value text); 2 columns were created.
該表包含一個(gè)名為 id 的主鍵字段和一個(gè)名為 value 的文本字段。
注意: 最少必須為新建的數(shù)據(jù)庫(kù)創(chuàng)建一個(gè)表或者視圖,這么才能將數(shù)據(jù)庫(kù)保存到磁盤中,否則數(shù)據(jù)庫(kù)不會(huì)被創(chuàng)建。
接下來(lái)往表里中寫入一些數(shù)據(jù):
sqlite> insert into mytable(id, value) values(1, 'Micheal'); sqlite> insert into mytable(id, value) values(2, 'Jenny'); sqlite> insert into mytable(value) values('Francis'); sqlite> insert into mytable(value) values('Kerk');
查詢數(shù)據(jù):
sqlite> select * from test; 1|Micheal 2|Jenny 3|Francis 4|Kerk
設(shè)置格式化查詢結(jié)果:
sqlite> .mode column; sqlite> .header on; sqlite> select * from test; id value ----------- ------------- 1 Micheal 2 Jenny 3 Francis 4 Kerk
.mode column 將設(shè)置為列顯示模式,.header 將顯示列名。
修改表結(jié)構(gòu),增加列:
sqlite> alter table mytable add column email text not null '' collate nocase;;
創(chuàng)建視圖:
sqlite> create view nameview as select * from mytable;
創(chuàng)建索引:
sqlite> create index test_idx on mytable(value);
4. 一些有用的 SQLite 命令
顯示表結(jié)構(gòu):
sqlite> .schema [table]
獲取所有表和視圖:
sqlite > .tables
獲取指定表的索引列表:
sqlite > .indices [table ]
導(dǎo)出數(shù)據(jù)庫(kù)到 SQL 文件:
sqlite > .output [filename ] sqlite > .dump sqlite > .output stdout
從 SQL 文件導(dǎo)入數(shù)據(jù)庫(kù):
sqlite > .read [filename ]
格式化輸出數(shù)據(jù)到 CSV 格式:
sqlite >.output [filename.csv ] sqlite >.separator , sqlite > select * from test; sqlite >.output stdout
從 CSV 文件導(dǎo)入數(shù)據(jù)到表中:
sqlite >create table newtable ( id integer primary key, value text ); sqlite >.import [filename.csv ] newtable
備份數(shù)據(jù)庫(kù):
/* usage: sqlite3 [database] .dump > [filename] */ sqlite3 mytable.db .dump > backup.sql
恢復(fù)數(shù)據(jù)庫(kù):
/* usage: sqlite3 [database ] < [filename ] */ sqlite3 mytable.db < backup.sql
- ASP.NET(C#)中操作SQLite數(shù)據(jù)庫(kù)實(shí)例
- C#中使用SQLite數(shù)據(jù)庫(kù)的方法介紹
- android創(chuàng)建數(shù)據(jù)庫(kù)(SQLite)保存圖片示例
- python操作數(shù)據(jù)庫(kù)之sqlite3打開(kāi)數(shù)據(jù)庫(kù)、刪除、修改示例
- php讀取sqlite數(shù)據(jù)庫(kù)入門實(shí)例代碼
- Python3實(shí)現(xiàn)連接SQLite數(shù)據(jù)庫(kù)的方法
- C#基于SQLiteHelper類似SqlHelper類實(shí)現(xiàn)存取Sqlite數(shù)據(jù)庫(kù)的方法
- 分享php代碼將360瀏覽器導(dǎo)出的favdb的sqlite數(shù)據(jù)庫(kù)文件轉(zhuǎn)換為html
相關(guān)文章
Sqlite數(shù)據(jù)庫(kù)里插入數(shù)據(jù)的條數(shù)上限是500
sqlite每次只能插入的數(shù)據(jù)不能超過(guò)500條數(shù)據(jù),大家在使用的時(shí)候需要注意一下。2015-04-04SQLite 實(shí)現(xiàn)if not exist 類似功能的操作
這篇文章主要介紹了SQLite 實(shí)現(xiàn)if not exist 類似功能的操作,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2021-01-01SQLite教程(一):SQLite數(shù)據(jù)庫(kù)介紹
這篇文章主要介紹了SQLite教程(一):SQLite數(shù)據(jù)庫(kù)介紹,本文講解了什么是SQLite、SQLite的主要優(yōu)點(diǎn)、和RDBMS相比SQLite的一些劣勢(shì)、個(gè)性化特征等內(nèi)容,需要的朋友可以參考下2015-05-05SQLite教程(十):內(nèi)存數(shù)據(jù)庫(kù)和臨時(shí)數(shù)據(jù)庫(kù)
這篇文章主要介紹了SQLite教程(十):內(nèi)存數(shù)據(jù)庫(kù)和臨時(shí)數(shù)據(jù)庫(kù),本文講解了它們的創(chuàng)建方法和相關(guān)知識(shí),需要的朋友可以參考下2015-05-05基于sqlite特殊字符轉(zhuǎn)義的實(shí)現(xiàn)方法
本篇文章是對(duì)sqlite特殊字符轉(zhuǎn)義的實(shí)現(xiàn)方法進(jìn)行了詳細(xì)的分析介紹,需要的朋友參考下2013-05-05Win11下基于VS2022編譯SQLite3源碼的實(shí)現(xiàn)步驟
本文主要介紹了Win11下基于VS2022編譯SQLite3源碼的實(shí)現(xiàn)步驟,文中通過(guò)圖文介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2023-09-09