Kompex SQLite Wrapper for C++ - Examples

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
  1. //opendatabase
  2. Kompex::sqliteDatabase*pDatabase=newKompex::sqliteDatabase("test.db",sqlITE_OPEN_READWRITE,0);
  3. //createstatementinstanceforsqlqueries/statements
  4. Kompex::sqliteStatement*pStmt=newKompex::sqliteStatement(pDatabase);


2. create a new table and fill it with data
  1. //createtableandinsertsomedata
  2. pStmt->sqlStatement("CREATETAbleuser\
  3. (\
  4. userIDINTEGERNOTNULLPRIMARYKEY,\
  5. lastNameVARCHAR(50)NOTNULL,\
  6. firstNameVARCHAR(50),ageINTEGER\
  7. )");
  8. pStmt->sqlStatement("INSERTINTOuser(userID,lastName,firstName,age)\
  9. VALUES(1,'Lehmann','Jamie',20)");
  10. pStmt->sqlStatement("INSERTINTOuser(userID,age)\
  11. VALUES(2,'Burgdorf','Peter',55)");
  12. pStmt->sqlStatement("INSERTINTOuser(userID,age)\
  13. VALUES(3,'Fernando',18)");
  14. pStmt->sqlStatement("INSERTINTOuser(userID,age)\
  15. VALUES(4,'Carlene',17)");


3. use some aggregate functions
  1. pStmt->sqlAggregateFuncResult("SELECTCOUNT(*)FROMuserWHERElastName='Lehmann';");
  2. pStmt->sqlAggregateFuncResult("SELECTCOUNT(weight)FROMuser;");
  3. pStmt->sqlAggregateFuncResult("SELECTMAX(age)FROMuser;");
  4. pStmt->sqlAggregateFuncResult("SELECTMIN(age)FROMuser;");
  5. pStmt->sqlAggregateFuncResult("SELECTAVG(age)FROMuser;");
  6. pStmt->sqlAggregateFuncResult("SELECTSUM(age)FROMuser;");
  7. pStmt->sqlAggregateFuncResult("SELECTTOTAL(age)FROMuser;");


4. execute and process a sql query
  1. pStmt->sql("SELECT*FROMuserWHEREfirstName='Jamie';");
  2. //processallresults
  3. while(pStmt->FetchRow())
  4. {
  5. std::cout<<"firstname:"<<pStmt->GetColumnInt(0)<<std::endl;
  6. std::cout<<"lastname:"<<pStmt->GetColumnString(1)<<std::endl;
  7. std::cout<<"firstname:"<<pStmt->GetColumnString(2)<<std::endl;
  8. }
  9. //donotforgettoclean-up
  10. pStmt->FreeQuery();


5. execute a simple transaction
  1. //ifanerroroccurs,arollbackisautomaticallyperformed
  2. pStmt->BeginTransaction();
  3. pStmt->Transaction("INSERTINTOuser(userID,age)VALUES(10,'Makoto',23)");
  4. pStmt->Transaction("INSERTINTOuser(userID,age)VALUES(11,'Yura',20)";
  5. pStmt->Transaction("INSERTINTOuser(userID,age)VALUES(12,63)");
  6. pStmt->CommitTransaction();


6. here you can see the exception handling
  1. try
  2. {
  3. //tablestructure:userIDINTEGERNOTNULLPRIMARYKEY,lastNameVARCHAR(50)
  4. pStmt->sqlStatement("INSERTINTOuser(userID,lastName)VALUES(1,'Lehmann')");
  5. //thefollowinglinewillthrowanexceptionbecausetheprimarykeyisnotunique
  6. pStmt->sqlStatement("INSERTINTOuser(userID,'Bergmann')");
  7. }
  8. catch(Kompex::sqliteException&exception)
  9. {
  10. std::cerr<<"ExceptionOccured"<<std::endl;
  11. exception.Show();
  12. }


7. work with a memory database
  1. Kompex::sqliteDatabase*pDatabase=newKompex::sqliteDatabase("scores.db",0);
  2. //movedatabasetomemory,sothatweareworkonthememorydatabasehence
  3. pDatabase->MoveDatabasetoMemory();
  4. Kompex::sqliteStatement*pStmt=newKompex::sqliteStatement(pDatabase);
  5. //insertsomedatasetsintothememorydatabase
  6. pStmt->sqlStatement("INSERTINTOscore(id,lastscore,avgscore)VALUES(1,429,341)");
  7. pStmt->sqlStatement("INSERTINTOscore(id,avgscore)VALUES(2,37,44)");
  8. pStmt->sqlStatement("INSERTINTOscore(id,avgscore)VALUES(3,310,280)");
  9. //savethememorydatabasetoafile
  10. //ifyoudon'tdoit,alldatabasechangeswillbelostafterclosingthememorydatabase
  11. pDatabase->SaveDatabaseFromMemoryToFile("newscores.db");


8. get some result values via column name (more flexible and a little bit slower than the common way)
  1. pStmt->sql("SELECT*FROMuserWHERElastName='Lehmann';");
  2. //processallresults
  3. while(pStmt->FetchRow())
  4. {
  5. std::cout<<"firstName:"<<pStmt->GetColumnString("firstName")<<std::endl;
  6. std::cout<<"age:"<<pStmt->GetColumnInt("age")<<std::endl;
  7. }
  8. //donotforgettoclean-up
  9. pStmt->FreeQuery();


9. two possibilities to update data in the database
  1. //thefirstway
  2. pStmt->sqlStatement("UPDATEuserSETweight=51.5,age=18WHEREfirstName='Carlene'");
  3. //thesecondway-withapreparedstatement
  4. pStmt->sql("UPDATEuserSETlastName=@lastName,age=@ageWHEREuserID=@userID");
  5. //bindanintegertothepreparedstatement
  6. pStmt->BindString(1,"Urushihara");//bindlastName
  7. pStmt->BindInt(2,56);//bindage
  8. pStmt->BindInt(3,2);//binduserID
  9. //executeitandclean-up
  10. pStmt->ExecuteAndFree();

相关文章

SQLite架构简单,又有Json计算能力,有时会承担Json文件/RES...
使用Python操作内置数据库SQLite以及MySQL数据库。
破解微信数据库密码,用python导出微信聊天记录
(Unity)SQLite 是一个软件库,实现了自给自足的、无服务器...
安卓开发,利用SQLite实现登陆注册功能