首先pThread 不是linux系统默认库,连接的时候需要使用库libpthread.a.
加入-lpthread参数。另外会有lopen什么找不到的情况。加入-ldl
指定目录。Project__targets options___C/C++ parser option
C/C++语言中调用 sqlite 的函数接口来实现对数据库的管理(创建数据库、创建表格、插入数据、查询、数据、删除数据等)。 首先要编译好 sqlite的库文件 : libsqlite3.a libsqlite3.la libsqlite3.so libsqlite3.so.0 libsqlite3.so.0.8.6 pkgconfig 可执行文件 : sqlite3 本次测试: sqlite3的库文件目录是:/usr/local/lib 可执行文件 sqlite3 的目录是: /usr/local/bin 头文件 sqlite3.h 的目录是: /usr/local/include 用ls命令查看如下: [root@localhost config]# ls /usr/local/lib libclamav.a libclamunrar_iface.a libclamunrar.so libsqlite3.so libclamav.la libclamunrar_iface.la libclamunrar.so.5 libsqlite3.so.0 libclamav.so libclamunrar_iface.so libclamunrar.so.5.0.3 libsqlite3.so.0.8.6 libclamav.so.5 libclamunrar_iface.so.5 libmstring.so pkgconfig libclamav.so.5.0.3 libclamunrar_iface.so.5.0.3 libsqlite3.a libclamunrar.a libclamunrar.la libsqlite3.la 此目录下包含库文件: libsqlite3.a libsqlite3.la libsqlite3.so libsqlite3.so.0 libsqlite3.so.0.8.6 pkgconfig 开始sqlite编程: 1. 下面是一个C程序的例子,显示怎么使用 sqlite 的 C/C++ 接口. 数据库的名字由第一个参数取得且第个参数或更多的参数是 SQL 执行语句. 这个函数调用sqlite3_open() 打开数据库,并且调用sqlite3_close() 关闭数据库连接。 程序一:opendbslite.c:
- #include <stdio.h>
- #include <stdlib.h>
- #include <sqlite3.h>
- 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);
- exit(1);
- }
- else printf("You have opened a sqlite3 database named zieckey.db successfully!/nCongratulations! Have fun ! ^-^ /n");
- sqlite3_close(db); //关闭数据库
- return 0;
- }
编译(问题):
[root@localhost liuxltest]# gcc -o opendbsqlite opendbsqlite.c /tmp/ccuquUQN.o: In function `main':opendbsqlite.c:(.text+0x2e): undefined reference to `sqlite3_open'opendbsqlite.c:(.text+0x42): undefined reference to `sqlite3_errmsg'opendbsqlite.c:(.text+0x67): undefined reference to `sqlite3_close'opendbsqlite.c:(.text+0x8a): undefined reference to `sqlite3_close'collect2: ld returned 1 exit status编译(解决):出现上述问题是因为没有找到库文件的问题。由于用到了用户自己的库文件,所用应该指明所用到的库,我们可以这样编译:[root@localhost liuxltest]# gcc -o opendbsqlite opendbsqlite.c -lsqlite3用 -lsqlite3 选项就可以了(前面我们生成的库文件是 libsqlite3.so.0.8.6 等,去掉前面的lib和后面的版本标志,就剩下 sqlite3 了所以是 -lsqlite3 )执行:[root@localhost liuxltest]# ./opendbsqlite You have opened a sqlite3 database named zieckey.db successfully!Congratulations! Have fun ! ^-^ [root@localhost liuxltest]#
2. 插入 :insert
在C语言中向数据库插入数据:sqlite3_exec的函数原型说明如下:int sqlite3_exec( sqlite3*, /* An open database */ const char *sql, /* SQL to be executed */ sqlite_callback, /* Callback function */ void *, /* 1st argument to callback function */ char **errmsg /* Error msg written here */);程序二:insert.c:
- #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);
- exit(1);
- }
- 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("%s/n",zErrMsg);
- #endif
- //插入数据
- char* sql1 = "INSERT INTO /"SensorData/" VALUES( NULL , 1 , 1 , '200605011206', 18.9 );" ;
- sqlite3_exec( db , sql1 , 0 , 0 , &zErrMsg );
- char* sql2 = "INSERT INTO /"SensorData/" VALUES( NULL , 1 , 1 , '200605011306', 16.4 );" ;
- sqlite3_exec( db , sql2 , 0 , 0 , &zErrMsg );
- sqlite3_close(db); //关闭数据库
- return 0;
- }
编译运行:
[root@localhost liuxltest]# gcc -o insert insert.c -lsqlite3[root@localhost liuxltest]# ./insert You have opened a sqlite3 database named zieckey.db successfully!Congratulations! Have fun ! ^-^ (null)(null)(null)[root@localhost liuxltest]# 查看是否插入数据:[root@localhost liuxltest]# /usr/local/bin/sqlite3 zieckey.db "select * from SensorData"
3. 查询: SELETE
C语言中查询数据库中的数据。 函数接口sqlite3_get_table( db , sql , &azResult , &nrow , &ncolumn , &zErrMsg ); 解释: int sqlite3_get_table(sqlite3*, const char *sql,char***result , int *nrow , int *ncolumn ,char **errmsg ); result中是以数组的形式存放你所查询的数据,首先是表名,再是数据。 nrow ,ncolumn分别为查询语句返回的结果集的行数,列数,没有查到结果时返回0程序三:query.c:- #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);
- exit(1);
- }
- 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;
- }
这里用到了一个查询的语句是 "SELECT * FROM SensorData " ,
编译运行:[root@localhost liuxltest]# gcc -o query query.c -lsqlite3 [root@localhost liuxltest]# ./query You have opened a sqlite3 database named zieckey.db successfully!Congratulations! Have fun ! ^-^ zErrMsg = (null) row:2 column=5 The result of querying is : azResult[0] = IDazResult[1] = SensorIDazResult[2] = SiteNumazResult[3] = TimeazResult[4] = SensorParameterazResult[5] = 1azResult[6] = 1azResult[7] = 1azResult[8] = 200605011206azResult[9] = 18.9azResult[10] = 2azResult[11] = 1azResult[12] = 1azResult[13] = 200605011306azResult[14] = 16.4zErrMsg = (null) [root@localhost liuxltest]# 这里我们可以看到,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. 删除:delete C语言中删除数据库中的特定的数据。 程序四: delete.c:
- #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);
- exit(1);
- }
- 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;
- }