一、前言:
今天试了下如何用C++类实现接口封装,感觉蛮好 。用于封装的类主要有两个,SQLiteStatement类和SQLiteWrapper类,是一个老外写的。我看了下源码,主要是对C接口进行了封装,好处自然不用说,可以重用。很佩服老外的技巧,在这里就引用下他们的代码供大家分享下他们的思想。
源代码链接: http://www.adp-gmbh.ch/sqlite/wrapper.html
二、类源码:
1.头文件:SQLiteWrapper.h
http://sqlitewrapper.kompex-online.com/index.php?content=examples
// open database
Kompex::SQLiteDatabase *pDatabase = new Kompex::SQLiteDatabase("test.db", SQLITE_OPEN_READWRITE, 0);
// create statement instance for sql queries/statements
Kompex::SQLiteStatement *pStmt = new Kompex::SQLiteStatement(pDatabase);
// create table and insert some data
pStmt->SqlStatement("CREATE TABLE user \
( \
userID INTEGER NOT NULL PRIMARY KEY, \
lastName VARCHAR(50) NOT NULL, \
firstName VARCHAR(50), age INTEGER \
)");
pStmt->SqlStatement("INSERT INTO user (userID, lastName, firstName, age) \
VALUES (1, 'Lehmann', 'Jamie', 20)");
pStmt->SqlStatement("INSERT INTO user (userID, lastName, firstName, age) \
VALUES (2, 'Burgdorf', 'Peter', 55)");
pStmt->SqlStatement("INSERT INTO user (userID, lastName, firstName, age) \
VALUES (3, 'Lehmann', 'Fernando', 18)");
pStmt->SqlStatement("INSERT INTO user (userID, lastName, firstName, age) \
VALUES (4, 'Lehmann', 'Carlene', 17)");
pStmt->SqlAggregateFuncResult("SELECT COUNT(*) FROM user WHERE lastName = 'Lehmann';");
pStmt->SqlAggregateFuncResult("SELECT COUNT(weight) FROM user;");
pStmt->SqlAggregateFuncResult("SELECT MAX(age) FROM user;");
pStmt->SqlAggregateFuncResult("SELECT MIN(age) FROM user;");
pStmt->SqlAggregateFuncResult("SELECT AVG(age) FROM user;");
pStmt->SqlAggregateFuncResult("SELECT SUM(age) FROM user;");
pStmt->SqlAggregateFuncResult("SELECT TOTAL(age) FROM user;");
pStmt->Sql("SELECT * FROM user WHERE firstName = 'Jamie';");
// process all results
while(pStmt->FetchRow())
{
std::cout << "first name: " << pStmt->GetColumnInt(0) << std::endl;
std::cout << "last name: " << pStmt->GetColumnString(1) << std::endl;
std::cout << "first name: " << pStmt->GetColumnString(2) << std::endl;
}
// do not forget to clean-up
pStmt->FreeQuery();
// if an error occurs, a rollback is automatically performed
pStmt->BeginTransaction();
pStmt->Transaction("INSERT INTO user(userID, firstName, age) VALUES (10, 'Makoto', 23)");
pStmt->Transaction("INSERT INTO user(userID, firstName, age) VALUES (11, 'Yura', 20)";
pStmt->Transaction("INSERT INTO user(userID, firstName, age) VALUES (12, 'Peter', 63)");
pStmt->CommitTransaction();
try
{
// table structure: userID INTEGER NOT NULL PRIMARY KEY, lastName VARCHAR(50)
pStmt->SqlStatement("INSERT INTO user(userID, lastName) VALUES(1, 'Lehmann')");
// the following line will throw an exception because the primary key is not unique
pStmt->SqlStatement("INSERT INTO user(userID, lastName) VALUES(1, 'Bergmann')");
}
catch(Kompex::SQLiteException &exception)
{
std::cerr << "Exception Occured" << std::endl;
exception.Show();
}
Kompex::SQLiteDatabase *pDatabase = new Kompex::SQLiteDatabase("scores.db", SQLITE_OPEN_READWRITE, 0);
// move database to memory, so that we are work on the memory database hence
pDatabase->MoveDatabaseToMemory();
Kompex::SQLiteStatement *pStmt = new Kompex::SQLiteStatement(pDatabase);
// insert some data sets into the memory database
pStmt->SqlStatement("INSERT INTO score(id, lastScore, avgScore) VALUES(1, 429, 341)");
pStmt->SqlStatement("INSERT INTO score(id, lastScore, avgScore) VALUES(2, 37, 44)");
pStmt->SqlStatement("INSERT INTO score(id, lastScore, avgScore) VALUES(3, 310, 280)");
// save the memory database to a file
// if you don't do it, all database changes will be lost after closing the memory database
pDatabase->SaveDatabaseFromMemoryToFile("newScores.db");
pStmt->Sql("SELECT * FROM user WHERE lastName = 'Lehmann';");
// process all results
while(pStmt->FetchRow())
{
std::cout << "firstName: " << pStmt->GetColumnString("firstName") << std::endl;
std::cout << "age: " << pStmt->GetColumnInt("age") << std::endl;
}
// do not forget to clean-up
pStmt->FreeQuery();
// the first way
pStmt->SqlStatement("UPDATE user SET weight=51.5, age=18 WHERE firstName='Carlene'");
// the second way - with a prepared statement
pStmt->Sql("UPDATE user SET lastName=@lastName, age=@age WHERE userID=@userID");
// bind an integer to the prepared statement
pStmt->BindString(1, "Urushihara"); // bind lastName
pStmt->BindInt(2, 56); // bind age
pStmt->BindInt(3, 2); // bind userID
// execute it and clean-up
pStmt->ExecuteAndFree();