在C語言中得到sqlite資料庫中表的內容
阿新 • • 發佈:2019-02-05
作者: zieckey ( [email protected] )
All Rights Reserved
現在我們來寫個C/C++程式,呼叫 sqlite 的 API 介面函式查詢e資料庫中表的內容。
下面是一個C程式的例子,顯示怎麼使用 sqlite 的 C/C++ 介面. 這個函式呼叫sqlite3_open() 在 22 行開啟資料庫, sqlite3_get_table 在 40 行執行 SQL 命令, 並且sqlite3_close() 在 47 行關閉資料庫連線。
程式碼:
// name: query_test_1.c
// This file is used to test C/C++ API for sqlite
// Author : zieckey
// 2006/06/28
#include <stdio.h>
#include <sqlite3.h>
int main( void )
{
sqlite3 *db=NULL;
char *zErrMsg = 0;
int nrow = 0, ncolumn = 0;
char **azResult; //二維陣列存放結果
int rc;
rc = sqlite3_open("zieckey.db", &db); //開啟指定的資料庫檔案,如果不存在將建立一個同名的資料庫檔案
if( rc ){
fprintf(stderr, "Can't open database: %s/n", sqlite3_errmsg(db));
sqlite3_close(db);
exit(1);
}
else printf("open zieckey.db successfully!/n");
//也可以參考 sprintf 的用法
//char *sql = "SELECT SensorParameter FROM SensorData WHERE SensorID = 1";
char *sql = "SELECT * FROM SensorData ";
/*
int sqlite3_get_table(sqlite3*, const char *sql,char***resultp,int *nrow, int *ncolumn,char **errmsg);
result中是以陣列的形式存放你所查詢的資料,首先是表名,再是資料。nrow ,ncolumn分別為 查詢語句返回的結果集的行數,列數,沒有查到返回0
*/
sqlite3_get_table(db,sql,&azResult,&nrow,&ncolumn,&zErrMsg);
int i=0;
for(i=0;i<100;i++)
printf("%s/n",azResult[i]);
printf("row:%d column=%d /n",nrow,ncolumn);
printf("zErrMsg = %s /n", zErrMsg);
sqlite3_close(db); //關閉資料庫
return 0;
}
編譯:# gcc query_test_1.c -o db.out
也許會碰到類似這樣的問題:
query_test_1.c:2:21: sqlite3.h: No such file or directory
query_test_1.c: In function `main':
query_test_1.c:6: error: `sqlite3' undeclared (first use in this function)
query_test_1.c:6: error: (Each undeclared identifier is reported only once
query_test_1.c:6: error: for each function it appears in.)
query_test_1.c:6: error: `db' undeclared (first use in this function)
這是個沒有找到庫檔案的問題。
由於用到了使用者自己的庫檔案,所用應該指明所用到的庫,我們可以這樣編譯:
# gcc query_test_1.c -o db.out -lsqlite3
我用用 -lsqlite3 選項就可以了(前面我們生成的庫檔案是 libsqlite3.so.0.8.6 等,
去掉前面的lib和後面的版本標誌,就剩下 sqlite3 了所以是 -lsqlite3 )。
如果我們在編譯安裝的時候,選擇了安裝路徑,例如這樣的話:
.......
# ../sqlite/configure --prefix=/usr/local/arm-linux/sqlite-ix86-linux
.......
這樣編譯安裝時,sqlite的庫檔案將會生成在 /usr/local/arm-linux/sqlite-ix86-linux/lib 目錄下
這時編譯還要指定庫檔案路徑,因為系統預設的路徑沒有包含 /usr/local/arm-linux/sqlite-ix86-linux/lib
# gcc query_test_1.c -lsqlite3 -L/usr/local/arm-linux/sqlite-ix86-linux/lib
如果還不行的話,可能還需要指定標頭檔案 sqlite3.h 的路徑,如下:
# gcc query_test_1.c -lsqlite3 -L/usr/local/arm-linux/sqlite-ix86-linux/lib -I/usr/local/arm-linux/sqlite-ix86-linux/include
這樣編譯應該就可以了 ,執行:
# ./db.out
./db.out: error while loading shared libraries: libsqlite3.so.0: cannot open shared object file:
執行是也許會出現類似上面的錯誤。
這個問題是由於執行沿著系統預設路徑沒有找到庫檔案引起的。這時需要指定 libsqlite3.so.0 庫檔案的路徑。
在shell下輸入:
export LD_LIBRARY_PATH=/usr/local/arm-linux/sqlite-ix86-linux/lib:$LD_LIBRARY_PATH
再執行
# ./db.out
open colliery.db successfully!
ID
SensorID
SiteNum
Time
SensorParameter
1
1
1
200605011206
18.9
2
1
1
200605011306
16.4
3
1
1
200605011406
15.3
4
1
1
200605011506
15.5
5
1
1
200605011606
15.8
6
1
1
200605011706
12.5
7
1
1
200605011806
5.8
8
8
5
200605011206
7
9
8
5
200605011306
6
10
8
5
200605011406
5
11
8
5
200605011506
6
12
8
5
200605011606
5
13
8
5
200605011706
6
14
8
5
200605011806
7
17
5
4
200605010606
21.6
18
5
4
200605010706
21.5
19
5
4
200605010806
21.2
20
5
4
200605010906
21.3
21
5
4
200605011006
21.6
row:51 column=5
zErrMsg = (null)
在shell命令列下用同樣的命令,可以看到一下內容。這裡可以比較一下,不難得到一些有價值的資訊。
sqlite> SELECT * FROM SensorData ;
1|1|1|200605011206|18.9
2|1|1|200605011306|16.4
3|1|1|200605011406|15.3
4|1|1|200605011506|15.5
5|1|1|200605011606|15.8
6|1|1|200605011706|12.5
7|1|1|200605011806|5.8
8|8|5|200605011206|7
9|8|5|200605011306|6
10|8|5|200605011406|5
11|8|5|200605011506|6
12|8|5|200605011606|5
13|8|5|200605011706|6
14|8|5|200605011806|7
17|5|4|200605010606|21.6
18|5|4|200605010706|21.5
19|5|4|200605010806|21.2
20|5|4|200605010906|21.3
21|5|4|200605011006|21.6
22|5|4|200605011106|21.5
23|5|4|200605011206|21.2
27|3|2|200603020720|0.28
28|3|2|200603020820|0.65
29|3|2|200603020920|0.32
30|3|2|200603021020|0.28
31|3|2|200603021120|0.65
32|3|2|200603021220|0.32
33|3|2|200603021320|0.23
54|2|1|200605011206|18.9
55|2|1|200605011306|16.4
56|2|1|200605011406|15.3
57|2|1|200605011506|15.5
58|2|1|200605011606|15.8
59|2|1|200605011706|12.5
60|2|1|200605011806|13.6
61|2|1|200605011906|15.5
62|4|3|200605011206|0.3
63|4|3|200605011206|0.4
64|4|3|200605011206|0.2
65|4|3|200605011206|0.2
66|4|3|200605011206|0.3
67|4|3|200605011206|0.3
68|4|3|200605011206|0.2
69|4|3|200605011206|0.4
95|6|6|200603022320|14.23
96|6|6|200603022420|16.23
97|6|6|200603022020|23.23
98|6|6|200603022120|21.23
99|6|6|200603022220|23.23
100|6|6|200603022320|22.23
101|6|6|200603022420|22.23
另外這裡用到zieckey.db資料庫內容:
BEGIN TRANSACTION;
CREATE TABLE SensorData(
ID INTEGER PRIMARY KEY,
SensorID INTEGER,
SiteNum INTEGER,
Time VARCHAR(12),
SensorParameter REAL
);
INSERT INTO "SensorData" VALUES(1, 1, 1, '200605011206', 18.9);
INSERT INTO "SensorData" VALUES(2, 1, 1, '200605011306', 16.4);
INSERT INTO "SensorData" VALUES(3, 1, 1, '200605011406', 15.3);
INSERT INTO "SensorData" VALUES(4, 1, 1, '200605011506', 15.5);
INSERT INTO "SensorData" VALUES(5, 1, 1, '200605011606', 15.8);
INSERT INTO "SensorData" VALUES(6, 1, 1, '200605011706', 12.5);
INSERT INTO "SensorData" VALUES(7, 1, 1, '200605011806', 5.8);
INSERT INTO "SensorData" VALUES(8, 8, 5, '200605011206', 7);
INSERT INTO "SensorData" VALUES(9, 8, 5, '200605011306', 6);
INSERT INTO "SensorData" VALUES(10, 8, 5, '200605011406', 5);
INSERT INTO "SensorData" VALUES(11, 8, 5, '200605011506', 6);
INSERT INTO "SensorData" VALUES(12, 8, 5, '200605011606', 5);
INSERT INTO "SensorData" VALUES(13, 8, 5, '200605011706', 6);
INSERT INTO "SensorData" VALUES(14, 8, 5, '200605011806', 7);
INSERT INTO "SensorData" VALUES(17, 5, 4, '200605010606', 21.6);
INSERT INTO "SensorData" VALUES(18, 5, 4, '200605010706', 21.5);
INSERT INTO "SensorData" VALUES(19, 5, 4, '200605010806', 21.2);
INSERT INTO "SensorData" VALUES(20, 5, 4, '200605010906', 21.3);
INSERT INTO "SensorData" VALUES(21, 5, 4, '200605011006', 21.6);
INSERT INTO "SensorData" VALUES(22, 5, 4, '200605011106', 21.5);
INSERT INTO "SensorData" VALUES(23, 5, 4, '200605011206', 21.2);
INSERT INTO "SensorData" VALUES(27, 3, 2, '200603020720', 0.28);
INSERT INTO "SensorData" VALUES(28, 3, 2, '200603020820', 0.65);
INSERT INTO "SensorData" VALUES(29, 3, 2, '200603020920', 0.32);
INSERT INTO "SensorData" VALUES(30, 3, 2, '200603021020', 0.28);
INSERT INTO "SensorData" VALUES(31, 3, 2, '200603021120', 0.65);
INSERT INTO "SensorData" VALUES(32, 3, 2, '200603021220', 0.32);
INSERT INTO "SensorData" VALUES(33, 3, 2, '200603021320', 0.23);
INSERT INTO "SensorData" VALUES(54, 2, 1, '200605011206', 18.9);
INSERT INTO "SensorData" VALUES(55, 2, 1, '200605011306', 16.4);
INSERT INTO "SensorData" VALUES(56, 2, 1, '200605011406', 15.3);
INSERT INTO "SensorData" VALUES(57, 2, 1, '200605011506', 15.5);
INSERT INTO "SensorData" VALUES(58, 2, 1, '200605011606', 15.8);
INSERT INTO "SensorData" VALUES(59, 2, 1, '200605011706', 12.5);
INSERT INTO "SensorData" VALUES(60, 2, 1, '200605011806', 13.6);
INSERT INTO "SensorData" VALUES(61, 2, 1, '200605011906', 15.5);
INSERT INTO "SensorData" VALUES(62, 4, 3, '200605011206', 0.3);
INSERT INTO "SensorData" VALUES(63, 4, 3, '200605011206', 0.4);
INSERT INTO "SensorData" VALUES(64, 4, 3, '200605011206', 0.2);
INSERT INTO "SensorData" VALUES(65, 4, 3, '200605011206', 0.2);
INSERT INTO "SensorData" VALUES(66, 4, 3, '200605011206', 0.3);
INSERT INTO "SensorData" VALUES(67, 4, 3, '200605011206', 0.3);
INSERT INTO "SensorData" VALUES(68, 4, 3, '200605011206', 0.2);
INSERT INTO "SensorData" VALUES(69, 4, 3, '200605011206', 0.4);
INSERT INTO "SensorData" VALUES(95, 6, 6, '200603022320', 14.23);
INSERT INTO "SensorData" VALUES(96, 6, 6, '200603022420', 16.23);
INSERT INTO "SensorData" VALUES(97, 6, 6, '200603022020', 23.23);
INSERT INTO "SensorData" VALUES(98, 6, 6, '200603022120', 21.23);
INSERT INTO "SensorData" VALUES(99, 6, 6, '200603022220', 23.23);
INSERT INTO "SensorData" VALUES(100, 6, 6, '200603022320', 22.23);
INSERT INTO "SensorData" VALUES(101, 6, 6, '200603022420', 22.23);
COMMIT;
另外感謝武漢大學 zHaocHen 朋友所給的幫助。