德阳建设网站的公司,上海 网站制作,怎么自己做网站备案,沧州商城网站建设sqlite3编程接口非常多#xff0c;对于初学者来说#xff0c;我们暂时只需要掌握常用的几个函数#xff0c;其他函数自然就知道如何使用了。
数据库
本篇假设数据库为my.db,有数据表student。
nonamescore4嵌入式开发爱好者89.0
创建表格语句如下#xff1a;
CREATE T…sqlite3编程接口非常多对于初学者来说我们暂时只需要掌握常用的几个函数其他函数自然就知道如何使用了。
数据库
本篇假设数据库为my.db,有数据表student。
nonamescore4嵌入式开发爱好者89.0
创建表格语句如下
CREATE TABLE IF NOT EXISTS student (no integer primary key, name text, score real);常用函数
sqlite3_open
int sqlite3_open(char *path, sqlite3 **db)
功能打开sqlite数据库
参数path 数据库文件路径db 指向sqlite句柄的指针后面对数据库所有的操作都要依赖这个句柄
返回值成功返回0失败返回错误码(非零值)sqlite3_close
int sqlite3_close(sqlite3 *db);
功能关闭sqlite数据库
返回值成功返回0失败返回错误码const char *sqlite3_errmsg(sqlite3 *db);
功能打印错误信息
返回值返回错误信息不使用回调函数执行SQL语句
sqlite3_get_table
int sqlite3_get_table(sqlite3 *db, const char *sql, char ***resultp, int*nrow, int *ncolumn, char **errmsg);
功能执行SQL操作
参数db数据库句柄sqlSQL语句resultp用来指向sql执行结果的指针nrow满足条件的记录的数目ncolumn每条记录包含的字段数目errmsg错误信息指针的地址
返回值成功返回0失败返回错误码举例
下面比如我们要显示student表中所有的数据信息我们就可以利用sqlite3_get_table执行语句
select * from student实现代码如下
void do_show_sample(sqlite3 *db){char **result, *errmsg;int nrow, ncolumn, i, j, index;if (sqlite3_get_table(db, select * from student, result, nrow, ncolumn, errmsg) ! 0){printf(error : %s\n, errmsg);sqlite3_free(errmsg);}index ncolumn;for (i0; inrow; i){for (j0; jncolumn; j){printf(%-8s : %-8s\n, result[j], result[index]); index;}printf(************************\n);}sqlite3_free_table(result);return;}假定当前的表格的数据信息如下
nonamescore4一口Linux77.05一口peng88.06一口wang99.07一口网66.0
关于这个函数中出现的这些参数的具体含义我们可以见下图
sqlite3编程接口非常多对于初学者来说我们暂时只需要掌握常用的几个函数其他函数自然就知道如何使用了。
数据库
本篇假设数据库为my.db,有数据表student。
nonamescore4一口Linux89.0
创建表格语句如下
CREATE TABLE IF NOT EXISTS student (no integer primary key, name text, score real);常用函数
sqlite3_open
int sqlite3_open(char *path, sqlite3 **db)
功能打开sqlite数据库
参数path 数据库文件路径db 指向sqlite句柄的指针
返回值成功返回0失败返回错误码(非零值)sqlite3_close
int sqlite3_close(sqlite3 *db);
功能关闭sqlite数据库
返回值成功返回0失败返回错误码const char *sqlite3_errmsg(sqlite3 *db);
功能打印错误信息
返回值返回错误信息不使用回调函数执行SQL语句
sqlite3_get_table
int sqlite3_get_table(sqlite3 *db, const char *sql, char ***resultp, int*nrow, int *ncolumn, char **errmsg);
功能执行SQL操作
参数db数据库句柄sqlSQL语句resultp用来指向sql执行结果的指针nrow满足条件的记录的数目ncolumn每条记录包含的字段数目errmsg错误信息指针的地址
返回值成功返回0失败返回错误码举例
下面比如我们要显示student表中所有的数据信息我们就可以利用sqlite3_get_table执行语句
select * from student实现代码如下
void do_show_sample(sqlite3 *db){char **result, *errmsg;int nrow, ncolumn, i, j, index;if (sqlite3_get_table(db, select * from student, result, nrow, ncolumn, errmsg) ! 0){printf(error : %s\n, errmsg);sqlite3_free(errmsg);}index ncolumn;for (i0; inrow; i){for (j0; jncolumn; j){printf(%-8s : %-8s\n, result[j], result[index]); index;}printf(************************\n);}sqlite3_free_table(result);return;}假定当前的表格的数据信息如下
nonamescore4一口Linux77.05一口peng88.06一口wang99.07一口网66.0
关于这个函数中出现的这些参数的具体含义我们可以见下图 在这里插入图片描述
由上图可知代码中
ncolumn 3
nrow 5
result 指向所有的结果组成的字符串数组
各个具体字符串的下标图上已经标明。结合此图再去理解代码就很容易理解代码的实现原理。
使用回调函数执行SQL语句
sqlite3_exec
typedef int (*sqlite3_callback)(void *, int, char **, char **);int sqlite3_exec(sqlite3 *db, const char *sql, sqlite3_callback callback, void *, char **errmsg);
功能执行SQL操作
参数db数据库句柄sqlSQL语句就是我们前面两章用于操作表的增删改查语句callback回调函数errmsg错误信息指针的地址
返回值成功返回0失败返回错误码回调函数
typedef int (*sqlite3_callback)(void *para, int f_num, char **f_value, char **f_name);
功能每找到一条记录自动执行一次回调函数
参数para传递给回调函数的参数f_num记录中包含的字段数目f_value包含每个字段值的指针数组f_name包含每个字段名称的指针数组
返回值成功返回0失败返回-1举例
sqlite3 *db;
char *errmsg**resultp;int callback(void *para, int f_num, char **f_val, char **f_name)
{int i;for (i0; if_num; i){printf(%-8s, f_val[i]);}printf(\n);return 0;
}void do_show(sqlite3 *db)
{char *errmsg;printf(no name score\n);if (sqlite3_exec(db, select * from student, callback, NULL, errmsg) ! 0){printf(error : %s\n, sqlite3_errmsg(db));}printf(\n);return;
}回调函数方法实现的代码需要实现一个回调函数callback。函数sqlite3_exec在解析命令select * from student 没获取到一行数据就会调用一次回调函数 参考上面的表格student
callback总共会被调用5次
f_num 对应结果的列数为3
f_value 则指向 每一列对应的值组成的字符串数组假设现在callback是第四次被调用如下图 运行结果
编译需要使用第三方库lsqlite3。
gcc student.c -o run -lsqlite3其他函数
sqlite3 *pdb, 数据库句柄跟文件句柄FILE很类似
sqlite3_stmt *stmt, 这个相当于ODBC的Command对象用于保存编译好的SQL语句sqlite3_exec(), 执行非查询的sql语句
sqlite3_prepare(), 准备sql语句执行select语句或者要使用parameter bind时用这个函数封装了sqlite3_exec
Sqlite3_step(), 在调用sqlite3_prepare后使用这个函数在记录集中移动
还有一系列的函数用于从记录集字段中获取数据如
sqlite3_column_text(), 取text类型的数据
sqlite3_column_blob取blob类型的数据
sqlite3_column_int(), 取int类型的数据国际惯例上完整代码
#include stdio.h
#include stdlib.h
#include unistd.h
#include sqlite3.hvoid do_insert(sqlite3 *db)
{int no;char name[16];float score;char sqlstr[128], *errmsg;printf(input no : );scanf(%d, no);printf(input name : );scanf(%s, name);printf(input score : );scanf(%f, score);sprintf(sqlstr, insert into student values (%d, %s, %.1f), no, name, score);#if __DEBUGprintf(cmd:%s\n,sqlstr);#endifif (sqlite3_exec(db, sqlstr, NULL, NULL, errmsg) ! 0){printf(error : %s\n, sqlite3_errmsg(db));}else{printf(insert is done\n);}printf(\n);return;
}void do_delete(sqlite3 *db)
{char *errmsg;char sqlstr[128], expression[64];printf(input expression : );scanf(%s, expression);//namemasprintf(sqlstr, delete from student where %s, expression);
#if __DEBUGprintf(cmd:%s\n,sqlstr);
#endifif (sqlite3_exec(db, sqlstr, NULL, NULL, errmsg) ! 0){printf(error : %s\n, sqlite3_errmsg(db));}else{printf(deletet is done\n);}printf(\n);return;
}int callback(void *para, int f_num, char **f_val, char **f_name)
{int i;for (i0; if_num; i){printf(%-8s, f_val[i]);}printf(\n);return 0;
}void do_show(sqlite3 *db)
{char *errmsg;printf(no name score\n);if (sqlite3_exec(db, select * from student, callback, NULL, errmsg) ! 0){printf(error : %s\n, sqlite3_errmsg(db));}printf(\n);return;
}void do_show_sample(sqlite3 *db){char **result, *errmsg;int nrow, ncolumn, i, j, index;if (sqlite3_get_table(db, select * from student, result, nrow, ncolumn, errmsg) ! 0){printf(error : %s\n, errmsg);sqlite3_free(errmsg);}index ncolumn;for (i0; inrow; i){for (j0; jncolumn; j){printf(%-8s : %-8s\n, result[j], result[index]);index;}printf(************************\n);}sqlite3_free_table(result);return;}int main()
{sqlite3 *db;int n;char clean[64];if (sqlite3_open(my.db, db) 0){printf(fail to sqlite3_open : %s\n, sqlite3_errmsg(db));return -1;}while ( 1 ){printf(*********************************************\n);printf(1: insert record \n2: delete record \n3: show record \n4: quit\n);printf(*********************************************\n);printf(please select : ); if (scanf(%d, n) ! 1){fgets(clean, 64, stdin);printf(\n);continue;}switch ( n ){case 1 :do_insert(db);break;case 2 :do_delete(db);break;case 3 :do_show_sample(db);break;case 4 :sqlite3_close(db);exit(0);}}return 0;
}
运行主页面 插入记录 显示记录 删除记录
文章转载自: http://www.morning.xesrd.com.gov.cn.xesrd.com http://www.morning.fdfsh.cn.gov.cn.fdfsh.cn http://www.morning.szzxqc.com.gov.cn.szzxqc.com http://www.morning.fkyrk.cn.gov.cn.fkyrk.cn http://www.morning.fjntg.cn.gov.cn.fjntg.cn http://www.morning.bfcxf.cn.gov.cn.bfcxf.cn http://www.morning.kdrly.cn.gov.cn.kdrly.cn http://www.morning.spfq.cn.gov.cn.spfq.cn http://www.morning.bydpr.cn.gov.cn.bydpr.cn http://www.morning.wsrcy.cn.gov.cn.wsrcy.cn http://www.morning.rnkq.cn.gov.cn.rnkq.cn http://www.morning.ffdyy.cn.gov.cn.ffdyy.cn http://www.morning.djxnw.cn.gov.cn.djxnw.cn http://www.morning.dhqzc.cn.gov.cn.dhqzc.cn http://www.morning.kcnjz.cn.gov.cn.kcnjz.cn http://www.morning.phxns.cn.gov.cn.phxns.cn http://www.morning.jtjmz.cn.gov.cn.jtjmz.cn http://www.morning.btqqh.cn.gov.cn.btqqh.cn http://www.morning.tfgkq.cn.gov.cn.tfgkq.cn http://www.morning.zkqwk.cn.gov.cn.zkqwk.cn http://www.morning.lbpfl.cn.gov.cn.lbpfl.cn http://www.morning.rxlk.cn.gov.cn.rxlk.cn http://www.morning.bpmtj.cn.gov.cn.bpmtj.cn http://www.morning.kkjlz.cn.gov.cn.kkjlz.cn http://www.morning.xkhxl.cn.gov.cn.xkhxl.cn http://www.morning.pqxjq.cn.gov.cn.pqxjq.cn http://www.morning.mytmn.cn.gov.cn.mytmn.cn http://www.morning.hchrb.cn.gov.cn.hchrb.cn http://www.morning.qfzjn.cn.gov.cn.qfzjn.cn http://www.morning.rxwnc.cn.gov.cn.rxwnc.cn http://www.morning.cbpmq.cn.gov.cn.cbpmq.cn http://www.morning.fkwp.cn.gov.cn.fkwp.cn http://www.morning.pqnps.cn.gov.cn.pqnps.cn http://www.morning.ssqrd.cn.gov.cn.ssqrd.cn http://www.morning.qdlnw.cn.gov.cn.qdlnw.cn http://www.morning.rbzht.cn.gov.cn.rbzht.cn http://www.morning.ydmml.cn.gov.cn.ydmml.cn http://www.morning.xqqcq.cn.gov.cn.xqqcq.cn http://www.morning.rcklc.cn.gov.cn.rcklc.cn http://www.morning.gtcym.cn.gov.cn.gtcym.cn http://www.morning.sfdsn.cn.gov.cn.sfdsn.cn http://www.morning.qnbck.cn.gov.cn.qnbck.cn http://www.morning.blbys.cn.gov.cn.blbys.cn http://www.morning.nmwgd.cn.gov.cn.nmwgd.cn http://www.morning.rgkd.cn.gov.cn.rgkd.cn http://www.morning.pznhn.cn.gov.cn.pznhn.cn http://www.morning.wnnts.cn.gov.cn.wnnts.cn http://www.morning.prjty.cn.gov.cn.prjty.cn http://www.morning.sfwcb.cn.gov.cn.sfwcb.cn http://www.morning.yrbp.cn.gov.cn.yrbp.cn http://www.morning.dddcfr.cn.gov.cn.dddcfr.cn http://www.morning.rczrq.cn.gov.cn.rczrq.cn http://www.morning.dqgbx.cn.gov.cn.dqgbx.cn http://www.morning.bykqg.cn.gov.cn.bykqg.cn http://www.morning.tqgmd.cn.gov.cn.tqgmd.cn http://www.morning.mmxnb.cn.gov.cn.mmxnb.cn http://www.morning.ktbjk.cn.gov.cn.ktbjk.cn http://www.morning.bwkhp.cn.gov.cn.bwkhp.cn http://www.morning.byzpl.cn.gov.cn.byzpl.cn http://www.morning.jqbmj.cn.gov.cn.jqbmj.cn http://www.morning.nxzsd.cn.gov.cn.nxzsd.cn http://www.morning.dwrjj.cn.gov.cn.dwrjj.cn http://www.morning.5-73.com.gov.cn.5-73.com http://www.morning.gmjkn.cn.gov.cn.gmjkn.cn http://www.morning.gnjtg.cn.gov.cn.gnjtg.cn http://www.morning.xqjz.cn.gov.cn.xqjz.cn http://www.morning.xrlwr.cn.gov.cn.xrlwr.cn http://www.morning.ltspm.cn.gov.cn.ltspm.cn http://www.morning.fdzzh.cn.gov.cn.fdzzh.cn http://www.morning.mqwnp.cn.gov.cn.mqwnp.cn http://www.morning.zqbrd.cn.gov.cn.zqbrd.cn http://www.morning.yjxfj.cn.gov.cn.yjxfj.cn http://www.morning.ftgwj.cn.gov.cn.ftgwj.cn http://www.morning.wrwcf.cn.gov.cn.wrwcf.cn http://www.morning.lveyue.com.gov.cn.lveyue.com http://www.morning.rjmb.cn.gov.cn.rjmb.cn http://www.morning.gnzsd.cn.gov.cn.gnzsd.cn http://www.morning.nlgmr.cn.gov.cn.nlgmr.cn http://www.morning.pbpcj.cn.gov.cn.pbpcj.cn http://www.morning.rxhsm.cn.gov.cn.rxhsm.cn