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;
}
相关推荐
Zzzzmo_7 分钟前
【MySQL】JDBC(含settings.xml文件配置/配置国内镜像以及pom.xml文件修改)
数据库·mysql
hjxu201623 分钟前
【OpenClaw 龙虾养成笔记一】在远程服务器,使用Docker安装OpenClaw
服务器·笔记·docker
todoitbo1 小时前
用虚拟局域网打通 Win/Mac/Linux 三端:跨设备协作的实用方案
linux·运维·macos
源远流长jerry1 小时前
RDMA 基本操作类型详解:从双端通信到单端直访
linux·网络·tcp/ip·ip
FirstFrost --sy1 小时前
MySQL内置函数
数据库·mysql
2401_879693871 小时前
将Python Web应用部署到服务器(Docker + Nginx)
jvm·数据库·python
reembarkation1 小时前
光标在a-select,鼠标已经移出,下拉框跟随页面滚动
java·数据库·sql
eggwyw2 小时前
MySQL-练习-数据汇总-CASE WHEN
数据库·mysql
星轨zb2 小时前
通过实际demo掌握SpringSecurity+MP中的基本框架搭建
数据库·spring boot·spring security·mp
Sylvia-girl2 小时前
Linux下的基本指令1
linux·运维·服务器