




版權(quán)說明:本文檔由用戶提供并上傳,收益歸屬內(nèi)容提供方,若內(nèi)容存在侵權(quán),請(qǐng)進(jìn)行舉報(bào)或認(rèn)領(lǐng)
文檔簡(jiǎn)介
C/C++中調(diào)用SQLITE3的基本步驟最近因?yàn)閷W(xué)校那個(gè)項(xiàng)目,也在搞數(shù)據(jù)庫方面的東西,以前也用過SQLITE的東西,拿來復(fù)習(xí)下
Sqlite是一個(gè)面向嵌入式系統(tǒng)的數(shù)據(jù)庫,編譯完成只有200K,同時(shí)支持2T的數(shù)據(jù)記錄。對(duì)于嵌入式設(shè)備是一個(gè)很好的數(shù)據(jù)庫引擎。本文通過一個(gè)小例子說明如何在C與C++調(diào)用SqliteAPI完成數(shù)據(jù)庫的創(chuàng)建、插入數(shù)據(jù)與查詢數(shù)據(jù)。本文的開發(fā)環(huán)境為(Redhat9.0+Qtopia2.1.2+Sqlite3)
安裝Sqlite3:
從上下載Sqlite3.2.2運(yùn)源代碼,依照Readme中的步驟:
tarxzfsqlite3.2.2.tar.gz
mkdirbld
cdbld
../sqlite3.2.2/configure
make
makeinstall
然后在shell下運(yùn)行sqlite3test.db命令可以檢驗(yàn)是否已經(jīng)安裝成功。
創(chuàng)建數(shù)據(jù)庫:
sqlite3*pDB=NULL;
char*errMsg=NULL;
//打開一個(gè)數(shù)據(jù)庫,如果改數(shù)據(jù)庫不存在,則創(chuàng)建一個(gè)名字為databaseName的數(shù)據(jù)庫文件
intrc=sqlite3_open(databaseName,&pDB);
if(rc)
{
cout<<"Openthedatabase"<<databaseName<<"failed"<<endl;
}
//如果創(chuàng)建成功,添加表
else
{
cout<<"createthedatabasesuccessful!"<<endl;
//creatthetable
inti;
for(i=1;i<nTableNum;i++)
{
}
//插入一個(gè)表,返回值為SQLITE_OK為成功,否則輸出出錯(cuò)信息
//函數(shù)參數(shù):第一個(gè)為操作數(shù)據(jù)庫的指針,第二句為SQL命令字符串
//第三個(gè)參數(shù)為callback函數(shù),這里沒有用,第四個(gè)參數(shù)為callback函數(shù)
//中的第一個(gè)參數(shù),第五個(gè)為出錯(cuò)信息
rc=sqlite3_exec(pDB,"CREATETABLEchn_to_eng(chineseQString,englishQString)",0,0,&errMsg);
if(rc==SQLITE_OK)
cout<<"createthechn_to_engtablesuccessful!"<<endl;
else
cout<<errMsg<<endl;
//同上,插入另一個(gè)表
rc=sqlite3_exec(pDB,"CREATETABLEeng_to_chn(englishQString,chineseQString)",0,0,&errMsg);
if(rc==SQLITE_OK)
cout<<"createtheeng_to_chntablesuccessful!"<<endl;
else
cout<<errMsg<<endl;
}
、、、、、、
//往表中添加數(shù)據(jù)
charchn[]="...";
chareng[]="...";
charvalue[500];
//定義一條參數(shù)SQL命令,其中chn,eng為需要插入的數(shù)據(jù)
sprintf(value,"INSERTINTOchn_to_eng(chinese,english)VALUES('%s','%s')",chn,eng);
//usetheSQLITEC/C++APItocreateandadjustadatabase.
rc=sqlite3_exec(pDB,
value,
0,0,&errMsg);
//查詢一條記錄
charvalue[500];
//定義一條查詢語句,其中條件為當(dāng)english為target時(shí)的中文記錄
//print_result_cb為callback函數(shù),在其中可以得到查詢的結(jié)果,具體見下文
sprintf(value,"SELECTchineseFROMeng_to_chnwhereenglish='%s'",target);
rc=sqlite3_exec(pDB,
value,
print_result_cb,0,&errMsg);
if(rc==SQLITE_OK)
{
//
#ifdef_debug
cout<<"selecttherecordsuccessful!"<<endl;
//
#endif
}
else
{
//
#ifdef_debug
cout<<errMsg<<endl;
//
#endif
returnfalse;
}
.......
}//callback回調(diào)函數(shù)print_result_cb的編寫,其中data為sqlite3_exec中的第四個(gè)參數(shù),第二個(gè)參數(shù)是欄的數(shù)目,第三個(gè)是欄的名字,第四個(gè)為查詢得到的值得。這兩個(gè)函數(shù)輸出所有查詢到的結(jié)果
intprint_result_cb(void*data,intn_columns,char**column_values,
char**column_names)
{
staticintcolumn_names_printed=0;
inti;
if(!column_names_printed){
print_row(n_columns,column_names);
column_names_printed=1;
}
print_row(n_columns,column_values);
return0;
}
voidprint_row(intn_values,char**values)
{
inti;
for(i=0;i<n_values;++i){
if(i>0){
printf("\t");
}
printf("%s",values[i]);
}
printf("\n");
}
大致過程就是如此,具體可以參考SQLITE的API函數(shù)說明,見
voidprint_row(intn_values,char**values)
{
inti;
for(i=0;i<n_values;++i){
if(i>0){
printf("\t");
}
printf("%s",values[i]);
}
printf("\n");
}
大致過程就是如此,具體可以參考SQLITE的API函數(shù)說明,見
voidprint_row(intn_values,char**values)
{
inti;
for(i=0;i<n_values;++i){
if(i>0){
printf("\t");
}
printf("%s",values[i]);
}
printf("\n");
}
大致過程就是如此,具體可以參考SQLITE的API函數(shù)說明,見
charvalue[500];
//定義一條參數(shù)SQL命令,其中chn,eng為需要插入的數(shù)據(jù)
sprintf(value,"INSERTINTOchn_to_eng(chinese,english)VALUES('%s','%s')",chn,eng);
//usetheSQLITEC/C++APItocreateandadjustadatabase.
rc=sqlite3_exec(pDB,
value,
0,0,&errMsg);
//查詢一條記錄
charvalue[500];
//定義一條查詢語句,其中條件為當(dāng)english為target時(shí)的中文記錄
//print_result_cb為callback函數(shù),在其中可以得到查詢的結(jié)果,具體見下文
sprintf(value,"SELECTchineseFROMeng_to_chnwhereenglish='%s'",target);
rc=sqlite3_exec(pDB,
value,
print_result_cb,0,&errMsg);
if(rc==SQLITE_OK)
{
//
#ifdef_debug
cout<<"selecttherecordsuccessful!"<<endl;
//
#endif
}
else
{
//
#ifdef_debug
cout<<errMsg<<endl;
//
#endif
returnfalse;
}
.......
}
//callback回調(diào)函數(shù)print_result_cb的編寫,其中data為sqlite3_exec中的第四個(gè)參數(shù),第二個(gè)參數(shù)是欄的數(shù)目,第三個(gè)是欄的名字,第四個(gè)為查詢得到的值得。這兩個(gè)函數(shù)輸出所有查詢到的結(jié)果
intprint_result_cb(void*data,intn_columns,char**column_values,
char**column_names)
{
staticintcolumn_names_printed=0;
inti;
if(!column_names_printed){
print_row(n_columns,column_names);
column_names_printed=1;
}
print_row(n_columns,column_values);
return0;
}
voidprint_row(intn_values,char**values)
{
inti;
for(i=0;i<n_values;++i){
if(i>0){
printf("\t");
}
printf("%s",values[i]);
}
printf("\n");
}大致過程就是如此,具體可以參考SQLITE的API函數(shù)說明,見
voidprint_row(intn_values,char**values)
{
inti;
for(i=0;i<n_values;++i){
if(i>0){
printf("\t");
}
printf("%s",values[i]);
}
printf("\n");
}
大致過程就是如此,具體可以參考SQLITE的API函數(shù)說明,見
voidprint_row(intn_values,char**values)
{
inti;
for(i=0;i<n_values;++i){
if(i>0){
printf("\t");
}
printf("%s",values[i]);
}
printf("\n");
}
大致過程就是如此,具體可以參考SQLITE的API函數(shù)說明,見
charvalue[500];
//定義一條參數(shù)SQL命令,其中chn,eng為需要插入的數(shù)據(jù)
sprintf(value,"INSERTINTOchn_to_eng(chinese,english)VALUES('%s','%s')",chn,eng);
//usetheSQLITEC/C++APItocreateandadjustadatabase.
rc=sqlite3_exec(pDB,
value,
0,0,&errMsg);
//查詢一條記錄
charvalue[500];
//定義一條查詢語句,其中條件為當(dāng)english為target時(shí)的中文記錄
//print_result_cb為callback函數(shù),在其中可以得到查詢的結(jié)果,具體見下文
sprintf(value,"SELECTchineseFROMeng_to_chnwhereenglish='%s'",target);
rc=sqlite3_exec(pDB,
value,
print_result_cb,0,&errMsg);
if(rc==SQLITE_OK)
{
//
#ifdef_debug
cout<<"selecttherecordsuccessful!"<<endl;
//
#endif
}
else
{
//
#ifdef_debug
cout<<errMsg<<endl;
//
#endif
retur
溫馨提示
- 1. 本站所有資源如無特殊說明,都需要本地電腦安裝OFFICE2007和PDF閱讀器。圖紙軟件為CAD,CAXA,PROE,UG,SolidWorks等.壓縮文件請(qǐng)下載最新的WinRAR軟件解壓。
- 2. 本站的文檔不包含任何第三方提供的附件圖紙等,如果需要附件,請(qǐng)聯(lián)系上傳者。文件的所有權(quán)益歸上傳用戶所有。
- 3. 本站RAR壓縮包中若帶圖紙,網(wǎng)頁內(nèi)容里面會(huì)有圖紙預(yù)覽,若沒有圖紙預(yù)覽就沒有圖紙。
- 4. 未經(jīng)權(quán)益所有人同意不得將文件中的內(nèi)容挪作商業(yè)或盈利用途。
- 5. 人人文庫網(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ì)自己和他人造成任何形式的傷害或損失。
最新文檔
- 化妝鏡、放大鏡、裝飾鏡及各類玻璃制品的生產(chǎn)第二次擴(kuò)建項(xiàng)目環(huán)評(píng)報(bào)告表
- 洋浦疏港高速公路工程SG01標(biāo)段拌合站、鋼筋加工場(chǎng)、辦公用房環(huán)評(píng)報(bào)告(公示稿)環(huán)評(píng)報(bào)告表
- 2025年化學(xué)氣相沉積硫化鋅(CVDZNS)晶體合作協(xié)議書
- 海南馨島優(yōu)匯生物科技有限公司年產(chǎn)3萬噸微生物菌劑和2萬噸有機(jī)水溶肥項(xiàng)目環(huán)境影響報(bào)告表公示稿環(huán)評(píng)報(bào)告表
- 中貝通信被擔(dān)保人最近一期財(cái)務(wù)報(bào)表
- 內(nèi)蒙古華云新材料有限公司審計(jì)報(bào)告
- 露天礦山工程邊坡施工方案
- 墩柱滑模施工方案
- 緩釋肥與常規(guī)復(fù)合肥配合施用對(duì)水稻產(chǎn)量和品質(zhì)的影響分析
- 醫(yī)療機(jī)構(gòu)水污染物排放的治理技術(shù)
- 2025年安徽中醫(yī)藥高等專科學(xué)校單招職業(yè)適應(yīng)性測(cè)試題庫有答案
- 2025年無錫職業(yè)技術(shù)學(xué)院?jiǎn)握新殬I(yè)傾向性測(cè)試題庫完整版
- 2025年皖西衛(wèi)生職業(yè)學(xué)院?jiǎn)握新殬I(yè)技能測(cè)試題庫及答案1套
- 《馬云創(chuàng)業(yè)經(jīng)歷》課件
- 常用量具使用方法課件
- 2024年05月安徽農(nóng)商銀行系統(tǒng)社會(huì)招考計(jì)算機(jī)法律專業(yè)員工人員筆試歷年參考題庫附帶答案詳解
- 騰訊云人工智能工程師認(rèn)證考試題(附答案)
- 專題03 透鏡及其應(yīng)用(5大模塊知識(shí)清單+5個(gè)易混易錯(cuò)+6種方法技巧+典例真題解析)
- 建設(shè)單位保證工程安全措施的實(shí)施方案
- 第16課數(shù)據(jù)管理與編碼(教案)四年級(jí)全一冊(cè)信息技術(shù)人教版
- 2024中考物理真題匯編:電與磁(含解析)
評(píng)論
0/150
提交評(píng)論