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;
}
相关推荐
Mintimate1 分钟前
云服务器 Linux 手动 DD 安装第三方 Linux 发行版:原理与实战
linux·运维·服务器
RussellFans12 分钟前
Linux 环境配置
linux·运维·服务器
网硕互联的小客服19 分钟前
503 Service Unavailable:服务器暂时无法处理请求,可能是超载或维护中如何处理?
服务器·git·github
高冷的肌肉码喽1 小时前
Linux-进程间的通信
linux·运维·服务器
乖乖是干饭王1 小时前
Linux系统编程中的_GNU_SOURCE宏
linux·运维·c语言·学习·gnu
敖云岚1 小时前
【Redis】分布式锁的介绍与演进之路
数据库·redis·分布式
jekc8681 小时前
禅道18.2集成LDAP
linux·运维·服务器
weixin_434936281 小时前
k8S 命令
linux·容器·kubernetes
weixin_307779132 小时前
Linux下GCC和C++实现统计Clickhouse数据仓库指定表中各字段的空值、空字符串或零值比例
linux·运维·c++·数据仓库·clickhouse
LUCIAZZZ2 小时前
HikariCP数据库连接池原理解析
java·jvm·数据库·spring·springboot·线程池·连接池