全方位認(rèn)識(shí)SQLite(二)_第1頁(yè)
全方位認(rèn)識(shí)SQLite(二)_第2頁(yè)
全方位認(rèn)識(shí)SQLite(二)_第3頁(yè)
全方位認(rèn)識(shí)SQLite(二)_第4頁(yè)
全方位認(rèn)識(shí)SQLite(二)_第5頁(yè)
已閱讀5頁(yè),還剩5頁(yè)未讀 繼續(xù)免費(fèi)閱讀

下載本文檔

版權(quán)說(shuō)明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)

文檔簡(jiǎn)介

1、示例代碼示例代碼一:includeincludesqlite3h*usingnamespacestd;intmain()sqlite3*db=NULL,定義SQLite的連接constchar*zErrMsg=0;/錯(cuò)誤信息intrc;rc=sqlite3_open(E:/Prograncode/SQLitecode/testSQLite.db*,&db);if(rc1=SQLITE.OK)如果打開數(shù)據(jù)庫(kù)失敗zErrMsg=sqlite3_errMsg(db);獲取錯(cuò)誤信息coutzErrMsgendl;sqlite3_close(db);關(guān)閉數(shù)據(jù)庫(kù)連接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,定義數(shù)據(jù)庫(kù)連接constchar*errMsg=0;定義錯(cuò)誤信息char*zerrMsg=0;定義錯(cuò)誤信息打開數(shù)堀庫(kù)intopen_db0打開數(shù)intrc=sqlite3.openCE:/Prograacode/SQLitecode/sqlitejdbcdb,&db);據(jù)庫(kù)if(rc!=SQLITE.OK)數(shù)據(jù)庫(kù)打開失敗errMsg=sqlite3_errsg(db);獲取錯(cuò)誤信息couterrMsgendl;s

4、qlite3_close(db);關(guān)閉數(shù)據(jù)庫(kù)連按returnT;cout*opendatabasesuccessfully!endl,return0;創(chuàng)建表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創(chuàng)建農(nóng)成功!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;數(shù)據(jù)添加intin

6、sert_data(intid,char*nae,intage)if(opendbO!=0)operudbO;sqlite3.stt*stat=NULL;/準(zhǔn)備語(yǔ)句對(duì)象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);參數(shù)一:準(zhǔn)備語(yǔ)句對(duì)象參數(shù)二:序號(hào)(從開始)參數(shù)三:字符半值參數(shù)四:字符串長(zhǎng)度參數(shù)五:函數(shù)指針.SQLITE3執(zhí)行完操作后回調(diào)此函數(shù).通常用丁常放字符串占用的內(nèi)存。(這個(gè)函數(shù)指針參數(shù)具休怎么使用,我現(xiàn)在還不消楚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數(shù)據(jù)插入成功

8、!endl;sqlite3_reset(stat);sqlite3_finalize(stjnt);sqlite3.close(db);return0;數(shù)據(jù)查詢根據(jù)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獲取數(shù)據(jù)庫(kù)農(nóng)的列數(shù)inttype;表字段所對(duì)應(yīng)的類型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語(yǔ)句,且有還有記錄,則應(yīng)該返回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;刪除數(shù)據(jù)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等語(yǔ)句,正常應(yīng)該返回SQLITE.DONEif(rc=SQLITE.DONE)/SQLITE.DONE意味著己成功完成執(zhí)行該語(yǔ)句cout刪除數(shù)據(jù)成功Thunderbird、Seamonkey%Songbi

溫馨提示

  • 1. 本站所有資源如無(wú)特殊說(shuō)明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
  • 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
  • 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁(yè)內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒(méi)有圖紙預(yù)覽就沒(méi)有圖紙。
  • 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
  • 5. 人人文庫(kù)網(wǎng)僅提供信息存儲(chǔ)空間,僅對(duì)用戶上傳內(nèi)容的表現(xiàn)方式做保護(hù)處理,對(duì)用戶上傳分享的文檔內(nèi)容本身不做任何修改或編輯,并不能對(duì)任何下載內(nèi)容負(fù)責(zé)。
  • 6. 下載文件中如有侵權(quán)或不適當(dāng)內(nèi)容,請(qǐng)與我們聯(lián)系,我們立即糾正。
  • 7. 本站不保證下載資源的準(zhǔn)確性、安全性和完整性, 同時(shí)也不承擔(dān)用戶因使用這些下載資源對(duì)自己和他人造成任何形式的傷害或損失。

最新文檔

評(píng)論

0/150

提交評(píng)論