sqlite3_exec及其回调函数

本文介绍了如何利用sqlite3_exec函数及其回调函数检查SQLite数据库中是否存在指定的表。通过执行SQL查询并自定义回调函数处理结果,程序可以判断test_table是否在test.db数据库中,并输出相应的提示信息。

一)sqlite3_exec

1.头文件和函数原型

#include "sqlite3.h"

int sqlite3_exec(sqlite3*, const char *sql, int (*callback)(void*para,int argc,char**argv,char**argv_name), void *para,char **errmsg)

2.说明
第一个参数sqlite3*是打开的数据库;
第二个参数const char *sql是要执行的sql语句;
第三个参数int (*callback)(void*para,int argc,char**argv,char**argv_name)是回调函数;
第四个参数void *para是传递给回调函数的参数
第五个参数char **errmsg是如果sqlite3_exec函数执行发生错误返回的错误信息;
执行成功返回SQLITE_OK (0),否则返回其他值。

二)sqlite3_exec回调函数

回调函数是用户自定义的,格式如下:

int callback(void*para,int argc,char**argv,char**argv_name)
{
	/*回调函数功能*/
}

说明:
第一个参数void*para是sqlite3_exec传给回调函数的参数;
第二个参数int argc是执行sqlite3_exec的sql语句后sqlite3的shell环境打印的字段数目;
第三个参数char**argv是字段的值(字符串类型);
第四个参数char**argv_name是字段的名称。

三)判断数据库中是否存在表

在sqlite3的shell环境中执行下面的命令和sql语句可以看到test.db数据库中存在表test_table。
在这里插入图片描述
现在我们用sqlite3_exec及其回调函数来判断test_table是否存在于test.db数据库中

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

#define MAX_BUF_SIZE 1024

int callback(void *para, int argc, char **argv, char **argv_name)
{
    int i;

    for(i=0; i<argc; i++)
        *(int *)para = atoi(argv[i]);

    return 0;
}

int main(int argc, char *argv[])
{
    sqlite3             *db = NULL;
    char                *zErrMsg = NULL;
    int                 result = -1;
    char                sql_ifexis[MAX_BUF_SIZE];
    

    
    if(sqlite3_open("test.db", &db) != SQLITE_OK)
    {
        printf("can't open database: %s\n", sqlite3_errmsg(db));
        return -1;
    }

    zErrMsg = NULL;
    
    memset(sql_ifexis, 0, sizeof(sql_ifexis));
    snprintf(sql_ifexis, sizeof(sql_ifexis), "SELECT count(*) FROM sqlite_master WHERE type='table' AND name='%s';", "test_table");
    sqlite3_exec(db, sql_ifexis, callback, &result, NULL);
    if(result == 1)
    {
        printf("table existence!\n");
    }
    else if(result == 0)
    {
        printf("table not existence!\n");
    }

    return 0;
}

程序运行结果:
在这里插入图片描述

void print_db_info(sqlite3 *db) { const char *sql = NULL; int rc = 0; char *zErrMsg = 0; if (!db) { printf(“Error: db pointer is NULL\n”); return; } BOOL flag = false; printf(“=== SQLite Database Connection Info ===\n”); // 1. 数据库文件名(通过 sqlite3_db_filename) const char *dbname = "main"; const char *filename = sqlite3_db_filename(db, dbname); if (filename) { printf("Database file: %s\n", filename); } else { printf("Database file: :memory: or unknown\n"); } // 2. 数据库状态(是否打开、只读等) int readonly = sqlite3_db_readonly(db, dbname); printf("Main database readonly: %s\n", readonly ? "yes" : "no"); // 3. 错误码和错误消息 int err_code = sqlite3_errcode(db); const char *err_msg = sqlite3_errmsg(db); printf("Last error code: %d\n", err_code); printf("Last error message: %s\n", err_msg); // 4. 库版本信息 printf("SQLite version: %s\n", sqlite3_libversion()); printf("Compiler: %s\n", sqlite3_sourceid()); // 5. 编译选项(显示启用的功能) printf("Compile options: "); const char *compile_opt; for (int i = 0; (compile_opt = sqlite3_compileoption_get(i)) != 0; i++) { if (i > 0) printf(", "); printf("%s", compile_opt); } printf("\n"); // 6. 当前连接上的 SQL 函数/聚合数量(可选调试信息) printf("Total number of functions: %d\n", sqlite3_limit(db, SQLITE_LIMIT_FUNCTION_ARG, -1)); // 7. 各种性能统计(页数、缓存命中等) int hit_count, miss_count; int dummy; // 必须存在,但 SQLite 不使用它的输出值(设为非0即可获取数据) sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_HIT, &hit_count, &dummy, 0); sqlite3_db_status(db, SQLITE_DBSTATUS_CACHE_MISS, &miss_count, &dummy, 0); printf("Cache hit: %d, miss: %d\n", hit_count, miss_count); flag = is_table_exist(db,"tAudioInfo"); printf("The value of flag is: %d\n", flag); sql = "SELECT name FROM sqlite_master WHERE type=&#39;table&#39; ORDER BY name;"; rc = sqlite3_exec(db, sql, callback, 0, &zErrMsg); if( rc != SQLITE_OK ) { fprintf(stderr, "SQL error: %s\n", zErrMsg); sqlite3_free(zErrMsg); } else { fprintf(stdout, "Operation done successfully\n"); } printf("========================================\n"); }这是我写的打印数据库句柄db的信息的函数,给我加上打印所有数据库名和文件路径的函数,其他的不要改变
最新发布
09-30
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值