使用C封裝Sqlite3實現簡單API
標簽: 技術總結
以下是對sqlite3接口的簡單封裝,這里只實現了幾個最基本的接口,喜歡的朋友可以繼續擴展
過程中參考了下面的文章
https://www.runoob.com/sqlite/sqlite-c-cpp.html
編譯方法:gcc sqlite_test.c sqlite_api.c -lsqlite3
運行: ./a.out
接口頭文件
#ifndef __SQLITE_API_H__
#define __SQLITE_API_H__
#ifdef __cplusplus
extern "C"{
#endif
#define DBBUSY_TIMEOUT (1100)
/*****************************************************************************
函 數 名 : API_Open_Sqlite
功能描述 : 打開數據庫
輸入參數 : const char *DbFile
sqlite3 **Db
輸出參數 : 無
返 回 值 :
調用函數 :
被調函數 :
修改歷史 :
1.日 期 : 2019年12月10日
作 者 :
修改內容 : 新生成函數
*****************************************************************************/
int API_Open_Sqlite(sqlite3 **Db, const char *DbFileName);
/*****************************************************************************
函 數 名 : API_Close_Sqlite
功能描述 : 關閉數據庫
輸入參數 : sqlite3 *Db
輸出參數 : 無
返 回 值 :
調用函數 :
被調函數 :
修改歷史 :
1.日 期 : 2019年12月10日
作 者 :
修改內容 : 新生成函數
*****************************************************************************/
int API_Close_Sqlite(sqlite3 *Db);
/*****************************************************************************
函 數 名 : API_Create_Sqlite_Table
功能描述 : 創建數據庫表
輸入參數 : sqlite3 *Db
const char* Sql
輸出參數 : 無
返 回 值 :
調用函數 :
被調函數 :
修改歷史 :
1.日 期 : 2019年12月10日
作 者 :
修改內容 : 新生成函數
*****************************************************************************/
int API_Create_Sqlite_Table(sqlite3 *Db, const char* Sql);
/*****************************************************************************
函 數 名 : API_Insert_Sqlite
功能描述 : 往數據中插入內容
輸入參數 : sqlite3 *Db
const char* Sql
輸出參數 : 無
返 回 值 :
調用函數 :
被調函數 :
修改歷史 :
1.日 期 : 2019年12月10日
作 者 :
修改內容 : 新生成函數
*****************************************************************************/
int API_Insert_Sqlite(sqlite3 *Db, const char* Sql);
/*****************************************************************************
函 數 名 : API_Select_Sqlite
功能描述 : 查詢數據庫
輸入參數 : sqlite3 *Db
const char* Sql
sqlite3_callback Cbfun
void *data
輸出參數 : 無
返 回 值 :
調用函數 :
被調函數 :
修改歷史 :
1.日 期 : 2019年12月11日
作 者 :
修改內容 : 新生成函數
*****************************************************************************/
int API_Select_Sqlite(sqlite3 *Db, const char* Sql, sqlite3_callback Cbfun ,void *data);
/*****************************************************************************
函 數 名 : API_Delete_Sqlite
功能描述 : 刪除數據庫內容
輸入參數 : sqlite3 *Db
const char* Sql
輸出參數 : 無
返 回 值 :
調用函數 :
被調函數 :
修改歷史 :
1.日 期 : 2019年12月10日
作 者 :
修改內容 : 新生成函數
*****************************************************************************/
int API_Delete_Sqlite(sqlite3 *Db, const char* Sql);
/*****************************************************************************
函 數 名 : API_Update_Sqlite
功能描述 : 更新數據庫內容
輸入參數 : sqlite3 *Db
const char* Sql
輸出參數 : 無
返 回 值 :
調用函數 :
被調函數 :
修改歷史 :
1.日 期 : 2019年12月10日
作 者 :
修改內容 : 新生成函數
*****************************************************************************/
int API_Update_Sqlite(sqlite3 *Db, const char* Sql);
#ifdef __cplusplus
}
#endif
#endif //__SQLITE_API_H__
API實現
#include <stdio.h>
#include <sqlite3.h>
#include "sqlite_api.h"
#ifdef __cplusplus
extern "C"
{
#endif
int API_Open_Sqlite(sqlite3 **Db, const char *DbFileName)
{
sqlite3 *db = NULL;
int iret = 0;
if(NULL == DbFileName)
{
printf("input param is invaild.\n");
return -1;
}
if(NULL != *Db)
{
printf("%s db has opened\n", DbFileName);
return -1;
}
iret = sqlite3_open(DbFileName, Db);
if (SQLITE_OK != iret)
{
printf("open %s db fail, iret is %d.\n", DbFileName, iret);
return -1;
}
printf("Open %s db success.\n", DbFileName);
return 0;
}
int API_Close_Sqlite(sqlite3 *Db)
{
int iret = 0;
if(NULL == Db)
{
printf("input param invalid.\n");
return -1;
}
iret = sqlite3_close_v2(Db);
if (SQLITE_OK != iret)
{
printf("close db fail.\n");
return -1;
}
return 0;
}
int API_Create_Sqlite_Table(sqlite3 *Db, const char* Sql)
{
int iret = 0;
char *ErrorMessage = 0;
if(NULL == Db || NULL == Sql)
{
printf("input param invalid.\n");
return -1;
}
if(SQLITE_BUSY == sqlite3_busy_timeout(Db, DBBUSY_TIMEOUT))
{
printf("sqlite3 is busy.");
return -1;
}
iret = sqlite3_exec(Db, Sql, NULL, NULL, &ErrorMessage);
if(iret != SQLITE_OK )
{
printf("Create tables %s SQL error: %s\n", Sql, ErrorMessage);
sqlite3_free(ErrorMessage);
return -1;
}
printf("Table Created successfully\n");
return 0;
}
int API_Insert_Sqlite(sqlite3 *Db, const char* Sql)
{
int iret = 0;
char *ErrorMessage = 0;
if(NULL == Db || NULL == Sql)
{
printf("input param invalid.\n");
return -1;
}
if(SQLITE_BUSY == sqlite3_busy_timeout(Db, DBBUSY_TIMEOUT))
{
printf("sqlite3 is busy.");
return -1;
}
iret = sqlite3_exec(Db, Sql, NULL, NULL, &ErrorMessage);
if( iret != SQLITE_OK )
{
printf("Insert %s SQL error: %s\n", Sql, ErrorMessage);
sqlite3_free(ErrorMessage);
return -1;
}
printf("Insert Sql successfully\n");
return 0;
}
int API_Select_Sqlite(sqlite3 *Db, const char* Sql, sqlite3_callback Cbfun ,void *data)
{
int iret = 0;
char *ErrorMessage = 0;
if(NULL == Db || NULL == Sql || NULL == data)
{
printf("input param invalid.\n");
return -1;
}
if(SQLITE_BUSY == sqlite3_busy_timeout(Db, DBBUSY_TIMEOUT))
{
printf("sqlite3 is busy.");
return -1;
}
iret = sqlite3_exec(Db, Sql, Cbfun, data, &ErrorMessage);
if( iret != SQLITE_OK )
{
printf("Select %s SQL error: %s\n", Sql, ErrorMessage);
sqlite3_free(ErrorMessage);
return -1;
}
printf("Select Sql successfully\n");
return 0;
}
int API_Delete_Sqlite(sqlite3 *Db, const char* Sql)
{
int iret = 0;
char *ErrorMessage = 0;
if(NULL == Db || NULL == Sql)
{
printf("input param invalid.\n");
return -1;
}
if(SQLITE_BUSY == sqlite3_busy_timeout(Db, DBBUSY_TIMEOUT))
{
printf("sqlite3 is busy.");
return -1;
}
iret = sqlite3_exec(Db, Sql, NULL, NULL, &ErrorMessage);
if( iret != SQLITE_OK )
{
printf("Delete %s SQL error: %s\n", Sql, ErrorMessage);
sqlite3_free(ErrorMessage);
return -1;
}
printf("Delete Sql successfully\n");
return 0;
}
int API_Update_Sqlite(sqlite3 *Db, const char* Sql)
{
int iret = 0;
char *ErrorMessage = 0;
if(NULL == Db || NULL == Sql)
{
printf("input param invalid.\n");
return -1;
}
if(SQLITE_BUSY == sqlite3_busy_timeout(Db, DBBUSY_TIMEOUT))
{
printf("sqlite3 is busy.");
return -1;
}
iret = sqlite3_exec(Db, Sql, NULL, NULL, &ErrorMessage);
if( iret != SQLITE_OK )
{
printf("Update %s SQL error: %s\n", Sql, ErrorMessage);
sqlite3_free(ErrorMessage);
return -1;
}
printf("Update Sql successfully\n");
return 0;
}
#ifdef __cplusplus
}
#endif
測試程序如下
#include <stdio.h>
#include <string.h>
#include <sqlite3.h>
#include "sqlite_api.h"
sqlite3 *g_db = 0;
const char* DbName= "./test.db";
typedef struct DataInfo
{
int Number;
char Name[20];
int Age;
char Birth[20];
}DataInfo_t;
int SelectCb(void *para,int ncolumn,char ** columnvalue,char *columnname[])
{
int i;
printf("total column is %d\n",ncolumn);
for(i = 0;i < ncolumn; i++)
{
printf("col_name:%10s value:%10s\n",columnname[i],columnvalue[i]);
}
return 0;
}
int main(int argc, char *argv[])
{
int iret = -1;
iret = API_Open_Sqlite(&g_db, DbName);
if(0 != iret)
{
return -1;
}
printf("API_Open_Sqlite success\n");
char *CreateSql = "CREATE TABLE IF NOT EXISTS PERSON_INFO(" \
"ID INT PRIMARY KEY NOT NULL," \
"NAME CHAR(20) NOT NULL," \
"AGE INT NOT NULL," \
"BIRTH CHAR(20) NOT NULL);";
iret = API_Create_Sqlite_Table(g_db, CreateSql);
if(0 != iret)
{
return -1;
}
char *InsertSql = "INSERT INTO PERSON_INFO (ID,NAME,AGE,BIRTH) " \
"VALUES (1, 'Paul', 32, '1987-12-12');";
iret = API_Insert_Sqlite(g_db, InsertSql);
if(0 != iret)
{
return -1;
}
char *SelectSql = "SELECT * FROM PERSON_INFO";
DataInfo_t DataInfo;
memset(&DataInfo, 0x00, sizeof(DataInfo));
iret = API_Select_Sqlite(g_db, SelectSql, SelectCb, (void *)&DataInfo);
if(0 != iret)
{
return -1;
}
printf("=========================select first===================================\n");
char *InsertSql2 = "INSERT INTO PERSON_INFO (ID,NAME,AGE,BIRTH) " \
"VALUES (2, 'Boll', 26, '1993-01-01');";
iret = API_Insert_Sqlite(g_db, InsertSql2);
if(0 != iret)
{
return -1;
}
memset(&DataInfo, 0x00, sizeof(DataInfo));
iret = API_Select_Sqlite(g_db, SelectSql, SelectCb, (void *)&DataInfo);
if(0 != iret)
{
return -1;
}
printf("=========================select second===================================\n");
char *DeleteSql = "DELETE from PERSON_INFO WHERE ID=1";
iret = API_Delete_Sqlite(g_db, DeleteSql);
if(0 != iret)
{
return -1;
}
memset(&DataInfo, 0x00, sizeof(DataInfo));
iret = API_Select_Sqlite(g_db, SelectSql, SelectCb, (void *)&DataInfo);
if(0 != iret)
{
return -1;
}
printf("=========================select after delete==============================\n");
char *UpdateSql = "UPDATE PERSON_INFO SET NAME = 'Wahaha' WHERE ID=2;";
iret = API_Update_Sqlite(g_db, UpdateSql);
if(0 != iret)
{
return -1;
}
memset(&DataInfo, 0x00, sizeof(DataInfo));
iret = API_Select_Sqlite(g_db, SelectSql, SelectCb, (void *)&DataInfo);
if(0 != iret)
{
return -1;
}
printf("=========================select after update===============================\n");
sleep(1);
(void)API_Close_Sqlite(g_db);
return 0;
}
測試結果如下:
智能推薦
SQLite3的安裝與使用
SQLite3的安裝與使用 SQLite下載 我是64位機,下載下面的兩個解壓就好 然后解壓配置環境變量Path H:\java\SQLlite\sqlite-tools-win32-x86-3310100 我的安裝目錄是在這里,所以Path配置地址就是這個 測試使用 在dos界面中輸入sqlite3查看 輸入sqlite3,顯示下面內容說明成功 創建數據庫 生成DB文件 創建表 新增數據 Nav...
使用sqlite3動態庫
下載SQLite3動態庫及頭文件 頭文件 下載 sqlite-autoconf-3210000.tar.gz提取其中的sqlite3.h及sqlite3ext.h文件。 動態庫 下載sqlite-dll-win32-x86-3210000.zip(64位系統請下載sqlite-dll-win64-x64-3210000.zip)提取其中的sqlite3.dll及sqlite3.def文件備用。 生...
freemarker + ItextRender 根據模板生成PDF文件
1. 制作模板 2. 獲取模板,并將所獲取的數據加載生成html文件 2. 生成PDF文件 其中由兩個地方需要注意,都是關于獲取文件路徑的問題,由于項目部署的時候是打包成jar包形式,所以在開發過程中時直接安照傳統的獲取方法沒有一點文件,但是當打包后部署,總是出錯。于是參考網上文章,先將文件讀出來到項目的臨時目錄下,然后再按正常方式加載該臨時文件; 還有一個問題至今沒有解決,就是關于生成PDF文件...
電腦空間不夠了?教你一個小秒招快速清理 Docker 占用的磁盤空間!
Docker 很占用空間,每當我們運行容器、拉取鏡像、部署應用、構建自己的鏡像時,我們的磁盤空間會被大量占用。 如果你也被這個問題所困擾,咱們就一起看一下 Docker 是如何使用磁盤空間的,以及如何回收。 docker 占用的空間可以通過下面的命令查看: TYPE 列出了docker 使用磁盤的 4 種類型: Images:所有鏡像占用的空間,包括拉取下來的鏡像,和本地構建的。 Con...
猜你喜歡
requests實現全自動PPT模板
http://www.1ppt.com/moban/ 可以免費的下載PPT模板,當然如果要人工一個個下,還是挺麻煩的,我們可以利用requests輕松下載 訪問這個主頁,我們可以看到下面的樣式 點每一個PPT模板的圖片,我們可以進入到詳細的信息頁面,翻到下面,我們可以看到對應的下載地址 點擊這個下載的按鈕,我們便可以下載對應的PPT壓縮包 那我們就開始做吧 首先,查看網頁的源代碼,我們可以看到每一...
Linux C系統編程-線程互斥鎖(四)
互斥鎖 互斥鎖也是屬于線程之間處理同步互斥方式,有上鎖/解鎖兩種狀態。 互斥鎖函數接口 1)初始化互斥鎖 pthread_mutex_init() man 3 pthread_mutex_init (找不到的情況下首先 sudo apt-get install glibc-doc sudo apt-get install manpages-posix-dev) 動態初始化 int pthread_...
統計學習方法 - 樸素貝葉斯
引入問題:一機器在良好狀態生產合格產品幾率是 90%,在故障狀態生產合格產品幾率是 30%,機器良好的概率是 75%。若一日第一件產品是合格品,那么此日機器良好的概率是多少。 貝葉斯模型 生成模型與判別模型 判別模型,即要判斷這個東西到底是哪一類,也就是要求y,那就用給定的x去預測。 生成模型,是要生成一個模型,那就是誰根據什么生成了模型,誰就是類別y,根據的內容就是x 以上述例子,判斷一個生產出...
styled-components —— React 中的 CSS 最佳實踐
https://zhuanlan.zhihu.com/p/29344146 Styled-components 是目前 React 樣式方案中最受關注的一種,它既具備了 css-in-js 的模塊化與參數化優點,又完全使用CSS的書寫習慣,不會引起額外的學習成本。本文是 styled-components 作者之一 Max Stoiber 所寫,首先總結了前端組件化樣式中的最佳實踐原則,然后在此基...