加入收藏 | 设为首页 | 会员中心 | 我要投稿 李大同 (https://www.lidatong.com.cn/)- 科技、建站、经验、云计算、5G、大数据,站长网!
当前位置: 首页 > 百科 > 正文

Kompex SQLite Wrapper for C++ - Examples

发布时间:2020-12-12 20:27:53 所属栏目:百科 来源:网络整理
导读: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 in
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();

(编辑:李大同)

【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容!

    推荐文章
      热点阅读