欢迎来到尧图网

客户服务 关于我们

您的位置:首页 > 教育 > 高考 > sqlite3数据库

sqlite3数据库

2025/9/15 0:35:25 来源:https://blog.csdn.net/m0_61965705/article/details/141436714  浏览:    关键词: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,不能返回其他

#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;
}

版权声明:

本网仅为发布的内容提供存储空间,不对发表、转载的内容提供任何形式的保证。凡本网注明“来源:XXX网络”的作品,均转载自其它媒体,著作权归作者所有,商业转载请联系作者获得授权,非商业转载请注明出处。

我们尊重并感谢每一位作者,均已注明文章来源和作者。如因作品内容、版权或其它问题,请及时与我们联系,联系邮箱:809451989@qq.com,投稿邮箱:809451989@qq.com