【sqlite的C语言访问接口】执行SQL语句的接口------sqlite3_exec回调函数的使用

简介: 【sqlite的C语言访问接口】执行SQL语句的接口------sqlite3_exec回调函数的使用

sqlite3_exec的函数原型

int sqlite3_exec(
sqlite3 *pDb,
const char *sql,
int (*callback)(void *arg, int col, char **str, char **name),
void *arg,
char **errmsg
)


参数说明:

pDb:打开数据库的句柄

sql:要执行的SQL语句

callback:回调函数,处理SQL语句执行的结果(查询操作)

一条结果调用一次该回调函数。(注:回调函数必须返回SQLITE_OK)

arg:exec的第四个参数

col:本条结果的字段数

str:记录字段值的数组

name:记录字段名的数组

arg:传递给回调函数的第一个参数

errmsg:错误信息


返回值

成功返回SQLITE_OK,否则打开失败

例子

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

//查询的回调函数
int sql_callback(void *arg,int col,char **str,char **name)
{
  int i;
  for(i=0;i<col;i++){
    printf("%s:%s ",name[i],str[i]);
  }
  printf("\n");
  
  //返回SQLITE_OK表示执行成功
  return SQLITE_OK;
}

int main()
{
  sqlite3 *pDb =  NULL;
  //1.打开数据库
  int res = sqlite3_open("./test.db",&pDb);
  if(res!=SQLITE_OK){
    printf("数据库打开失败!\n");
    return -1;
  }
  printf("数据库打开成功!\n");
  
  int id = 0;
  char sql[128] = {};
  printf("请输入要查找的id:");
  scanf("%d",&id);
  sprintf(sql,"select * from mytbl where id=%d;",id);
  
  //构建SQL语句
  //const char *sql = "create table if not exists mytbl(id int unique,name ntext,age int);";//建表
  //const char *sql = "insert into mytbl values(1,'宋江',36);";//插入
  //const char *sql = "insert into mytbl values(2,'李逵',24);";//插入
  //const char *sql = "insert into mytbl values(3,'潘金莲',21);";//插入
  //const char *sql = "select * from mytbl;";//查询
  
  
  //2.执行SQL语句
  res = sqlite3_exec(pDb,sql,sql_callback,NULL,NULL);
  if(res!=SQLITE_OK){
    printf("sql语句执行失败!\n");
    return -1;
  }
  printf("sql语句执行成功!\n");
  
  //3.关闭数据库
  sqlite3_close(pDb);
  return 0;
}


相关文章
|
1月前
|
开发框架 前端开发 .NET
七天.NET 8操作SQLite入门到实战 - (3)第七天Blazor学生管理页面编写和接口对接
七天.NET 8操作SQLite入门到实战 - (3)第七天Blazor学生管理页面编写和接口对接
|
26天前
|
SQL 安全 Java
访问者模式问题之在上面的 SQL 结构定义中, sealed 接口的作用如何理解
访问者模式问题之在上面的 SQL 结构定义中, sealed 接口的作用如何理解
|
28天前
|
网络协议 NoSQL 网络安全
【Azure 应用服务】由Web App“无法连接数据库”而逐步分析到解析内网地址的办法(SQL和Redis开启private endpoint,只能通过内网访问,无法从公网访问的情况下)
【Azure 应用服务】由Web App“无法连接数据库”而逐步分析到解析内网地址的办法(SQL和Redis开启private endpoint,只能通过内网访问,无法从公网访问的情况下)
|
29天前
|
传感器 IDE 编译器
C语言与硬件接口
C语言与硬件接口
44 0
|
29天前
|
SQL 关系型数据库 数据库
C语言与数据库:使用C语言操作SQLite等数据库。
C语言与数据库:使用C语言操作SQLite等数据库。
17 0
|
30天前
|
SQL 机器学习/深度学习 开发工具
【机器学习 Azure Machine Learning】Azure Machine Learning 访问SQL Server 无法写入问题 (使用微软Python AML Core SDK)
【机器学习 Azure Machine Learning】Azure Machine Learning 访问SQL Server 无法写入问题 (使用微软Python AML Core SDK)
|
30天前
|
SQL 数据库 Windows
【应用服务 App Service】当使用EntityFrameWorkCore访问Sql Server数据库时,在Azure App Service会出现Cannot create a DbSet for ** because this type is not included in the model for the context的错误
【应用服务 App Service】当使用EntityFrameWorkCore访问Sql Server数据库时,在Azure App Service会出现Cannot create a DbSet for ** because this type is not included in the model for the context的错误
|
3月前
|
SQL 关系型数据库 数据库
nacos 2.2.3版本 查看配置文件的历史版本的接口 是针对MySQL数据库的sql 改成postgresql后 sql语句报错 该怎么解决
在Nacos 2.2.3中切换到PostgreSQL后,执行配置文件历史版本分页查询出错,因`LIMIT 0, 10`语法不被PostgreSQL支持,需改为`LIMIT 10 OFFSET 0`。仅当存在历史版本时报错。解决方案是调整查询SQL以兼容PostgreSQL语法。
|
4月前
|
存储 数据库连接 数据库
Android数据存储:解释SQLite数据库在Android中的使用。
Android数据存储:解释SQLite数据库在Android中的使用。
65 0
|
3月前
|
数据库 Android开发 数据安全/隐私保护
在 Android Studio 中结合使用 SQLite 数据库实现简单的注册和登录功能
在 Android Studio 中结合使用 SQLite 数据库实现简单的注册和登录功能
157 2