CppSQLiteC++ Wrapper for SQLiteWord文档下载推荐.docx
《CppSQLiteC++ Wrapper for SQLiteWord文档下载推荐.docx》由会员分享,可在线阅读,更多相关《CppSQLiteC++ Wrapper for SQLiteWord文档下载推荐.docx(22页珍藏版)》请在冰豆网上搜索。
canbefoundontheSQLitewebsite.
Linkingdynamicallystillrequiresthata
.lib
fileisbuiltforlinkingwithyourapplication.ThiscanbedoneusingMicrosoft'
s
LIB
command.Onmysystem,thisislocatedat
D:
\ProgramFiles\MicrosoftVisualStudio\VC98\Bin\lib.exe.
Unzip
sqlite.zip
whichcontains
sqlite.dll
and
sqlite.def,andexecutethefollowingcommandtoproducethelibfile.
Collapse
|
CopyCode
c:
\>
lib/def:
sqlite.def
sqlite.h
needstobevisibletoyourapplicationatcompiletime,asdoes
sqlite.lib.
needstobeavailabletoyourapplicationatruntime.
CppSQLiteDemoCode
ThefollowingcodedemonstrateshowtousethemainfeaturesofSQLitevia
CppSQLite,withcommentsinline.
#include"
CppSQLite.h"
#include<
ctime>
iostream>
usingnamespacestd;
constchar*gszFile="
C:
\\test.db"
;
intmain(intargc,char**argv)
{
try
{
inti,fld;
time_ttmStart,tmEnd;
CppSQLiteDBdb;
cout<
<
"
SQLiteVersion:
<
db.SQLiteVersion()<
endl;
remove(gszFile);
db.open(gszFile);
endl<
Creatingemptable"
db.execDML("
createtableemp(empnoint,empnamechar(20));
"
);
///////////////////////////////////////////////////////////////
//ExecutesomeDML,andprintnumberofrowsaffectedbyeachone
DMLtests"
intnRows=db.execDML("
insertintoempvalues(7,'
DavidBeckham'
nRows<
rowsinserted"
nRows=db.execDML(
updateempsetempname='
ChristianoRonaldo'
whereempno=7;
rowsupdated"
nRows=db.execDML("
deletefromempwhereempno=7;
rowsdeleted"
/////////////////////////////////////////////////////////////////
//TransactionDemo
//Thetransactioncouldjustaseasilyhavebeenrolledback
intnRowsToCreate(50000);
Transactiontest,creating"
nRowsToCreate;
rowspleasewait..."
tmStart=time(0);
begintransaction;
for(i=0;
i<
i++)
charbuf[128];
sprintf(buf,"
insertintoempvalues(%d,'
Empname%06d'
i,i);
db.execDML(buf);
}
committransaction;
tmEnd=time(0);
////////////////////////////////////////////////////////////////
//DemonstrateCppSQLiteDB:
:
execScalar()
db.execScalar("
selectcount(*)fromemp;
)
rowsinemptablein"
tmEnd-tmStart<
seconds(thatwasfast!
)"
//Re-createemptablewithauto-incrementfield
Autoincrementtest"
droptableemp;
db.execDML(
createtableemp(empnointegerprimarykey,empnamechar(20));
5;
sprintf(buf,
insertintoemp(empname)values('
i+1);
primarykey:
db.lastRowId()<
///////////////////////////////////////////////////////////////////
//Querydataandalsoshowresultsofinsertsintoauto-incrementfield
//////////////////////////////////////////////////////////////////
Selectstatementtest"
CppSQLiteQueryq=db.execQuery("
select*fromemporderby1;
for(fld=0;
fld<
q.numFields();
fld++)
q.fieldName(fld)<
("
q.fieldType(fld)<
)|"
while(!
q.eof())
q.fieldValue(0)<
|"
q.fieldValue
(1)<
q.nextRow();
//SQLite'
sprintf()functionality.HandlesembeddedquotesandNULLs
SQLitesprintftest"
CppSQLiteBufferbufSQL;
bufSQL.format("
insertintoemp(empname)values(%Q);
"
He'
sbad"
(constchar*)bufSQL<
db.execDML(bufSQL);
NULL);
////////////////////////////////////////////////////////////////////
//Fetchtableatonce,andalsoshowhowto
//useCppSQLiteTable:
setRow()method
getTable()test"
CppSQLiteTablet=db.getTable("
t.numFields();
t.fieldName(fld)<
for(introw=0;
row<
t.numRows();
row++)
t.setRow(row);
for(intfld=0;
if(!
t.fieldIsNull(fld))
t.fieldValue(fld)<
else
NULL"
//TestCppSQLiteBinarybystoring/retrievingsomebinarydata,checking
//itafterwardstomakesureitisthesame
Binarydatatest"
createtablebindata(descchar(10),datablob);
unsignedcharbin[256];
CppSQLiteBinaryblob;
sizeofbin;
bin[i]=i;
blob.setBinary(bin,sizeofbin);
insertintobindatavalues('
testing'
%Q);
blob.getEncoded());
StoredbinaryLength:
sizeofbin<
q=db.execQuery("
selectdatafrombindatawheredesc='
blob.setEncoded((unsignedchar*)q.fieldValue("
data"
));
RetrievedbinaryLength:
blob.getBinaryLength()<
constunsignedchar*pbin=blob.getBinary();
if(pbin[i]!
=i)
Problem:
i:
"
bin[i]:
pbin[i]<
/////////////////////////////////////////////////////////
//Pre-compiledStatementsDemo
/////////////////////////////////////////////////////////////
CppSQLiteStatementstmt=pileStatement(
insertintoempvalues(?
?
charbuf[16];
EmpName%06d"
i);
stmt.bind(1,i);
stmt.bind(2,buf);
stmt.execDML();
stmt.reset();
seconds(thatwasevenfaster!
Endoftests"
catch(CppSQLiteException&
e)
cerr<
e.errorCode()<
e.errorMessage()<
//LoopuntiluserentersqorQ
///////////////////////////////////////////////////////////
charc('
'
while(c!
='
q'
&
&
c!
Q'
)
Pressqthenenter