sqlite安装与封装后编译

========================安装sqlite=================
官网下载安装包*.tar.gz格式
./configure --prefix=/usr/server/sqlit 安装包目录
make
make install

测试安装成功 sqlite3 test.db 

sudo yum install sqlite-devel  (我没安装也成,-devel 继续开发的开发包,有些头文件之类的)

=======================编译====================
编译时 加入参数 -I /usr/server/sqlite/include
-L /usr/server/sqlite/lib
如果编译为静态 则 -static

gcc db.c test.c -o main -I /usr/server/sqlite/include -L /usr/server/sqlite/lib -lsqlite3

 =======================源程序=================

网上找的

test.c

/*****************
对sqlite封装函数的测试
名称:test.c
*********************************/
#include <stdio.h>
#include "db.h"
int main(int argc, char *argv[]) 
{
    char *dbname="dtmsg.db";
    sqlite3 *db;
    int iReturn=DbOpenDatabase(dbname,&db);
    printf("-0->打开返回值%d<---
",iReturn);
    if(iReturn==1)
    {
        //新建table t_msg 
        char *sql="create table t_msg(msg,msg_buff)";
        printf("-1-->新建sql语句 %s<--
",sql);
        int cReturn=DbChangeDataRecord(db,sql);
        if (cReturn==-1)
            return 0;
        printf("-1-->执行新建返回值 %d<--
",cReturn);
        //增加、修改、删除
        sql="insert into t_msg values ('test','测试信息11')";
        //char * sql="update t_msg set msg_buff='测试信息2222222' where msg='test'";
        //char * sql="delete from t_msg where msg='test'";
        printf("-2-->%s<--
",sql);
        int iSqlRet=DbChangeDataRecord(db,sql);
        printf("-3-->%d<--
",iSqlRet);
        
        //查询
        sql="select * from t_msg";
        char **dbResult;
        int nRow,nColumn,index;
        int i,j;
        int iQret=DbExecuteQuerySql(db,sql,&dbResult,&nRow,&nColumn);
        if(iQret==1)
        {
            index=nColumn;
            for( i = 0; i < nRow ; i++ )
            {
                printf( "第====================> %d 条记录
", i+1 );
                for( j = 0 ; j < nColumn; j++ )
                {
                    printf( "字段名:%s ==>字段值:%s
", dbResult[j],dbResult[index]?dbResult[index]:"" );
                    ++index; 
                }
            }
        }
        //不论数据库查询是否成功,都释放 char** 查询结果,使用 sqlite 提供的功能来释放
        //sqlite3_free_table( dbResult );
        DbFreeResources(dbResult);
    }
    DbCloseDatabase(db);
    return 1;
}
View Code

db.c

/*************************************************
    File name:         db.c
    Description:    C语言查询sqlite3数据库的基本操作封装
    Others:         需要sqlite3的动态库 gcc -o db db.c -lsqlite3 
*************************************************/
#include "db.h"
                       
/*************************************************
    Function:       DbOpenDatabase
    Description:    打开sqlite3数据库
    Input:          strDbName:数据库名称;hDbCon:sqlite3的句柄
    Output:         无
    Return:         1,数据库打开成功;0,数据库打开失败
    Others:         无
*************************************************/
int DbOpenDatabase(char *strDbName, sqlite3 **hDbCon)
{
    int iRet = sqlite3_open(strDbName,hDbCon); //打开数据库
    if(iRet != SQLITE_OK)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}
/*************************************************
    Function:       DbCloseDatabase
    Description:    关闭sqlite3数据库
    Input:          hDbCon:sqlite3的句柄
    Output:         无
    Return:         无
    Others:         无
*************************************************/
void DbCloseDatabase(sqlite3 *hDbCon)
{
    sqlite3_close(hDbCon);
}
/*************************************************
    Function:       DbChangeDataRecord
    Description:    向数据库进行插入、修改、删除操作
    Input:          hDbCon:sqlite3的句柄; strSql:sql语句;
    Output:         无
    Return:         -1,插入(或修改或删除)失败; 
                    大于-1,执行sql后,返回响应的行数
    Others:         无
*************************************************/
int DbChangeDataRecord(sqlite3 *hDbCon,char *strSql)
{
    char *pErrMsg = 0;  //错误信息
    int iRet = sqlite3_exec(hDbCon, strSql, NULL, NULL, &pErrMsg);
    if(iRet != SQLITE_OK)
    {
        printf("查询失败,错误码==>:%d,错误原因==>:%s
", iRet, pErrMsg);
        return -1;
    }
    else
    {
        return iRet;
    }    
}

/*************************************************
    Function:       DbExecuteQuerySql
    Description:    查询数据库中的记录集
    Input:          hDbCon:sqlite3的句柄;strSql:sql语句;
    Output:         strResult:结果集;iRow:行;iColumn:列;
    Return:         1,查询成功;0,查询失败
    Others:         无
*************************************************/ 
int DbExecuteQuerySql(sqlite3 *hDbCon,
                      char *strSql,
                      char ***strResult,
                      int *iRow,
                      int *iColumn)
{
    char *pErrMsg = NULL;         //错误日志
    int iReturn = 0;
    int iResult = sqlite3_get_table( hDbCon,strSql, strResult, iRow, iColumn, &pErrMsg );
    if( SQLITE_OK == iResult )
    {
        iReturn = 1;            
    }
    else
    {
        iReturn = 0;
    }
    return iReturn;
}
/*************************************************
    Function:       DbFreeResources
    Description:    不论数据库查询是否成功,都释放 char** 
                    查询结果,使用 sqlite 提供的功能来释放
    Input:          strResult:结果集
    Output:         无
    Return:         无
    Others:         无
*************************************************/ 
void DbFreeResources(char **strResult)
{
    sqlite3_free_table( strResult );
}
View Code

db.h

/*************************************************
    File name:         db.h
    Description:    C语言查询sqlite3数据库的基本操作封装
    Others:         无
*************************************************/

#ifndef DB_H
#define DB_H

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

/*************************************************
    Function:       DbOpenDatabase
    Description:    打开sqlite3数据库
    Input:          strDbName:数据库名称;hDbCon:sqlite3的句柄
    Output:         无
    Return:         1,数据库打开成功;0,数据库打开失败
    Others:         无
*************************************************/
int DbOpenDatabase(char *strDbName, sqlite3 **hDbCon);

/*************************************************
    Function:       DbCloseDatabase
    Description:    关闭sqlite3数据库
    Input:          hDbCon:sqlite3的句柄
    Output:         无
    Return:         无
    Others:         无
*************************************************/
void DbCloseDatabase(sqlite3 *hDbCon);

/*************************************************
    Function:       DbChangeDataRecord
    Description:    向数据库进行插入、修改、删除操作
    Input:          hDbCon:sqlite3的句柄; strSql:sql语句;
    Output:         无
    Return:         -1,插入(或修改或删除)失败; 
                    大于-1,执行sql后,返回响应的行数
    Others:         无
*************************************************/ 
int DbChangeDataRecord(sqlite3 *hDbCon,char *strSql);

/*************************************************
    Function:       DbExecuteQuerySql
    Description:    查询数据库中的记录集
    Input:          hDbCon:sqlite3的句柄;strSql:sql语句;
    Output:         strResult:结果集;iRow:行;iColumn:列;
    Return:         1,查询成功;0,查询失败
    Others:         无
*************************************************/ 
int DbExecuteQuerySql(sqlite3 *hDbCon,
                      char *strSql,
                      char ***strResult,
                      int *iRow,
                      int *iColumn);

/*************************************************
    Function:       DbFreeResources
    Description:    不论数据库查询是否成功,都释放 char** 
                    查询结果,使用 sqlite 提供的功能来释放
    Input:          strResult:结果集
    Output:         无
    Return:         无
    Others:         无
*************************************************/  
void DbFreeResources(char **strResult);

#endif
View Code
/*    Others:         需要sqlite3的动态库  gcc -o db db.c ./libsqlite3.so //还没弄懂
                                      或 gcc -o db db.c -lsqlite3 //还没弄懂
*************************************************/
#include <stdio.h>
#include <string.h>
#include <stdlib.h>
#include <sqlite3.h>               

int DbOpenDatabase(char *strDbName, sqlite3 **hDbCon)
{
    int iRet = sqlite3_open(strDbName,hDbCon); //打开数据库
    if(iRet != SQLITE_OK)
    {
        return 0;
    }
    else
    {
        return 1;
    }
}

void DbCloseDatabase(sqlite3 *hDbCon)
{
    sqlite3_close(hDbCon);
}

int DbChangeDataRecord(sqlite3 *hDbCon,char *strSql)
{
    char *pErrMsg = 0;  //错误信息
    int iRet = sqlite3_exec(hDbCon, strSql, NULL, NULL, &pErrMsg);
    if(iRet != SQLITE_OK)
    {
        //printf("查询失败,错误码==>:%d,错误原因==>:%s
", ret, errmsg );
        return -1;
    }
    else
    {
        return iRet;
    }    
}

int DbExecuteQuerySql(sqlite3 *hDbCon,
                      char *strSql,
                      char ***strResult,
                      int *iRow,
                      int *iColumn)
{
    char *pErrMsg = NULL;         //错误日志
    int iReturn = 0;
    int iResult = sqlite3_get_table( hDbCon,strSql, strResult, iRow, iColumn, &pErrMsg );
    if( SQLITE_OK == iResult )
    {
        iReturn = 1;            
    }
    else
    {
        iReturn = 0;
    }
    return iReturn;
}

void DbFreeResources(char **strResult)
{
    sqlite3_free_table( strResult );
}

int DbSqliteCallBack(sqlite3 * hDbCon,const char *strSql,int (*QueryResultBack)())
{
    char *pErrMsg;
    int iRetDb = 0;
    iRetDb = sqlite3_exec(hDbCon,strSql,QueryResultBack,NULL,&pErrMsg);
    if(iRetDb != SQLITE_OK)
    {
        return 0;
    }
    return 1;
}

//回调函数例子测试
/*static int QueryResultBack(void *para,int iColumn,char **cValue,char **cColumnValueName)
{
    int iCn;
    for(iCn = 0; iCn < iColumn; iCn++)
    {
        //printf( "%s = %s
" , cColumnValueName[i], cValue[i] ? cValue[i] : "NULL" ); 
        printf( "%s = %s
" , cColumnValueName[i], cValue[i]);
    }
    return 0;
}*/

int main(int argc, char *argv[]) 
{
    char *dbname="dtmsg.db";
    sqlite3 *db;
    int iReturn=DbOpenDatabase(dbname,&db);
    printf("-1->%d<---
",iReturn);
    if(iReturn==1)
    {
        //增加、修改、删除
        char *sql="insert into t_msg (msg,msg_buff) values ('test','测试信息11')";
        //char * sql="update t_msg set msg_buff='测试信息2222222' where msg='test'";
        //char * sql="delete from t_msg where msg='test'";
        printf("-2-->%s<--
",sql);
        int iSqlRet=DbChangeDataRecord(db,sql);
        printf("-3-->%d<--
",iSqlRet);
        
        //查询
        //char *sql="select * from t_msg";
        sql="select * from t_msg";
        char **dbResult;
        int nRow,nColumn,index;
        int i,j;
        int iQret=DbExecuteQuerySql(db,sql,&dbResult,&nRow,&nColumn);
        if(iQret==1)
        {
            index=nColumn;
            for( i = 0; i < nRow ; i++ )
            {
                printf( "第====================> %d 条记录
", i+1 );
                for( j = 0 ; j < nColumn; j++ )
                {
                    printf( "字段名:%s ==>字段值:%s
", dbResult[j],dbResult[index]?dbResult[index]:"" );
                    ++index; 
                }
            }
        }
        //不论数据库查询是否成功,都释放 char** 查询结果,使用 sqlite 提供的功能来释放
        //FreeResources(dbResult);
        sqlite3_free_table( dbResult );
    }
    DbCloseDatabase(db);
    return 1;
}
原文地址:https://www.cnblogs.com/qbmiller/p/3852747.html