sqlite3数据库

sqlits3 数据库名,创建一个数据库,并打开,有点像vim

.headers on .mode column可以让输出的更美观一点

//打开一个数据库文件

ret = sqlite3_open("student.db",&pDb);

if(ret != SQLITE_OK){

fprintf(stderr,"sqlite3_open failed! : %s\n",sqlite3_errmsg(pDb));

return -1;

}

错误码存放在pDb中所以还要调用sqlite3_errmsg

const char *sqlite3_errmsg(sqlite3*); 获得出错原因

//执行SQL语句

sprintf(tmpbuff,"select *from student;");

ret = sqlite3_exec(pDb,tmpbuff,callback,NULL,&perrmsg);

注意:1.callback回调函数(只有在select语句时会使用,其余SQL语句只需传入NULL),对找到的数据完成要完成的操作

2.perrmsg是申请空间的来的,所以出错必须要释放

int callback(void *arg, int colum,char **pcontent,char **ptitle);

参 数:

arg:sqlite3_exec给传的参数

column:找到的一条数据的列数(与SQL语句select后面选择的列数有关)

pcontent:指针数组的数组名(指向该条数据每一列字符串的首地址)

ptitle:指针数组的数组名(指向每一列名称字符串首地址的指针数组)

返回值:

成功必须返回0,不能返回其他

cpp 复制代码
#include <stdio.h>
#include <sqlite3.h>
typedef struct student{
    char name[32];
    char sex[4];
    int age;
    double score;
}stu_t;
/********************************************************
 * 函数名:callback
 * 功  能:
 *        处理找到的数据
 * 参  数:
 *      arg:sqlite3_exec给传的参数
 *      column:找到的一条数据的列数(与SQL语句select后面选择的列数有关)
 *      pcontent:指针数组的数组名(指向该条数据每一列字符串的首地址)
 *      ptitle:指针数组的数组名(指向每一列名称字符串首地址的指针数组)
 * 返回值:
 *       成功返回0 
 *       失败返回-1 
 * 注意事项:
 *      1.函数必须返回0,返回非0会sqlite3_exec出错
 *      2.每找到一条匹配的数据,则会调用一次callback,所以callback可能被调多次
 ********************************************************/

int callback(void *arg, int colum,char **pcontent,char **ptitle){
    int i = 0;
    for(i = 0; i < colum; i++){
        printf("%s = %10s           ",ptitle[i],pcontent[i]);
    }
    printf("\n");
    return 0;

}

int main(void){
    int ret = 0;
    sqlite3 *pDb = NULL;
    char *perrmsg = NULL;
    char tmpbuff[1024] = {0};
    int i = 0;

    stu_t s[3] = {
        {"张三","男",18,98.9},
        {"李四","男",10,89.8},
        {"王五","女",20,66.4},
    };

    //打开一个数据库文件
    ret = sqlite3_open("student.db",&pDb);
    if(ret != SQLITE_OK){
        fprintf(stderr,"sqlite3_open failed! : %s\n",sqlite3_errmsg(pDb));
        return -1;
    }

    //执行SQL语句
    //创建表
    sprintf(tmpbuff,"create table if not exists student(id integer primary key asc,name text,sex text,age integer,score double);");
    ret = sqlite3_exec(pDb,tmpbuff,NULL,NULL,&perrmsg);
    if(ret != SQLITE_OK){
        fprintf(stderr,"sqlite3_exec failed! :%s\n",perrmsg);
        sqlite3_free(perrmsg);
        sqlite3_close(pDb);
        return -1;
    }
    //插入数据
    for(i = 0; i < 3; i++){
        sprintf(tmpbuff,"insert into student values(NULL,\"%s\",\"%s\",%d,%lf);",s[i].name,s[i].sex,s[i].age,s[i].score);
        ret = sqlite3_exec(pDb,tmpbuff,NULL,NULL,&perrmsg);
        if(ret != SQLITE_OK){
            fprintf(stderr,"sqlite3_exec failed! :%s\n",perrmsg);
            sqlite3_free(perrmsg);
            sqlite3_close(pDb);
        }
    }
    //查询数据
    sprintf(tmpbuff,"select *from student;");
    ret = sqlite3_exec(pDb,tmpbuff,callback,NULL,&perrmsg);
     if(ret != SQLITE_OK){
        fprintf(stderr,"sqlite3_exec failed! :%s\n",perrmsg);
        sqlite3_free(perrmsg);
        sqlite3_close(pDb);
        return -1;
    }
    sqlite3_close(pDb);
    return 0;
}
相关推荐
orion579 小时前
Missing Semester Class1:course overview and introduction of shell
linux
先吃饱再说13 小时前
存储的进化:从 MySQL 到浏览器缓存,数据到底住在哪?
数据库
Nturmoils13 小时前
字段太多看不全,ksql 的展开模式和输出控制怎么用
数据库·后端
用户1204872216115 小时前
Linux驱动编译与加载
linux·嵌入式
Databend16 小时前
Agent 轨迹分析与归因的数据工程实践
大数据·数据库·agent
这个DBA有点耶16 小时前
SQL改写进阶:标量子查询的“隐形代价”与消除实战
数据库·mysql·架构
程序员老赵17 小时前
服务器文件不想 SFTP 上传?Docker 跑个 File Browser,浏览器就能管理
服务器·docker·开源
smallyoung17 小时前
数据库乐观锁深度解析:MySQL、PostgreSQL 实战 + Spring Boot 集成指南
数据库·mysql·postgresql
parade岁月17 小时前
MySQL JOIN解析:朴实无华但食之有味
数据库·后端
用户31693538118318 小时前
MySQL服务无法启动问题解决全记录
数据库