sqlite 在麒麟下安装、编译

sqlite 在麒麟下安装

1、登录官方网站,查看下载地址,下载,安装

    https://www.sqlite.org/download.html   

$ wget http://www.sqlite.org/sqlite-3******.tar.gz
$ ./configure
$ make
$ sudo make install

2、验证

$ sqlite3
$ sqlite

3、程序验证

首先要编译好
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

#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;  
   
}  

  

posted @ 2022-03-23 10:16  古锁阳关  阅读(613)  评论(0编辑  收藏  举报