使用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;
}

测试结果如下:
在这里插入图片描述
在这里插入图片描述

长按二维码关注【 嵌入式C部落】,获取更多编程资料及精华文章

猜你喜欢

转载自blog.csdn.net/weixin_35933684/article/details/103489249
今日推荐