久久久国产精品视频袁燕,99re久久精品国产,亚洲欧美日韩国产综合v,天天躁夜夜躁狠狠久久,激情五月婷婷激情五月婷婷

一口Linux
認(rèn)證:優(yōu)質(zhì)創(chuàng)作者
所在專題目錄 查看專題
兩個線程,兩個互斥鎖,怎么形成一個死循環(huán)?
Linux庫概念,動態(tài)庫和靜態(tài)庫的制作,如何移植第三方庫
關(guān)于線程調(diào)度,你需要了解的幾個基礎(chǔ)知識點(diǎn)都在這里了
從概念到代碼,一文了解Linux系統(tǒng)守護(hù)進(jìn)程知識
如何用C語言操作sqlite3,一文搞懂
linux共享內(nèi)存入門
作者動態(tài) 更多
iptables -m connlimit導(dǎo)致內(nèi)存不足
07-20 18:05
為什么對技術(shù)人員的考核大多都只看加班時間?
07-08 23:40
某通信公司筆試題,你會做幾道?
07-01 21:58
10種初學(xué)者最常見的c語言段錯誤實例及原因分析
05-30 12:13
linux系統(tǒng)監(jiān)控工具小神器:btop
05-17 17:37

如何用C語言操作sqlite3,一文搞懂

嵌入式數(shù)據(jù)共3篇,本片是最后一篇。

嵌入式數(shù)據(jù)庫sqlite3【基礎(chǔ)篇】-基本命令操作,小白一看就懂

嵌入式數(shù)據(jù)庫sqlite3【進(jìn)階篇】-子句和函數(shù)的使用,小白一文入門

sqlite3編程接口非常多,對于初學(xué)者來說,我們暫時只需要掌握常用的幾個函數(shù),其他函數(shù)自然就知道如何使用了。

數(shù)據(jù)庫

本篇假設(shè)數(shù)據(jù)庫為my.db,有數(shù)據(jù)表student。

nonamescore4一口Linux89.0

創(chuàng)建表格語句如下:

CREATE TABLE  IF NOT EXISTS student (no integer primary key, name text, score real);

常用函數(shù)

sqlite3_open

int   sqlite3_open(char  *path,   sqlite3 **db);功能:    打開sqlite數(shù)據(jù)庫參數(shù):    path: 數(shù)據(jù)庫文件路徑    db: 指向sqlite句柄的指針,后面對數(shù)據(jù)庫所有的操作都要依賴這個句柄返回值: 成功返回0,失敗返回錯誤碼(非零值)

sqlite3_close

int   sqlite3_close(sqlite3 *db);功能: 關(guān)閉sqlite數(shù)據(jù)庫返回值: 成功返回0,失敗返回錯誤碼
const  char  *sqlite3_errmsg(sqlite3 *db);功能: 打印錯誤信息        返回值: 返回錯誤信息

不使用回調(diào)函數(shù)執(zhí)行SQL語句

sqlite3_get_table

int   sqlite3_get_table(sqlite3 *db, const  char  *sql,  char ***resultp,  int*nrow,  int *ncolumn, char **errmsg);
功能: 執(zhí)行SQL操作參數(shù): 
db:數(shù)據(jù)庫句柄 sql:SQL語句 
resultp:用來指向sql執(zhí)行結(jié)果的指針 
nrow:滿足條件的記錄的數(shù)目 
ncolumn:每條記錄包含的字段數(shù)目 
errmsg:錯誤信息指針的地址
返回值: 成功返回0,失敗返回錯誤碼

舉例

下面比如我們要顯示student表中所有的數(shù)據(jù)信息,我們就可以利用sqlite3_get_table()執(zhí)行語句:

select * from student

實現(xiàn)代碼如下:

void do_show_sample(sqlite3 *db)
 {
  char **result, *errmsg;
 int nrow, ncolumn, i, j, index;

 if (sqlite3_get_table(db, "select * from student", &result, &nrow, &ncolumn, &errmsg) != 0)
 {
  printf("error : %s\n", errmsg);
  sqlite3_free(errmsg);
 }
 index = ncolumn;
 for (i=0; i<nrow; i++)
 {
  for (j=0; j<ncolumn; j++)
  {
   printf("%-8s : %-8s\n", result[j], result[index]);   
   index++;
  }
  printf("************************\n");
 }
 sqlite3_free_table(result);
 return;
 }

假定當(dāng)前的表格的數(shù)據(jù)信息如下:

 

關(guān)于這個函數(shù)中出現(xiàn)的這些參數(shù)的具體含義,我們可以見下圖:

sqlite3編程接口非常多,對于初學(xué)者來說,我們暫時只需要掌握常用的幾個函數(shù),其他函數(shù)自然就知道如何使用了。

數(shù)據(jù)庫

本篇假設(shè)數(shù)據(jù)庫為my.db,有數(shù)據(jù)表student。

 

創(chuàng)建表格語句如下:

CREATE TABLE  IF NOT EXISTS student (no integer primary key, name text, score real);

常用函數(shù)

sqlite3_open

int   sqlite3_open(char  *path,   sqlite3 **db);

功能:    打開sqlite數(shù)據(jù)庫
參數(shù): 
path: 數(shù)據(jù)庫文件路徑 
db: 指向sqlite句柄的指針
返回值: 成功返回0,失敗返回錯誤碼(非零值)

sqlite3_close

int   sqlite3_close(sqlite3 *db);
功能: 關(guān)閉sqlite數(shù)據(jù)庫      
返回值: 成功返回0,失敗返回錯誤碼
const  char  *sqlite3_errmsg(sqlite3 *db);
功能: 打印錯誤信息        
返回值: 返回錯誤信息

不使用回調(diào)函數(shù)執(zhí)行SQL語句

sqlite3_get_table

int   sqlite3_get_table(sqlite3 *db, const  char  *sql,  char ***resultp,  int*nrow,  int *ncolumn, char **errmsg);
功能:
 執(zhí)行SQL操作
參數(shù):
 db:數(shù)據(jù)庫句柄
 sql:SQL語句
 resultp:用來指向sql執(zhí)行結(jié)果的指針
 nrow:滿足條件的記錄的數(shù)目
 ncolumn:每條記錄包含的字段數(shù)目
 errmsg:錯誤信息指針的地址
返回值:
 成功返回0,失敗返回錯誤碼

舉例

下面比如我們要顯示student表中所有的數(shù)據(jù)信息,我們就可以利用sqlite3_get_table()執(zhí)行語句:

select * from student

實現(xiàn)代碼如下:

void do_show_sample(sqlite3 *db)
 {
  char **result, *errmsg;
 int nrow, ncolumn, i, j, index;

 if (sqlite3_get_table(db, "select * from student", &result, &nrow, &ncolumn, &errmsg) != 0)
 {
  printf("error : %s\n", errmsg);
  sqlite3_free(errmsg);
 }
 index = ncolumn;
 for (i=0; i<nrow; i++)
 {
  for (j=0; j<ncolumn; j++)
  {
   printf("%-8s : %-8s\n", result[j], result[index]);   
   index++;
  }
  printf("************************\n");
 }
 sqlite3_free_table(result);
 return;
 }

假定當(dāng)前的表格的數(shù)據(jù)信息如下:

 

關(guān)于這個函數(shù)中出現(xiàn)的這些參數(shù)的具體含義,我們可以見下圖:

在這里插入圖片描述

由上圖可知:代碼中:

ncolumn = 3nrow    = 5result 指向所有的結(jié)果組成的字符串?dāng)?shù)組,各個具體字符串的下標(biāo),圖上已經(jīng)標(biāo)明。

結(jié)合此圖再去理解代碼,就很容易理解代碼的實現(xiàn)原理。

使用回調(diào)函數(shù)執(zhí)行SQL語句

sqlite3_exec

typedef  int (*sqlite3_callback)(void *, int, char **, char **);

int   sqlite3_exec(sqlite3 *db, const  char  *sql,  sqlite3_callback callback, void *,  char **errmsg);
功能:
 執(zhí)行SQL操作
參數(shù):
 db:數(shù)據(jù)庫句柄
 sql:SQL語句,就是我們前面兩章用于操作表的增刪改查語句
 callback:回調(diào)函數(shù)
 errmsg:錯誤信息指針的地址
返回值:
 成功返回0,失敗返回錯誤碼

回調(diào)函數(shù)

typedef  int (*sqlite3_callback)(void *para, int f_num, char **f_value, char **f_name);
功能:
 每找到一條記錄自動執(zhí)行一次回調(diào)函數(shù)
參數(shù):
 para:傳遞給回調(diào)函數(shù)的參數(shù)
 f_num:記錄中包含的字段數(shù)目
 f_value:包含每個字段值的指針數(shù)組
 f_name:包含每個字段名稱的指針數(shù)組
返回值:
 成功返回0,失敗返回-1

舉例

sqlite3 *db;
char  *errmsg,**resultp;

int callback(void *para, int f_num, char **f_val, char **f_name)
{
 int i;

 for (i=0; i<f_num; i++)
 {
  printf("%-8s", f_val[i]);
 }
 printf("\n");

 return 0;
}

void do_show(sqlite3 *db)
{
 char *errmsg;

 printf("no      name    score\n");
 
 if (sqlite3_exec(db, "select * from student", callback, NULL, &errmsg) != 0)
 {
  printf("error : %s\n", sqlite3_errmsg(db));
 }
 printf("\n");

 return;
}

回調(diào)函數(shù)方法實現(xiàn)的代碼,需要實現(xiàn)一個回調(diào)函數(shù):callback。函數(shù)sqlite3_exec()在解析命令"select * from student" ,沒獲取到一行數(shù)據(jù)就會調(diào)用一次回調(diào)函數(shù), 參考上面的表格student,

callback()總共會被調(diào)用5次,
f_num 對應(yīng)結(jié)果的列數(shù),為3
f_value 則指向 每一列對應(yīng)的值組成的字符串?dāng)?shù)組

假設(shè)現(xiàn)在callback是第四次被調(diào)用,如下圖:

運(yùn)行結(jié)果

編譯需要使用第三方庫lsqlite3。

gcc student.c -o run -lsqlite3

其他函數(shù)

sqlite3 *pdb, 數(shù)據(jù)庫句柄,跟文件句柄FILE很類似
sqlite3_stmt *stmt, 這個相當(dāng)于ODBC的Command對象,用于保存編譯好的SQL語句

sqlite3_exec(), 執(zhí)行非查詢的sql語句
sqlite3_prepare(), 準(zhǔn)備sql語句,執(zhí)行select語句或者要使用parameter bind時,用這個函數(shù)(封裝了sqlite3_exec)
Sqlite3_step(), 在調(diào)用sqlite3_prepare后,使用這個函數(shù)在記錄集中移動

還有一系列的函數(shù),用于從記錄集字段中獲取數(shù)據(jù),如

sqlite3_column_text(), 取text類型的數(shù)據(jù)
sqlite3_column_blob(),取blob類型的數(shù)據(jù)
sqlite3_column_int(), 取int類型的數(shù)據(jù)

國際慣例,上完整代碼:

#include <stdio.h>
#include <stdlib.h>
#include <unistd.h>
#include <sqlite3.h>

void do_insert(sqlite3 *db)
{
 int no;
 char name[16];
 float score;
 char sqlstr[128], *errmsg;

 printf("input no : ");
 scanf("%d", &no);
 printf("input name : ");
 scanf("%s", name);
 printf("input score : ");
 scanf("%f", &score);
 sprintf(sqlstr, "insert into student values (%d, '%s', %.1f)", 
 no, name, score);
 #if __DEBUG
 printf("cmd:%s\n",sqlstr);
 #endif
 if (sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg) != 0)
 {
  printf("error : %s\n", sqlite3_errmsg(db));
 }
 else
 {
  printf("insert is done\n");
 }
 printf("\n");

 return;
}

void do_delete(sqlite3 *db)
{
 char *errmsg;
 char sqlstr[128], expression[64];

 printf("input expression : ");
 scanf("%s", expression);//name='ma'
 sprintf(sqlstr, "delete from student where %s", expression);
#if __DEBUG
 printf("cmd:%s\n",sqlstr);
#endif
 if (sqlite3_exec(db, sqlstr, NULL, NULL, &errmsg) != 0)
 {
  printf("error : %s\n", sqlite3_errmsg(db));
 }
 else
 {
  printf("deletet is done\n");
 }
 printf("\n");

 return;
}
 
int callback(void *para, int f_num, char **f_val, char **f_name)
{
 int i;

 for (i=0; i<f_num; i++)
 {
  printf("%-8s", f_val[i]);
 }
 printf("\n");

 return 0;
}

void do_show(sqlite3 *db)
{
 char *errmsg;

 printf("no      name    score\n");

 if (sqlite3_exec(db, "select * from student", callback, NULL, &errmsg) != 0)
 {
  printf("error : %s\n", sqlite3_errmsg(db));
 }
 printf("\n");

 return;
}

 void do_show_sample(sqlite3 *db)
 {
  char **result, *errmsg;
 int nrow, ncolumn, i, j, index;

 if (sqlite3_get_table(db, "select * from student", &result, &nrow, &ncolumn, &errmsg) != 0)
 {
  printf("error : %s\n", errmsg);
  sqlite3_free(errmsg);
 }
 
 index = ncolumn;

 for (i=0; i<nrow; i++)
 {
  for (j=0; j<ncolumn; j++)
  {
   printf("%-8s : %-8s\n", result[j], result[index]);
   
    
   index++;
  }
  printf("************************\n");
 }
 sqlite3_free_table(result);

 return;
 }
 

int main()
{
 sqlite3 *db;
 int n;
 char clean[64];

 if (sqlite3_open("my.db", &db) < 0)
 {
  printf("fail to sqlite3_open : %s\n", sqlite3_errmsg(db));
  return -1;
 }

 while ( 1 )
 {
  printf("*********************************************\n");
  printf("1: insert record   \n2: delete record  \n3: show record  \n4: quit\n");
  printf("*********************************************\n");
  printf("please select : "); 
  
  if (scanf("%d", &n) != 1)
  {
   fgets(clean, 64, stdin);
   printf("\n");
   continue;
  }
  switch ( n )
  {
   case 1 :
    do_insert(db);
    break;
   case 2 :
    do_delete(db);
    break;
   case 3 :
    do_show_sample(db);
    break;
   case 4 :
    sqlite3_close(db);
    exit(0);
  }
 }
 return 0;
}

運(yùn)行主頁面:

插入記錄:

顯示記錄:

刪除記錄:

 

End

聲明:本內(nèi)容為作者獨(dú)立觀點(diǎn),不代表電子星球立場。未經(jīng)允許不得轉(zhuǎn)載。授權(quán)事宜與稿件投訴,請聯(lián)系:editor@netbroad.com
覺得內(nèi)容不錯的朋友,別忘了一鍵三連哦!
贊 2
收藏 3
關(guān)注 181
成為作者 賺取收益
全部留言
0/200
成為第一個和作者交流的人吧