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;
}
相关推荐
一 乐21 小时前
健身达人小程序|基于java+vue健身达人小程序的系统设计与实现(源码+数据库+文档)
java·前端·数据库·vue.js·spring boot·小程序
q***787821 小时前
PostgreSQL的备份方式
数据库·postgresql
q***13341 天前
Linux(CentOS)安装 Nginx
linux·nginx·centos
Austindatabases1 天前
SQLite3 如果突发断电,关机,数据会丢还是不会丢?
数据库·sqlite
摘星编程1 天前
openGauss 快速上手:CentOS 环境下单机部署完整指南
linux·运维·centos
青春:一叶知秋1 天前
【Redis存储】List列表
数据库·redis·缓存
哈里谢顿1 天前
使用kvm创建一台虚拟机
linux
爆更小哇1 天前
MyBatis的TypeHandler :优雅地实现数据加密与解密
数据库·后端·mybatis
hanyi_qwe1 天前
文本三剑客--awk
linux·运维·服务器
likuolei1 天前
Eclipse 创建 Java 接口
java·数据库·eclipse