作者: zieckey ( zieckey@yahoo.com.cn )
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: No such file or directory
运行是也许会出现类似上面的错误。
这个问题是由于运行沿着系统默认路径没有找到库文件引起的。这时需要指定 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 朋友所给的帮助。
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: No such file or directory
运行是也许会出现类似上面的错误。
这个问题是由于运行沿着系统默认路径没有找到库文件引起的。这时需要指定 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 朋友所给的帮助。