带有sqlite数据库的C++类

C++ Class with sqlite database

本文关键字:C++ 数据库 sqlite 带有      更新时间:2023-10-16

我使用的是纯c++编码的Qt创建者IDE。我需要创建一个包含2个成员变量a&B(也添加在表中)和3个公共和私人功能。

公共函数:检查数据库是否存在,如果存在则打开并读取数据A&B从private调用这3个函数。第二个函数是生成一个26个字符的数组并存储在25位数字中。使用srand()返回它-getA()。第三个函数是采用sha256散列串接第二个函数+时间戳-getB()

私有函数:创建一个sqlite数据库,初始化一个基本的表结构(char a|char B|blob C)。第三个函数是生成详细信息,存储到数据库和a&B.从该函数调用getA()getB()以生成详细信息。

代码:

class Init  {
char AppID[50];
char AppCode[50];
//create sqlite database
int DbInit() {
sqlite3 *db;
if(sqlite3_open("Init.db", &db))
cout<<"opened database successfullyn";
else
cout<< "failed to create databasen";
return 0;
}
//initialize table
int tableGen() {
string query = "CREATE TABLE tableInit(AppID TEXT(25), AppCode TEXT(60),Cert BLOB)";
sqlite3_stmt *stmnt;
cout<< "creating table statement"<<endl;
sqlite3_prepare(db, query.c_str() , query.size(), &stmnt, NULL );
if(sqlite3_step(stmnt)  !=SQLITE_DONE)
cout<< "didnt create table"<<endl;

}
//generate details to store to database and also into member functions.
int AppGen() {
both getappid and get appcode is called to generate details.
}

public:
Init() {}

//check whether database exists if exists read data else call the private functions .
bool start() {
int rc = sqlite3_open("Init.db", &db, SQLITE_OPEN_READONLY);
if(rc = SQLITE_OK) {
sqlite3_stmt *statement;
sqlite3_prepare(db,"SELECT AppID,AppCode FROM tableInit",-1,&statement,0);
sqlite3_step(statement);
}
else
Init::DbInit();
Init::tablegen();
Init::AppGen();

}
// generate appid 
int GetAppID() {
generate a 25 digit number and store it in an array using rand()
}
//take sha256 hash concating appid + time-stamp
int  getcode() {
}

如何定义appgen()appid()code()

我认为这个示例会有所帮助http://www.tutorialspoint.com/sqlite/sqlite_c_cpp.htm