目录
1 概述
Windows、Linux平台下先安装SQLite3数据库,嵌入式平台需要先交叉编译移植SQLite3数据库。
2 实现
2.1 databasesql.h
#ifndef DATABASESQL_H
#define DATABASESQL_H
// phonebook
#define SQL_CREATE_PHONEBOOK "create table if not exists phonebook(addr text,N_name text,FN_name text,pinyin text,number text,groupID int,type int);"
#define SQL_INSERT_PHONEBOOK "insert into phonebook values(\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",%d,%d);"
#define SQL_SELECT_PHONEBOOK_ALL "select * from phonebook where addr=\"%s\";"
#define SQL_DELETE_PHONEBOOK_UNADDR "delete from phonebook where addr!=\"%s\";"
#define SQL_DELETE_PHONEBOOK "delete from phonebook;"
// phone_history
#define SQL_CREATE_PHONEHISTORY "create table if not exists phone_history(id integer primary key autoincrement,addr text,N_name text,FN_name text,number text,time text,state int,type int);"
#define SQL_INSERT_PHONEHISTORY "insert into phone_history values(null,\"%s\",\"%s\",\"%s\",\"%s\",\"%s\",%d,%d);"
#define SQL_SELECT_PHONEHISTORY_ALL "select * from phone_history where addr=\"%s\" order by id asc;"
#define SQL_DELETE_PHONEHISTORY_UNADDR "delete from phone_history where addr!=\"%s\";"
#define SQL_DELETE_PHONEHISTORY "delete from phone_history;"
#endif // DATABASESQL_H
2.2 appdatabase.h
#ifndef APPDATABASE_H
#define APPDATABASE_H
#include <vector>
#include <map>
#include <memory>
#include <string>
#define appDb AppDatabase::getInstance()
struct sqlite3;
class AppDatabase
{
public:
static std::shared_ptr<AppDatabase> getInstance()
{
if(self == NULL)
{
self = std::shared_ptr<AppDatabase>(new AppDatabase());
}
return self;
}
~AppDatabase();
bool insertData(const std::string sqlStr);
bool updateData(const std::string sqlStr);
bool selectData(const std::string sqlStr, std::vector< std::map<std::string,std::string> > &valuesVector);
bool deleteData(const std::string sqlStr);
bool openDatabase(const std::string dbFileName);
bool initDatabase();
bool beginTransaction();
bool commitTransaction();
bool rollbackTransaction();
// phonebook
bool insertPhoneBook(std::string addr,std::string N_name,std::string FN_name,std::string pinyin,std::string number,int groupID,int type);
bool selectPhoneBook(std::string addr,std::vector< std::map<std::string,std::string> > &valuesVector);
bool deletePhoneBookUnAddr(std::string addr);
bool deletePhoneBook();
// phone_history
bool insertPhoneHistory(std::string addr,std::string N_name,std::string FN_name,std::string number,std::string time,int state,int type);
bool selectPhoneHistory(std::string addr,std::vector< std::map<std::string,std::string> > &valuesVector);
bool deletePhoneHistoryUnAddr(std::string addr);
bool deletePhoneHistory();
private:
AppDatabase();//禁止构造函数
AppDatabase(const AppDatabase&){}//禁止拷贝构造函数
AppDatabase & operator=(const AppDatabase &){}//禁止赋值拷贝构造函数
private:
static std::shared_ptr<AppDatabase> self;
sqlite3 *db;
std::string dbFileName;
};
#endif // APPDATABASE_H
2.3 appdatabase.cpp
#include "appdatabase.h"
#include <iostream>
#include <stdio.h>
#include "sqlite3.h"
#include "databasesql.h"
std::shared_ptr<AppDatabase> AppDatabase::self = NULL;
AppDatabase::AppDatabase()
: dbFileName("/media/flash/nvm/Sunplus/appdatabase.db"),
db(NULL)
{
if(openDatabase(dbFileName))
initDatabase();
}
AppDatabase::~AppDatabase()
{
if(db != NULL){
sqlite3_close(db);
}
}
bool AppDatabase::insertData(const std::string sqlStr)
{
// std::cout<<"[SQL] "<<sqlStr<<std::endl;
char *errMsg;
int rs = sqlite3_exec(db,sqlStr.c_str(),0,0,&errMsg);
if(rs!=SQLITE_OK){
// std::cout<<"[SQL] error: "<<errMsg<<std::endl;
sqlite3_free(errMsg);
return false;
}
return true;
}
bool AppDatabase::updateData(const std::string sqlStr)
{
// std::cout<<"[SQL] "<<sqlStr<<std::endl;
char *errMsg;
int rs = sqlite3_exec(db,sqlStr.c_str(),0,0,&errMsg);
if(rs!=SQLITE_OK){
// std::cout<<"[SQL] error: "<<errMsg<<std::endl;
sqlite3_free(errMsg);
return false;
}
return true;
}
bool AppDatabase::selectData(const std::string sqlStr, std::vector< std::map<std::string,std::string> > &valuesVector)
{
// std::cout<<"[SQL] "<<sqlStr<<std::endl;
char *zErrMsg = NULL;
int rc;
int nrow;
int ncolumn;
char **azResult = NULL;
int ret = sqlite3_get_table(db, sqlStr.c_str(), &azResult, &nrow, &ncolumn, &zErrMsg);
if(ret != 0){
// std::cout << "[SQL] error: " << zErrMsg << std::endl;
sqlite3_free(zErrMsg);
}
if(azResult == NULL)
return false;
for( int r=0; r<nrow; r++ )
{
std::map<std::string,std::string> valueMap;
for( int c=0; c<ncolumn; c++ )
{
valueMap[azResult[c]] = azResult[(r+1)*ncolumn+c];
}
valuesVector.push_back(valueMap);
}
sqlite3_free_table(azResult);
return valuesVector.empty() ? false : true;
}
bool AppDatabase::deleteData(const std::string sqlStr)
{
// std::cout<<"[SQL] "<<sqlStr<<std::endl;
char *errMsg;
int rs = sqlite3_exec(db,sqlStr.c_str(),0,0,&errMsg);
if(rs!=SQLITE_OK){
// std::cout<<"[SQL] error: "<<errMsg<<std::endl;
sqlite3_free(errMsg);
return false;
}
return true;
}
bool AppDatabase::openDatabase(const std::string dbFileName)
{
// 判断是否启用串行模式
// int rc = SQLITE_DONE;
// if (sqlite3_threadsafe() != 1) {
// rc = sqlite3_config(SQLITE_CONFIG_SERIALIZED);//设置为串行模式
// if (rc != SQLITE_OK) {
// std::cout<<"SQLite3 is not compiled with serialized threading mode!"<<std::endl;
// return false;
// }
// }
// sqlite初始化
int rc = sqlite3_initialize();
if (rc != SQLITE_OK) {
// std::cout<<"database_init sqlite_initalize error"<<std::endl;
return false;;
}
rc = sqlite3_open(dbFileName.c_str(),&db);
if(rc != SQLITE_OK){
// std::cout<<"openDatabase error: "<<sqlite3_errmsg(db)<<std::endl;
return false;
}
return true;
}
bool AppDatabase::initDatabase()
{
if (db != NULL)
{
char *errMsg;
//创建表phonebook
// std::cout<<"[SQL] "<<SQL_CREATE_PHONEBOOK<<std::endl;
int rs = sqlite3_exec(db,SQL_CREATE_PHONEBOOK,0,0,&errMsg);
if(rs!=SQLITE_OK){
// std::cout<<"[SQL] "<<errMsg<<std::endl;
return false;
}
//创建表phone_history
// std::cout<<"[SQL] "<<SQL_CREATE_PHONEHISTORY<<std::endl;
rs = sqlite3_exec(db,SQL_CREATE_PHONEHISTORY,0,0,&errMsg);
if(rs!=SQLITE_OK){
// std::cout<<"[SQL] "<<errMsg<<std::endl;
return false;
}
return true;
}
return false;
}
bool AppDatabase::beginTransaction()
{
char *errMsg;
int rs = sqlite3_exec(db,"begin transaction",0,0,&errMsg);
if(rs!=SQLITE_OK){
// std::cout<<"[SQL] "<<errMsg<<std::endl;
return false;
}
return true;
}
bool AppDatabase::commitTransaction()
{
char *errMsg;
int rs = sqlite3_exec(db,"commit transaction",0,0,&errMsg);
if(rs!=SQLITE_OK){
// std::cout<<"[SQL] "<<errMsg<<std::endl;
return false;
}
return true;
}
bool AppDatabase::rollbackTransaction()
{
char *errMsg;
int rs = sqlite3_exec(db,"rollback transaction",0,0,&errMsg);
if(rs!=SQLITE_OK){
// std::cout<<"[SQL] "<<errMsg<<std::endl;
return false;
}
return true;
}
// phonebook
bool AppDatabase::insertPhoneBook(std::string addr,std::string N_name,std::string FN_name,std::string pinyin,std::string number,int groupID,int type)
{
char sqlStr[512] = {0};
sprintf(sqlStr,SQL_INSERT_PHONEBOOK,addr.c_str(),N_name.c_str(),FN_name.c_str(),pinyin.c_str(),number.c_str(),groupID,type);
return insertData(sqlStr);
}
bool AppDatabase::selectPhoneBook(std::string addr,std::vector< std::map<std::string,std::string> > &valuesVector)
{
char sqlStr[512] = {0};
sprintf(sqlStr,SQL_SELECT_PHONEBOOK_ALL,addr.c_str());
return selectData(sqlStr,valuesVector);
}
bool AppDatabase::deletePhoneBookUnAddr(std::string addr)
{
char sqlStr[512] = {0};
sprintf(sqlStr,SQL_DELETE_PHONEBOOK_UNADDR,addr.c_str());
return deleteData(sqlStr);
}
bool AppDatabase::deletePhoneBook()
{
char sqlStr[512] = {0};
sprintf(sqlStr,SQL_DELETE_PHONEBOOK);
return deleteData(sqlStr);
}
// phone_history
bool AppDatabase::insertPhoneHistory(std::string addr,std::string N_name,std::string FN_name,std::string number,std::string time,int state,int type)
{
char sqlStr[512] = {0};
sprintf(sqlStr,SQL_INSERT_PHONEHISTORY,addr.c_str(),N_name.c_str(),FN_name.c_str(),number.c_str(),time.c_str(),state,type);
return insertData(sqlStr);
}
bool AppDatabase::selectPhoneHistory(std::string addr,std::vector< std::map<std::string,std::string> > &valuesVector)
{
char sqlStr[512] = {0};
sprintf(sqlStr,SQL_SELECT_PHONEHISTORY_ALL,addr.c_str());
return selectData(sqlStr,valuesVector);
}
bool AppDatabase::deletePhoneHistoryUnAddr(std::string addr)
{
char sqlStr[512] = {0};
sprintf(sqlStr,SQL_DELETE_PHONEHISTORY_UNADDR,addr.c_str());
return deleteData(sqlStr);
}
bool AppDatabase::deletePhoneHistory()
{
char sqlStr[512] = {0};
sprintf(sqlStr,SQL_DELETE_PHONEHISTORY);
return deleteData(sqlStr);
}
3 使用示例
#include <iostream>
#include <string>
#include "appdatabase.h"
int main(int argc , char *argv[])
{
std::string addrStr = "0f:32:9a:03:b2:44";
appDb->insertPhoneBook(addrStr,"","张三","ZhangSan","12345678910",1,0);
appDb->insertPhoneBook(addrStr,"","李四","LiSi","10987654321",1,0);
appDb->insertPhoneHistory(addrStr,"","张三","12345678910","20210406T102501",1,1);
appDb->insertPhoneHistory(addrStr,"","张三","12345678910","20210406T102401",1,1);
appDb->insertPhoneHistory(addrStr,"","","10086","20210406T102304",1,1);
appDb->insertPhoneHistory(addrStr,"","李四","10987654321","20210406T102303",1,1);
appDb->insertPhoneHistory(addrStr,"","","10086","20210406T102302",1,1);
appDb->insertPhoneHistory(addrStr,"","李四","10987654321","20210406T102301",1,1);
std::vector< std::map<std::string,std::string> > valuesVector;
appDb->selectPhoneBook(addrStr,valuesVector);
addrStr = "0f:32:9a:03:b2:00";
appDb->deletePhoneBookUnAddr(addrStr);
valuesVector.clear();
appDb->selectPhoneBook(addrStr,valuesVector);
valuesVector.clear();
appDb->selectPhoneHistory(addrStr,valuesVector);
return 0;
}