版权说明:本文档由用户提供并上传,收益归属内容提供方,若内容存在侵权,请进行举报或认领
文档简介
1、示例代码示例代码一:includeincludesqlite3h*usingnamespacestd;intmain()sqlite3*db=NULL,定义SQLite的连接constchar*zErrMsg=0;/错误信息intrc;rc=sqlite3_open(E:/Prograncode/SQLitecode/testSQLite.db*,&db);if(rc1=SQLITE.OK)如果打开数据库失败zErrMsg=sqlite3_errMsg(db);获取错误信息coutzErrMsgendl;sqlite3_close(db);关闭数据库连接return-1,cout*opente
2、stSQLite.dbsuccessfully!*endl,sqlite3_close(db);return0;示例代码二:头文件:my_dbh.sqlite3.h源文fl:sqlitedb.cpp、main.cppMy_db.h代码fifndefMY.DB.HfdefineMY.DB.Hintopen_db(),intcreate.tableO;intdrop_tablc(),intinsert-data(intidchar*naae,intage);intsearch.data(intid);intsearch.data(char*nae);intdelete_data(intage),f
3、endifSqlitedb.cpp代码:鼻include*sqlite3h-include*y_dbhincludeusingnamespacestd;sqlite3*db=NULL,定义数据库连接constchar*errMsg=0;定义错误信息char*zerrMsg=0;定义错误信息打开数堀库intopen_db0打开数intrc=sqlite3.openCE:/Prograacode/SQLitecode/sqlitejdbcdb,&db);据库if(rc!=SQLITE.OK)数据库打开失败errMsg=sqlite3_errsg(db);获取错误信息couterrMsgendl;s
4、qlite3_close(db);关闭数据库连按returnT;cout*opendatabasesuccessfully!endl,return0;创建表intcreate.tableOif(open_db()!=0)open.dbO;char*sql=*createtabletab(idintpriaarykey,naaevarchar(20),ageint)*,intrc=sqliteS.execCdb,sql,NULL.NULL,AzerrMsg);if(rc1=SQLITE_OK)errMsg=sqlite3_errMsg(db);couterrMsgendl;/coutzerrXs
5、gendl;sqlite3_close(db);return-1;cout创建农成功!endl;return0;/删除表intdrop.tableOif(open.dbO!=0)open.dbO;char*sql=droptabletab;intrc=sqlite3_exec(db,sql,NULL,NULL,AzerrMsg)zif(rc1=SQLITE.OK)errMsg=sqlite3_errBSg(db);couterrMsgendl;/coutzerrNsgendl;sqlite3_close(db);returnTzcout9H除表成功*endl:return0;数据添加intin
6、sert_data(intid,char*nae,intage)if(opendbO!=0)operudbO;sqlite3.stt*stat=NULL;/准备语句对象char*sql=insertintotab(id,nae,age)values(?,?);intrc=sqlite3_prepare_v2(db,sql,strlenCsqDstmtNULL):if(rc1=SQLITE.OK)errMsg=sqlite3_errBSg(db);couterrMsgendl;if(stt)sqlite3_finalize(stMt);sqlitc3_close(db);return-1;sql
7、ite3_bincLint(stmt,1,id);参数一:准备语句对象参数二:序号(从开始)参数三:字符半值参数四:字符串长度参数五:函数指针.SQLITE3执行完操作后回调此函数.通常用丁常放字符串占用的内存。(这个函数指针参数具休怎么使用,我现在还不消楚sqlite3_bincLtext(stat,2,name,strlen(naMe),NULL);sqlite3_bind_int(stmt,3,age);if(sqlite3.step(stt)!=SQLITE.DONE)sqlite3_finalize(stmt);sqlite3_close(db);returnT;cout数据插入成功
8、!endl;sqlite3_reset(stat);sqlite3_finalize(stjnt);sqlite3.close(db);return0;数据查询根据id唯一性査询intsearch.data(inticDif(open.dbO!=0)open_db();char*sql=select*fromtabwhereid=sqlite3.stt*stmt=NULL;intrc=sqlite3_prepare_v2(ci),sql,strlen(sql),AstmtNULL):if(rc1=SQLITE_OK)(errMsg=sqlite3_errMSg(db);coutcrrMsgcn
9、dl;if(stBt)sqlite3_finalize(stMt);sqlite3_close(db);return-1;sqlite3_bind_int(stmt,1,id);intnColuan=sqlite3_colujnn_count(stat)r获取数据库农的列数inttype;表字段所对应的类型rc=sqlite3.step(stBt);if(rc=SQLITE.ROI)for(inti=O;itype=sqlite3_coluMn_type(stmt,i);if(type=SQLITE.INTEGER)coutsqlite3_coluin_name(stiit,i)te)if(o
10、pen.dbO1=0)open_db();char*sql=select*fromtabwherenaae=?;sqlite3_stt*stjnt=NULL;intrc=sqlite3_prepare_v2(db,sql,strlen(sql),&s5t,NULL);if(rc!=SQLITE.OK)errMsg=sqlite3_errBSg(db);couterrMsgendl;if(stat)sqlite3_finalize(stMt);sqlite3.close(db);return-1,sqlite3.bincLtext(stat,1.name,strlen(nanie),NULL);
11、intnColuan=sqliteS.column.count(stat);inttype;rc=sqlite3_step(stMt);/如果足select语句,且有还有记录,则应该返回SQLITE.ROfwhile(true)if(rc=SQLITE.ROt)coutendl;for(inti=0;itype=sqlite3_colunm_type(stMt,i);if(type=SQLITE.INTEGER)coutsqlite3-colmrLnaMe(stnt,i)tsqlite3_coluMn_int(stmt,i)endl;if(type=SQLITE_TEXT)coutsqlite
12、3_colmnnajne(stmt,i)t*sqlite3_coluan_text(stat,i)endl;elseif(type=SQLITE.NULL)cout*novalue*endl;elseif(rc=SQLITE.DONE)coutselectfinish!*endl;elsecout*selectfailendl;sqlite3.finalize(stt);if(sqlite3_step(st*t)!=SQLITE.ROI)break;sqlite3_finalize(stmt),sqlite3_close(db);return0;删除数据intdelete_data(intag
13、e)if(open.dbO1=0)open.dbO;char*sql=deletefrontabwhereage=Ytsqlite3.stMt*stmt=NULL;intrc=sqlite3j?repare_v2(cb.sql,strlenCsqD.AstmtNULL);if(rc1=SQLITE.OK)errMsg=sqlite3_errMSg(db);couterrMsgendl;if(stat)sqlite3_finalize(stt);sqlite3_close(db);return-lzsqlite3.bind_int(s5t,1.age);rc=sqlite3_step(stmt);/如果ALupdate,delete,insert等语句,正常应该返回SQLITE.DONEif(rc=SQLITE.DONE)/SQLITE.DONE意味着己成功完成执行该语句cout删除数据成功Thunderbird、Seamonkey%Songbi
温馨提示
- 1. 本站所有资源如无特殊说明,都需要本地电脑安装OFFICE2007和PDF阅读器。图纸软件为CAD,CAXA,PROE,UG,SolidWorks等.压缩文件请下载最新的WinRAR软件解压。
- 2. 本站的文档不包含任何第三方提供的附件图纸等,如果需要附件,请联系上传者。文件的所有权益归上传用户所有。
- 3. 本站RAR压缩包中若带图纸,网页内容里面会有图纸预览,若没有图纸预览就没有图纸。
- 4. 未经权益所有人同意不得将文件中的内容挪作商业或盈利用途。
- 5. 人人文库网仅提供信息存储空间,仅对用户上传内容的表现方式做保护处理,对用户上传分享的文档内容本身不做任何修改或编辑,并不能对任何下载内容负责。
- 6. 下载文件中如有侵权或不适当内容,请与我们联系,我们立即纠正。
- 7. 本站不保证下载资源的准确性、安全性和完整性, 同时也不承担用户因使用这些下载资源对自己和他人造成任何形式的伤害或损失。
最新文档
- 精准整形美容行业分析及未来五至十年行业发展报告
- 精神药物相关行业公司成立方案及可行性研究报告
- 2024年吉林省中考化学试卷二套合卷附答案
- 初中++数学确定圆的条件+课件+苏科版数学九年级上册
- unit4(拔尖作业)2024-2025学年三年级上册 英语 译林版
- 美国教师课程设计 案例
- 中大班建构游戏活动方案
- 四川省德阳市(2024年-2025年小学四年级语文)统编版阶段练习((上下)学期)试卷及答案
- 2024年江苏省宿迁市泗阳县仓集镇中心小学四上数学期末达标检测试题含解析
- 2024年湖南株州市攸县事业单位招聘工作人员59人高频500题难、易错点模拟试题附带答案详解
- 医疗质量管理的七大工具
- 上海张江集电港二期幕墙工程施工组织设计
- 全面深化改革的意义及总体思路
- 新外研版高中英语选择性必修一Unit5 understanding ideas课件
- 食品生产企业财务规划与预算管理方案
- 愚公移山英文 -中国故事英文版课件
- 2023储能柜通用技术规范
- 试卷讲评课教学设计
- 一年级上册 劳动 全册课件
- 【03312】劳动和社会保障概论题库+答案
- 历年高中数学联赛真题分类汇编32不等式
评论
0/150
提交评论