Here you can see some examples. It shows a part of the functionality of the wrapper and how you can use it. You can find more examples in the example application (contained in download package). 1. open a database and create a statement instance for sql queries/statements
//opendatabase
Kompex::SQLiteDatabase*pDatabase=newKompex::SQLiteDatabase("test.db",SQLITE_OPEN_READWRITE,0);
//createstatementinstanceforsqlqueries/statements
Kompex::SQLiteStatement*pStmt=newKompex::SQLiteStatement(pDatabase);
2. create a new table and fill it with data
//createtableandinsertsomedata
pStmt->SqlStatement("CREATETABLEuser
(
userIDINTEGERNOTNULLPRIMARYKEY,
lastNameVARCHAR(50)NOTNULL,
firstNameVARCHAR(50),ageINTEGER
)");
pStmt->SqlStatement("INSERTINTOuser(userID,lastName,firstName,age)
VALUES(1,'Lehmann','Jamie',20)");
pStmt->SqlStatement("INSERTINTOuser(userID,age)
VALUES(2,'Burgdorf','Peter',55)");
pStmt->SqlStatement("INSERTINTOuser(userID,age)
VALUES(3,'Fernando',18)");
pStmt->SqlStatement("INSERTINTOuser(userID,age)
VALUES(4,'Carlene',17)");
3. use some aggregate functions
pStmt->SqlAggregateFuncResult("SELECTCOUNT(*)FROMuserWHERElastName='Lehmann';");
pStmt->SqlAggregateFuncResult("SELECTCOUNT(weight)FROMuser;");
pStmt->SqlAggregateFuncResult("SELECTMAX(age)FROMuser;");
pStmt->SqlAggregateFuncResult("SELECTMIN(age)FROMuser;");
pStmt->SqlAggregateFuncResult("SELECTAVG(age)FROMuser;");
pStmt->SqlAggregateFuncResult("SELECTSUM(age)FROMuser;");
pStmt->SqlAggregateFuncResult("SELECTTOTAL(age)FROMuser;");
4. execute and process a sql query
pStmt->Sql("SELECT*FROMuserWHEREfirstName='Jamie';");
//processallresults
while(pStmt->FetchRow())
{
std::cout<<"firstname:"<<pStmt->GetColumnInt(0)<<std::endl;
std::cout<<"lastname:"<<pStmt->GetColumnString(1)<<std::endl;
std::cout<<"firstname:"<<pStmt->GetColumnString(2)<<std::endl;
}
//donotforgettoclean-up
pStmt->FreeQuery();
5. execute a simple transaction
//ifanerroroccurs,arollbackisautomaticallyperformed
pStmt->BeginTransaction();
pStmt->Transaction("INSERTINTOuser(userID,age)VALUES(10,'Makoto',23)");
pStmt->Transaction("INSERTINTOuser(userID,age)VALUES(11,'Yura',20)";
pStmt->Transaction("INSERTINTOuser(userID,age)VALUES(12,63)");
pStmt->CommitTransaction();
6. here you can see the exception handling
try
{
//tablestructure:userIDINTEGERNOTNULLPRIMARYKEY,lastNameVARCHAR(50)
pStmt->SqlStatement("INSERTINTOuser(userID,lastName)VALUES(1,'Lehmann')");
//thefollowinglinewillthrowanexceptionbecausetheprimarykeyisnotunique
pStmt->SqlStatement("INSERTINTOuser(userID,'Bergmann')");
}
catch(Kompex::SQLiteException&exception)
{
std::cerr<<"ExceptionOccured"<<std::endl;
exception.Show();
}
7. work with a memory database
Kompex::SQLiteDatabase*pDatabase=newKompex::SQLiteDatabase("scores.db",0);
//movedatabasetomemory,sothatweareworkonthememorydatabasehence
pDatabase->MoveDatabaseToMemory();
-
Kompex::SQLiteStatement*pStmt=newKompex::SQLiteStatement(pDatabase);
//insertsomedatasetsintothememorydatabase
pStmt->SqlStatement("INSERTINTOscore(id,lastScore,avgScore)VALUES(1,429,341)");
pStmt->SqlStatement("INSERTINTOscore(id,avgScore)VALUES(2,37,44)");
pStmt->SqlStatement("INSERTINTOscore(id,avgScore)VALUES(3,310,280)");
-
//savethememorydatabasetoafile
//ifyoudon'tdoit,alldatabasechangeswillbelostafterclosingthememorydatabase
pDatabase->SaveDatabaseFromMemoryToFile("newScores.db");
8. get some result values via column name (more flexible and a little bit slower than the common way)
pStmt->Sql("SELECT*FROMuserWHERElastName='Lehmann';");
-
//processallresults
while(pStmt->FetchRow())
{
std::cout<<"firstName:"<<pStmt->GetColumnString("firstName")<<std::endl;
std::cout<<"age:"<<pStmt->GetColumnInt("age")<<std::endl;
}
-
//donotforgettoclean-up
pStmt->FreeQuery();
9. two possibilities to update data in the database
//thefirstway
pStmt->SqlStatement("UPDATEuserSETweight=51.5,age=18WHEREfirstName='Carlene'");
//thesecondway-withapreparedstatement
pStmt->Sql("UPDATEuserSETlastName=@lastName,age=@ageWHEREuserID=@userID");
//bindanintegertothepreparedstatement
pStmt->BindString(1,"Urushihara");//bindlastName
pStmt->BindInt(2,56);//bindage
pStmt->BindInt(3,2);//binduserID
-
//executeitandclean-up
pStmt->ExecuteAndFree();
(编辑:李大同)
【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!
|