天天看點

嵌入式linux下sqlite3資料庫操作

query.c
           
#include <stdio.h> 
#include <stdlib.h> 
#include <string.h>
#include "sqlite3.h" 
#define _DEBUG_  
int main( void )  
{  
 int i = 0 ;  
 int nrow = 0, ncolumn = 0;  
 char **azResult; //二維數組存放結果  
 sqlite3 *db=NULL;  
 char *zErrMsg = 0; 
 char value[200]; 
 int id=10;
 int ip=19;
 int rc;  
   rc = sqlite3_open("aa.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");
  //建立資料表
 rc = sqlite3_exec(db,"create table mytable(id,ip);",NULL,NULL,zErrMsg);
 //插入資料
 memset(value,0,200);
 //sprintf(value, "update tbl set id=%d,ip=%d",id,ip);//更新
 sprintf(value, "insert into mytable(id,ip) values(%d,%d)",id,ip);//插入
 sqlite3_exec(db,value,NULL,NULL,zErrMsg);
 //查詢資料  
 
 sqlite3_get_table( db , "SELECT * FROM mytable ", &azResult , &nrow , &ncolumn , &zErrMsg );  
 
 printf( "row:%d column=%d \n" , nrow , ncolumn );  
 printf( "The result of querying is : \n" );  
   
 for( i=ncolumn ; i<( nrow + 1 ) * ncolumn ; i++ )  
  printf( "azResult[%d] = %s\n", i-ncolumn , azResult[i] );  
 //釋放掉  azResult 的記憶體空間  
 sqlite3_free_table( azResult ); 
   
 #ifdef _DEBUG_  
        printf("zErrMsg = %s \n", zErrMsg); 
 #endif  
 sqlite3_close(db); //關閉資料庫  
 return 0;  
   
} 
           

編譯的時候要加上-lsqlite3,指定連結庫。(gcc編譯隻需加-lsqlite3,arm-linux-gcc編譯時除此之外還要添加-I /usr/include -L /home/sqlite3_test)

sqlite需要的庫檔案libsqlite3.so、libsqlite3.so.0、libsqlite3.so.0.8.6放在/home/sqlite3_test目錄下,這三個庫檔案和虛拟機下的名字雖相同,卻不能用,一定要是嵌入式移植的庫檔案。

程式最後運作結果為

嵌入式linux下sqlite3資料庫操作

上面是sqlite3資料庫的基本操作(打開建立資料庫,建立資料表,插入資料,查詢資料)。

其中查詢資料有兩種操作方法,分别調用不同的函數,上面的方法比較簡單,直接調用sqlite3_get_table()函數得到結果,還有一種方法如下面query1 .c所示。

query1.c
           
#include <stdlib.h>
#include <stdio.h>
#include <sqlite3.h>
#include <string.h>

int Display(void *para,int n_column,char **column_value,char **column_name)
{
   	int i;
	printf("記錄包含 %d 個字段\n",n_column);
	for( i = 0;i < n_column;i++)
	{

   	 	printf("字段名:%s 字段值:%s\n",column_name[i],column_value[i]);

	}
	printf("------------------\n ");         
	return 0;
}

int main ()
{	
	char value[200];
	sqlite3 *db;
	char *errmsg = NULL;
	int rc;
	rc = sqlite3_open("bb.db",&db);
	if(rc)
	{
		fprintf(stderr, "Can't open sqlite: %s\n", sqlite3_errmsg(db));
    		sqlite3_close(db);
      		exit(1);
        }
       else 
	printf("open sqlite success\n");
	
	rc = sqlite3_exec(db,"create table mytable(id,ip);",NULL,NULL,errmsg);
 	if(rc == SQLITE_OK)
    	{
        	printf("create table success\n");
    	}
    	else
    	{
        	printf("create table failure\n");
    	}
	sqlite3_exec(db,"insert into mytable(id,ip) values(1,2);",NULL,NULL,errmsg);
	sqlite3_exec(db,"select * from mytable",Display,NULL,errmsg);

	sqlite3_close(db);


}
           

運作結果如下,由于之前已經建立過mytable這個表,再次建立會提示建立失敗。

嵌入式linux下sqlite3資料庫操作

查詢采用了回顯函數Display(void *para,int n_column,char **column_value,char **column_name)和sqlite3_exec(db,"select * from mytable",Display,NULL,errmsg);組合使用