赞
踩
参考SQLite的编译、安装和使用_whz_zb的博客-CSDN博客,如有侵权,请告知删除。
参考:头文件路径问题 Linux下的头文件搜索路径 - 心哲 - 博客园
参考:进一步学习资源 SQlite - 标签 - likebeta - 博客园
一、源码获取
二、移植步骤
(1)下载和复制数据库sqlite-autoconf-3210000.tar.gz至目录/tmp/mysql。
(2)解压:tar –zxvf sqlite-autoconf-3210000.tar.gz,得到sqlite-autoconf-3210000。
(3)新建某个目录,存放编译后生成的文件。我这里是/tmp/build。
(4)配置:进入sqlite-autoconf-3210000中,使用命令:
(5)编译和安装:make之后再make install安装。
(6)移植:编译好后在build目录中会生成4个文件夹“bin 、include 、lib 、share”。将bin文件夹中的文件拷贝到开发板的/bin中,并将lib中的文件拷贝到开发板的/lib中。
(7)检验是否移植成功:输入sqlite3 test.db。如果成功会打印
三、常用命令
1、新建一个数据库
2、创建表和插入值
(1)可以用CREATE TABLE语法命令。
(2)实例
sqlite> CREATE TABLE SensorData(
...> ID INTEGER PRIMARY KEY,
...> SensorID INTEGER,
...> SiteNum INTEGER,
...> Time VARCHAR(12),
...> SensorParameter REAL
...> );
3、查看所创建的表
(1).schema 是用来查看数据库中所有的表的定义内容。如果后面跟了具体的表名称,则显示该表的内容。
(2)实例
sqlite> .schema
CREATE TABLE SensorData(
ID INTEGER PRIMARY KEY,
SensorID INTEGER,
SiteNum INTEGER,
Time VARCHAR(12),
SensorParameter REAL
);
sqlite>
3、向表中插入具体元素值
sqlite> INSERT INTO "SensorData" VALUES(2, 1, 0, '200605011306', 16.4);
sqlite> INSERT INTO "SensorData" VALUES(3, 1, 0, '200605011406', 15.3);
sqlite> INSERT INTO "SensorData" VALUES(4, 1, 0, '200605011506', 15.5);
4、在线帮助:.help
5、查看当前数据库下的所有表:.table
6、退出sqlite3:.quit
四、linux下用c语言操作数据库
1、调用 sqlite 的 API 接口函数示例
(1)移植好sqlite。
(2)先创建如下文件test.c
- // name: opendbsqlite.c
- // This prog is used to test C/C++ API for sqlite3.It is very simple,ha!
-
- #include <stdio.h>
- #include "sqlite3.h"
-
- int main( int argc, char **argv )
- {
- char *file = "database.sqlite3";
- sqlite3 *pDB = NULL;
- int rc = 0;
-
- sqlite3_initialize( );
- rc = sqlite3_open_v2( file, &pDB, SQLITE_OPEN_READWRITE |
- SQLITE_OPEN_CREATE, NULL );
- if ( rc != SQLITE_OK)
- {
- sqlite3_close_v2( pDB );
-
- }
-
- /* perform database operations */
-
- sqlite3_close_v2( pDB );
-
- }
(2)然后
root@ubuntu:/tmp# gcc test.c -lsqlite3 -L/tmp/build/lib/ -I/tmp/build/include/
root@ubuntu:/tmp# ls
a.out build mysql ssh-KQYQ2kfL2MIM test.c VMwareDnD vmware-root vmware-root-859941367 vmware-xjh wifi_test
root@ubuntu:/tmp# ./a.out
root@ubuntu:/tmp# ls
a.out build database.sqlite3 mysql ssh-KQYQ2kfL2MIM test.c VMwareDnD vmware-root vmware-root-859941367 vmware-xjh wifi_test
root@ubuntu:/tmp#
2、向数据库插入数据
(1)代码
- // name: insert.c
- // This prog is used to test C/C++ API for sqlite3 .It is very simple,ha !
-
- #include <stdio.h>
- #include <stdlib.h>
- #include "sqlite3.h"
- #define _DEBUG_
-
- int main( void )
- {
- sqlite3 *db=NULL;
- char *zErrMsg = 0;
- int rc;
- char *sql;
-
- rc = sqlite3_open("zieckey.db", &db); //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
- if( rc )
- {
- fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
- sqlite3_close(db);
- return 0;
- }
- else
- printf("You have opened a sqlite3 database named zieckey.db successfully!\nCongratulations! Have fun ! ^-^ \n");
-
- //创建一个表,如果该表存在,则不创建,并给出提示信息,存储在 zErrMsg 中
- sql = "CREATE TABLE SensorData(\
- ID INTEGER PRIMARY KEY, \
- SensorID INTEGER, \
- SiteNum INTEGER, \
- Time VARCHAR(12), \
- SensorParameter REAL \
- );";
- sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
-
- //插入数据
- sql = "INSERT INTO \"SensorData\" VALUES( NULL , 1 , 1 , '201205011206', 18.9 );" ;
- sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
-
- sql = "INSERT INTO \"SensorData\" VALUES( NULL , 1 , 1 , '201205011306', 16.4 );" ;
- sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
-
-
- sqlite3_close(db); //关闭数据库
- return 0;
- }
(2)然后
root@ubuntu:/tmp# ./build/bin/sqlite3 zieckey.db
SQLite version 3.21.0 2017-10-24 18:55:49
Enter ".help" for usage hints.
sqlite> select * from SensorData;//这里记得有分号
1|1|1|201205011206|18.9
2|1|1|201205011306|16.4
sqlite>
3、查询数据库中的数据
(1)代码
- // name: query.c
- // This prog is used to test C/C++ API for sqlite3 .It is very simple,ha !
-
- #include <stdio.h>
- #include <stdlib.h>
- #include "sqlite3.h"
- #define _DEBUG_
-
- int main( void )
- {
- sqlite3 *db=NULL;
- char *zErrMsg = 0;
-
- int rc;
-
- rc = sqlite3_open("zieckey.db", &db); //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
- if( rc )
- {
- fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
- sqlite3_close(db);
- return 0;
- }
- else printf("You have opened a sqlite3 database named zieckey.db successfully!\nCongratulations! Have fun ! ^-^ \n");
-
- //创建一个表,如果该表存在,则不创建,并给出提示信息,存储在 zErrMsg 中
- char *sql = " CREATE TABLE SensorData(\
- ID INTEGER PRIMARY KEY,\
- SensorID INTEGER,\
- SiteNum INTEGER,\
- Time VARCHAR(12),\
- SensorParameter REAL\
- );" ;
- sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
-
- #ifdef _DEBUG_
- printf("zErrMsg = %s \n", zErrMsg);
- #endif
-
- //插入数据
- sql = "INSERT INTO \"SensorData\" VALUES(NULL , 1 , 1 , '200605011206', 18.9 );" ;
- sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
-
- sql = "INSERT INTO \"SensorData\" VALUES(NULL , 1 , 1 , '200605011306', 16.4 );" ;
- sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
-
- int nrow = 0, ncolumn = 0;
- char **azResult; //二维数组存放结果
-
- //查询数据
- /*
- int sqlite3_get_table(sqlite3*, const char *sql,char***result , int *nrow , int *ncolumn ,char **errmsg );
- result中是以数组的形式存放你所查询的数据,首先是表名,再是数据。
- nrow ,ncolumn分别为查询语句返回的结果集的行数,列数,没有查到结果时返回0
- */
- sql = "SELECT * FROM SensorData ";
- sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
-
- int i = 0 ;
- printf( "row:%d column=%d \n" , nrow , ncolumn );
- printf( "\nThe result of querying is : \n" );
-
- for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )
- printf( "azResult[%d] = %s\n", i , azResult[i] );
-
- //释放掉 azResult 的内存空间
- sqlite3_free_table( azResult );
-
- #ifdef _DEBUG_
- printf("zErrMsg = %s \n", zErrMsg);
- #endif
-
- sqlite3_close(db); //关闭数据库
- return 0;
-
- }
(2)然后
root@ubuntu:/tmp# ./a.out
You have opened a sqlite3 database named zieckey.db successfully!
Congratulations! Have fun ! ^-^
zErrMsg = table SensorData already exists
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] = 1
azResult[7] = 1
azResult[8] = 201205011206
azResult[9] = 18.9
azResult[10] = 2
azResult[11] = 1
azResult[12] = 1
azResult[13] = 201205011306
azResult[14] = 16.4
azResult[15] = 3
azResult[16] = 1
azResult[17] = 1
azResult[18] = 200605011206
azResult[19] = 18.9
azResult[20] = 4
azResult[21] = 1
azResult[22] = 1
azResult[23] = 200605011306
azResult[24] = 16.4
zErrMsg = (null)
root@ubuntu:/tmp#
(3)分析
azResult 的前面 5 个数据正好是我们的表 SensorData 的列属性,之后才是我们要查询的数据。
所以我们的程序中才有 i<( nrow + 1 ) * ncolumn 的判断条件:
for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )
printf( "azResult[%d] = %s\n", i , azResult[i] );
输出中有 zErrMsg = (null) 这样的字句,这是 zErrMsg 保留的错误信息,zErrMsg 为空,表明在执行过程中没有错误信息。
4、删除数据库中的特定的数据
(1)代码
- // name: delete.c
- // This prog is used to test C/C++ API for sqlite3 .It is very simple,ha !
-
- #include <stdio.h>
- #include <stdlib.h>
- #include "sqlite3.h"
- #define _DEBUG_
-
- int main( void )
- {
- sqlite3 *db=NULL;
- char *zErrMsg = 0;
-
- int rc;
-
- rc = sqlite3_open("zieckey.db", &db); //打开指定的数据库文件,如果不存在将创建一个同名的数据库文件
- if( rc )
- {
- fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
- sqlite3_close(db);
- return 0;
- }
- else printf("You have opened a sqlite3 database named zieckey.db successfully!\nCongratulations! Have fun ! ^-^ \n");
-
- //创建一个表,如果该表存在,则不创建,并给出提示信息,存储在 zErrMsg 中
- char *sql = " CREATE TABLE SensorData(\
- ID INTEGER PRIMARY KEY,\
- SensorID INTEGER,\
- SiteNum INTEGER,\
- Time VARCHAR(12),\
- SensorParameter REAL\
- );" ;
- sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
-
- #ifdef _DEBUG_
- printf("zErrMsg = %s \n", zErrMsg);
- #endif
-
- //插入数据
- sql = "INSERT INTO \"SensorData\" VALUES(NULL , 1 , 1 , '200605011206', 18.9 );" ;
- sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
-
- sql = "INSERT INTO \"SensorData\" VALUES(NULL , 23 , 45 , '200605011306', 16.4 );" ;
- sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
-
- sql = "INSERT INTO \"SensorData\" VALUES(NULL , 34 , 45 , '200605011306', 15.4 );" ;
- sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
-
-
- int nrow = 0, ncolumn = 0;
- char **azResult; //二维数组存放结果
-
- //查询数据
- sql = "SELECT * FROM SensorData ";
- sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
- int i = 0 ;
- printf( "row:%d column=%d \n" , nrow , ncolumn );
- printf( "\nThe result of querying is : \n" );
- for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )
- printf( "azResult[%d] = %s\n", i , azResult[i] );
-
- //删除数据
- sql = "DELETE FROM SensorData WHERE SensorID = 1 ;" ;
- sqlite3_exec( db , sql , 0 , 0 , &zErrMsg );
- #ifdef _DEBUG_
- printf("zErrMsg = %s \n", zErrMsg);
- #endif
- sql = "SELECT * FROM SensorData ";
- sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg );
- printf( "\n\n\n\nrow:%d column=%d " , nrow , ncolumn );
- printf( "\nAfter deleting , the result of querying is : \n" );
- for( i=0 ; i<( nrow + 1 ) * ncolumn ; i++ )
- printf( "azResult[%d] = %s\n", i , azResult[i] );
-
-
- //释放掉 azResult 的内存空间
- sqlite3_free_table( azResult );
-
- #ifdef _DEBUG_
- printf("zErrMsg = %s \n", zErrMsg);
- #endif
-
- sqlite3_close(db); //关闭数据库
- return 0;
-
- }
(2)然后
root@ubuntu:/tmp# gcc test.c -lsqlite3 -L/tmp/build/lib/ -I/tmp/build/include/
root@ubuntu:/tmp# ./a.out
You have opened a sqlite3 database named zieckey.db successfully!
Congratulations! Have fun ! ^-^
zErrMsg = table SensorData already exists
row:7 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] = 1
azResult[7] = 1
azResult[8] = 201205011206
azResult[9] = 18.9
azResult[10] = 2
azResult[11] = 1
azResult[12] = 1
azResult[13] = 201205011306
azResult[14] = 16.4
azResult[15] = 3
azResult[16] = 1
azResult[17] = 1
azResult[18] = 200605011206
azResult[19] = 18.9
azResult[20] = 4
azResult[21] = 1
azResult[22] = 1
azResult[23] = 200605011306
azResult[24] = 16.4
azResult[25] = 5
azResult[26] = 1
azResult[27] = 1
azResult[28] = 200605011206
azResult[29] = 18.9
azResult[30] = 6
azResult[31] = 23
azResult[32] = 45
azResult[33] = 200605011306
azResult[34] = 16.4
azResult[35] = 7
azResult[36] = 34
azResult[37] = 45
azResult[38] = 200605011306
azResult[39] = 15.4
zErrMsg = (null)
row:2 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] = 6
azResult[6] = 23
azResult[7] = 45
azResult[8] = 200605011306
azResult[9] = 16.4
azResult[10] = 7
azResult[11] = 34
azResult[12] = 45
azResult[13] = 200605011306
azResult[14] = 15.4
zErrMsg = (null)
root@ubuntu:/tmp#
(3)分析
从程序输出结果就可以看出,在删除数据前,我们有三条记录,删除数据后我们发现,数据库内记录少了,从而实现了我们的删除数据目的。
Copyright © 2003-2013 www.wpsshop.cn 版权所有,并保留所有权利。