c学习:sqlite3数据库操作

目录

获取sqlite3源码

c调用步骤

常用接口函数说明

[例子 打开数据库,新建表,插入数据,查询数据,关闭数据库](#例子 打开数据库,新建表,插入数据,查询数据,关闭数据库)

查询数据需要在回调函数中获取


获取sqlite3源码

先下载c的sqlite3源码,https://www.sqlite.org/index.html

解压后将sqlite3.c和sqlite3.h放到自己的项目里

查看数据库文件

可以用sqliteexpert打开查看,下载请看

SQLite administration | SQLite Experthttps://www.sqliteexpert.com/download.html

c调用步骤

  1. 添加头文件
  2. 打开数据库文件,如果数据库文件不存在,则创建
  3. 创建表
  4. 增删改查
  5. 关闭数据库文件

常用接口函数说明

SQLite -- C/C++ | 菜鸟教程 (runoob.com)https://www.runoob.com/sqlite/sqlite-c-cpp.html

  • sqlite3_open(const char *filename, sqlite3 **ppDb)
    • 函数作用:打开数据库文件,如果该数据库文件不存在则创建
    • filename:你要打开的那个的数据库文件的路径名
    • ppDb:数据库的文件句柄(类似之前所学的文件描述符)
    • 返回值,成功返回0,否则非0
  • sqlite3_exec(sqlite3*, const char *sql, sqlite_callback, void *data, char **errmsg)
    • 函数作用:执行sql语句
    • sqlite3:数据库文件句柄
    • sql:sql语句
    • sqlite_callback:回调函数指针,将查询的结果存储到这里
    • data:传入回调函数中得数据
    • errmsg:错误原因
    • 返回值,成功返回SQLITE_OK,否则非SQLITE_OK
  • sqlite3_close(sqlite3*)
    • 函数作用:关闭数据库文件
  • int (*callback)(void*arg,int,char**,char**)
    • arg:是sqlite3_exec第四个参数传进来得值
    • int:数据库得列数,也就是多少列
    • char**:每一行的信息,char data2256
    • char**:表示每列的标题名

例子 打开数据库,新建表,插入数据,查询数据,关闭数据库

复制代码
    //创建一个sqlite3空的句柄指针
    sqlite3 *ppDb = NULL;//数据库文件的句柄
    //打开数据库文件
    int ret = sqlite3_open("test.db",&ppDb);
    if(ret == 0)
    {
        printf("open success\n");
    }
    else{
        printf("open error:%s\n",sqlite3_errmsg(ppDb));
        return ;
    }
    
    //创建表
    const char *sql = "create table  if not exists staff(name text,age int);";    
    char *errmsg;//错误信息
    //执行
    ret = sqlite3_exec(ppDb,sql,NULL,NULL,&errmsg);
    if(ret != SQLITE_OK)
    {
        printf("create table error:%s\n",errmsg);
    }

    //插入数据
    //准备sql语句
    const char *insertSql = "insert into staff values('zhang3',22);";
    //执行
    ret = sqlite3_exec(ppDb,insertSql,NULL,NULL,&errmsg);
    if(ret != SQLITE_OK)
    {
        printf("insert error:%s\n",errmsg);
    }

    //查询
    //准备sql语句
    const char*selectSql = "select * from staff;";
    //执行
    ret = sqlite3_exec(ppDb,selectSql,callback,NULL,&errmsg);
    if(ret != SQLITE_OK)
    {
        printf("select error:%s\n",errmsg);
    }
    

    //关闭数据库文件
    sqlite3_close(ppDb);

查询数据需要在回调函数中获取

复制代码
查询到一行数据调用一次
int callback(void*arg,int column,char**data,char**feild)
{
    //name:zhang3  age:22
    //c++  qt
    qDebug()<<feild[0]<<":"<<data[0] <<"   "<<feild[1]<<":"<<data[1];
    //c
    printf("%s:%s %s:%s\n", feild[0], data[0], feild[1], data[1]);
    return 0;//返回值一定要写
}
相关推荐
fox_lht2 小时前
15.3.改进我们之前的输入、输出项目
开发语言·后端·学习·rust
chase。3 小时前
【学习笔记】SimpleVLA-RL:通过强化学习扩展 VLA 训练
笔记·学习
C语言小火车3 小时前
什么时候用智能指针?什么时候用裸指针?
c语言·c++·学习·指针
AOwhisky4 小时前
Redis 学习笔记(第一期):概述、安装配置与核心理论
运维·数据库·redis·笔记·学习·云计算
ytttr8734 小时前
C# 定时数据库备份工具
开发语言·数据库·c#
睡不醒男孩0308234 小时前
自建 Prometheus+Grafana 与 CLUP 深度监控 PG 集群有什么区别?
数据库·oracle
AOwhisky4 小时前
Redis 学习笔记(第四期):高可用与集群(哨兵 + Cluster + 容器化)
linux·运维·数据库·redis·笔记·学习·缓存
skywalk81634 小时前
言知项目后续方向建议
开发语言·学习·编程
努力写A题的小菜鸡4 小时前
PyTorch 图像预处理 transforms 与 TensorBoard 可视化 (自己学习记录)
人工智能·pytorch·学习
一尘之中4 小时前
从概念到实践:软件架构核心知识梳理
学习·ai写作