Linux下SQLite資料庫安裝操作與程式設計
//----------------------------------------------------
//AUTHOR: lanyang123456
//DATE: 2012-6-20
//----------------------------------------------------
環境
Linux 作業系統: Fedora 16
核心:3.1
sqlite 版本:3.7.13
目錄
1.編譯與安裝
1.1下載sqlite原始碼
1.2編譯與安裝
2.sqlite資料庫管理
3.sqlite資料庫程式設計
參考
1.編譯與安裝
1.1下載sqlite原始碼
官網下載地址
選擇下載項:
Source Code
下載得到檔案
sqlite-autoconf-3071300.tar.gz
1.2編譯與安裝
解壓sqlite壓縮檔案
tar –zvxf sqlite-autoconf-3071300.tar.gz
得到檔案sqlite-autoconf-3071300
下面的一些操作參考sqlite原始檔中的INSTALL檔案,這是一份好的安裝說明。
進入sqlite-autoconf-3071300目錄
[[email protected] ~]# cd /home/sqlite-autoconf-3071300/
配置
[[email protected] sqlite-autoconf-3071300]# ./configure
編譯
[[email protected] sqlite-autoconf-3071300]# make
安裝
[[email protected] sqlite-autoconf-3071300]# make install
預設安裝路徑為/usr/local/及系統標準目錄
標頭檔案 sqlite3.h sqlite3ext.h安裝在 /usr/local/include下
以及標頭檔案標準目錄 /usr/include下
庫檔案libsqlite3.a libsqlite3.so.0.8.6 libsqlite3.so.0 libsqlite3.so
安裝在/usr/local/lib目錄下
並且共享庫檔案libsqlite3.so libsqlite3.so.0 libsqlite3.so.0.8.6
可執行檔案sqlite3安裝在 /usr/local/bin目錄下以及系統可執行標準目錄/usr/bin下
幫助文件man安裝在/usr/local/share目錄下
2.資料庫管理
建立資料庫檔案
[[email protected] /]# sqlite3 mydbtest
SQLite version 3.7.13 2012-06-11 02:05:22
Enter ".help" for instructions
Enter SQL statements terminated with a ";"
sqlite>
出現sqlite>提示符
檢視目前的資料庫。注意資料庫操作命令以 ”.”開頭。
sqlite> .database
seq name file
--- --------------- ----------------------------------------------------------
0 main //mydbtest
sqlite>
列出當前使用的資料庫mydbtest
資料庫mydbtest檔案建立在執行命令# sqlite3 mydbtest時,命令列所在的目錄。
建立表
sqlite> create table mytable(name varchar(80),num smallint);
列出表
檢視建立了哪些表
sqlite> .tables
mytable
sqlite>
查詢某個表
sqlite> .tables my
sqlite> .tables mytable
mytable
sqlite>
插入記錄
sqlite> insert into mytable values('lian',19);
sqlite> insert into mytable values('zheng',20);
sqlite> insert into mytable values('Qing',23);
查詢
sqlite> select * from mytable;
lian|19
zheng|20
Qing|23
sqlite>
模式查看錶結構
sqlite> .schema
CREATE TABLE mytable(name varchar(80),num smallint);
sqlite>
從檔案向表中匯入資料
建立檔案data.txt,內容如下
LTian Hong|19
Eng Lish|20
Gao Yuan|23
Wei Da|26
其中“|”是分隔符,分隔符左右不要有空格
匯入資料前查詢
sqlite> select * from mytable;
lian|19
zheng|20
Qing|23
sqlite> .import data.txt mytable
匯入資料後查詢
sqlite> select * from mytable;
lian|19
zheng|20
Qing|23
LTian Hong|19
Eng Lish|20
Gao Yuan|23
Wei Da|26
sqlite>
生成形成資料庫表的SQL指令碼
sqlite> .dump mytable
PRAGMA foreign_keys=OFF;
BEGIN TRANSACTION;
CREATE TABLE mytable(name varchar(80),num smallint);
INSERT INTO "mytable" VALUES('lian',19);
INSERT INTO "mytable" VALUES('zheng',20);
INSERT INTO "mytable" VALUES('Qing',23);
INSERT INTO "mytable" VALUES('LTian Hong',19);
INSERT INTO "mytable" VALUES('Eng Lish',20);
INSERT INTO "mytable" VALUES('Gao Yuan',23);
INSERT INTO "mytable" VALUES('Wei Da',26);
COMMIT;
sqlite>
資料匯出
將輸出定向到檔案
sqlite> .output create.sql
sqlite> .dump mytable
將資料庫表生成的SQL指令碼輸出到create.sql檔案
將輸出恢復到標準輸出
sqlite> .output stdout
sqlite>
列印SQLite環境變數到設定
sqlite> .show
echo: off
explain: off
headers: off
mode: list
nullvalue: ""
output: stdout
separator: "|"
stats: off
width:
sqlite>
退出資料庫,使用.quit或.q
sqlite> .quit
[[email protected] mysqlite_databasefile]#
特殊用法
命令列下直接使用
[[email protected] mysqlite_databasefile]# sqlite3 mydbtest "select * from mytable;"
lian|19
zheng|20
Qing|23
[[email protected] mysqlite_databasefile]#
3.資料庫程式設計
系統標頭檔案標準目錄/usr/include下有標頭檔案sqlite3.h sqlite3ext.h
所以程式設計時可直接使用 #include <sqlite3.h>包含標頭檔案。
若系統標頭檔案標準目錄下沒有需要的標頭檔案,則需要將標頭檔案與程式原始檔放在同一目錄下,並使用#include “sqlite3.h”,或者其它方法。
3.1編寫原始碼
#include <stdio.h>
#include <stdlib.h> //exit等函式的宣告
#include <sqlite3.h>
………………
3.2編譯原始碼
[[email protected] mysqlite_databasefile]# gcc -o mysqlite3 mysqlite.c
/tmp/ccuW3QVl.o: In function `inquire_Usecb':
mysqlite.c:(.text+0xa7): undefined reference to `sqlite3_exec'
/tmp/ccuW3QVl.o: In function `inquire_nocb':
mysqlite.c:(.text+0x11d): undefined reference to `sqlite3_get_table'
mysqlite.c:(.text+0x1ab): undefined reference to `sqlite3_free_table'
/tmp/ccuW3QVl.o: In function `createnTable':
mysqlite.c:(.text+0x1ef): undefined reference to `sqlite3_exec'
/tmp/ccuW3QVl.o: In function `insertRecord':
mysqlite.c:(.text+0x23f): undefined reference to `sqlite3_exec'
mysqlite.c:(.text+0x287): undefined reference to `sqlite3_exec'
mysqlite.c:(.text+0x2cf): undefined reference to `sqlite3_exec'
/tmp/ccuW3QVl.o: In function `deleteRecord':
mysqlite.c:(.text+0x334): undefined reference to `sqlite3_exec'
mysqlite.c:(.text+0x381): undefined reference to `sqlite3_get_table'
mysqlite.c:(.text+0x40f): undefined reference to `sqlite3_free_table'
/tmp/ccuW3QVl.o: In function `main':
mysqlite.c:(.text+0x436): undefined reference to `sqlite3_open'
mysqlite.c:(.text+0x44d): undefined reference to `sqlite3_errmsg'
mysqlite.c:(.text+0x474): undefined reference to `sqlite3_close'
mysqlite.c:(.text+0x4e2): undefined reference to `sqlite3_close'
collect2: ld返回 1
編譯時指定庫檔名sqlite3,系統會在庫檔案預設目錄/lib或/usr/lib搜尋庫
[[email protected] mysqlite_databasefile]# gcc -o mysqlite3 mysqlite.c -lsqlite3
3.3執行程式
[[email protected] mysqlite_databasefile]# ./mysqlite3
You have opened a sqlite3 database successfully!
row:4 column = 5
The result of querying is :
azResult[0] = ID
azResult[1] = SensorID
azResult[2] = SiteNum
azResult[3] = Time
azResult[4] = SensorParameter
azResult[5] = 1
azResult[6] = 101
azResult[7] = 261
azResult[8] = 20100314
azResult[9] = 18.9
azResult[10] = 2
……………………
Total column is 5
欄位名: ID---->欄位值:1
欄位名: SensorID---->欄位值:101
欄位名: SiteNum---->欄位值:261
欄位名: Time---->欄位值:20100314
欄位名: SensorParameter---->欄位值:18.9
==========================
………………
Total column is 5
欄位名: ID---->欄位值:3
欄位名: SensorID---->欄位值:667
欄位名: SiteNum---->欄位值:290
欄位名: Time---->欄位值:20110315
欄位名: SensorParameter---->欄位值:27.0
==========================
Total column is 5
欄位名: ID---->欄位值:4
欄位名: SensorID---->欄位值:865
欄位名: SiteNum---->欄位值:300
欄位名: Time---->欄位值:20120616
欄位名: SensorParameter---->欄位值:323.0
==========================
operate failed: near "DELETE ": syntax error
row:4 column:5
After deleting,the result of querying is :
azResult[0] = ID
azResult[1] = SensorID
azResult[2] = SiteNum
azResult[3] = Time
azResult[4] = SensorParameter
azResult[5] = 1
azResult[6] = 101
azResult[7] = 261
azResult[8] = 20100314
…………
[[email protected] mysqlite_databasefile]#
SQL刪除語句出現語法錯誤,並由輸出可以得到其錯誤的原因。經檢查發現 DELETE後面多了空格,修改後,執行結果如下
[[email protected] mysqlite_databasefile]# ./mysqlite3
You have opened a sqlite3 database successfully!
row:4 column = 5
The result of querying is :
azResult[0] = ID
azResult[1] = SensorID
azResult[2] = SiteNum
azResult[3] = Time
……………………
==========================
Total column is 5
欄位名: ID---->欄位值:4
欄位名: SensorID---->欄位值:865
欄位名: SiteNum---->欄位值:300
欄位名: Time---->欄位值:20120616
欄位名: SensorParameter---->欄位值:323.0
==========================
row:3 column:5
After deleting,the result of querying is :
azResult[0] = ID
azResult[1] = SensorID
…………
[[email protected] mysqlite_databasefile]#
成功刪除第4條記錄
參考
SQLite 官網
SQLite中文網
SQLite3使用教學資料庫使用說明
嵌入式資料庫SQLite的一份教程
sqlite3程式設計筆記 .
SQLite3 API程式設計手冊
幾篇關於嵌入式資料庫的簡介,包括SQLite Berkeley DB
嵌入式資料庫SQLite移植到S3C2410的方法 .
sqlite嵌入式資料庫在arm-linux下的編譯全攻略
嵌入式資料庫sqlite在Motorola Coldfire + uclinux下的移植
SQLITE3使用總結 windows下程式設計介面說明
sqlite3使用簡介 Windows下程式設計介面說明
Sqlite快速上手使用指南 Windows下SQLite圖形介面使用
Linux configure關於交叉編譯的引數設定
轉載請註明出處。