環境
Linux 操作系統: Fedora 16
內核:3.1
sqlite 版本:3.7.13
目錄
1.編譯與安裝
1.1下載sqlite源碼
1.2編譯與安裝
2.sqlite數據庫管理
3.sqlite數據庫編程
參考
1.編譯與安裝
1.1下載sqlite源碼
官網下載地址
http://www.sqlite.org/download.html
選擇下載項:
Source Code
sqlite-autoconf-3071300.tar.gz
(1.76 MiB)
下載得到文件
sqlite-autoconf-3071300.tar.gz
1.2編譯與安裝
解壓sqlite壓縮文件
tar –zvxf sqlite-autoconf-3071300.tar.gz
得到文件sqlite-autoconf-3071300
下面的一些操作參考sqlite源文件中的INSTALL文件,這是一份好的安裝說明。
進入sqlite-autoconf-3071300目錄
[root@localhost ~]# cd /home/sqlite-autoconf-3071300/
配置
[root@localhost sqlite-autoconf-3071300]# ./configure
編譯
[root@localhost sqlite-autoconf-3071300]# make
安裝
[root@localhost 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安裝在系統庫文件標準目錄/usr/lib下
可執行文件sqlite3安裝在 /usr/local/bin目錄下以及系統可執行標準目錄/usr/bin下
幫助文檔man安裝在/usr/local/share目錄下
2.數據庫管理
創建數據庫文件
[root@localhost /]# 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
[root@localhost mysqlite_databasefile]#
特殊用法
命令行下直接使用
[root@localhost mysqlite_databasefile]# sqlite3 mydbtest "select * from mytable;"
lian|19
zheng|20
Qing|23
[root@localhost 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>
………………
詳細代碼見程序源文件http://download.csdn.net/detail/lanyang123456/4384399。
3.2編譯源代碼
[root@localhost 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搜索庫
[root@localhost mysqlite_databasefile]# gcc -o mysqlite3 mysqlite.c -lsqlite3
3.3執行程序
[root@localhost 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
…………
[root@localhost mysqlite_databasefile]#
SQL刪除語句出現語法錯誤,並由輸出可以得到其錯誤的原因。經檢查發現 DELETE後面多了空格,修改後,執行結果如下
[root@localhost 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
…………
[root@localhost mysqlite_databasefile]#
成功刪除第4條記錄
詳細執行結果見執行結果文件http://download.csdn.net/detail/lanyang123456/4384399。
參考
SQLite 官網
SQLite中文網
SQLite3使用教學 數據庫使用說明
http://www.sqlite.com.cn/MySqlite/4/378.Html
嵌入式數據庫SQLite的一份教程
http://www.sqlite.com.cn/MySqlite/3/380.Html
sqlite3編程筆記 .
http://blog.csdn.net/wl_haanel/article/details/6231417
SQLite3 API編程手冊
http://www.cnblogs.com/hnrainll/archive/2011/09/08/2170506.html
幾篇關於嵌入式數據庫的簡介,包括SQLite Berkeley DB
http://blog.chinaunix.net/uid/9563036/frmd/23812.html
嵌入式數據庫SQLite移植到S3C2410的方法 .
http://blog.csdn.net/liuzhidong123/article/details/6827379
sqlite嵌入式數據庫在arm-linux下的編譯全攻略
http://blog.chinaunix.net/uid-9563036-id-352307.html
嵌入式數據庫sqlite在Motorola Coldfire + uclinux下的移植
http://www.sqlite.com.cn/MySqlite/6/379.Html
SQLITE3使用總結 windows下編程接口說明
http://hi.baidu.com/llhg/blog/item/0c3c0da89d83d6b3cb130cdf.html
sqlite3使用簡介 Windows下編程接口說明
http://www.cnblogs.com/kfqcome/archive/2011/06/27/2136999.html
Sqlite快速上手使用指南 Windows下SQLite圖形界面使用
http://www.cnblogs.com/yjmyzz/archive/2010/02/18/1669210.html
Linux configure關於交叉編譯的參數設置
http://tech.ccidnet.com/art/2583/20080307/1383653_1.html
轉載請註明出處。