C++利用Sqlite3写一个辅助类能够进行数据库的创建增删改查等功能,支持数据库备份功能
时间: 2023-08-18 18:06:05 浏览: 146
c++实现数据库的连接以及对应的增删改查功能
好的,以下是一个简单的 C++ 辅助类,可以进行 SQLite3 数据库的创建、增、删、改、查和备份等操作。
首先需要安装 SQLite3,并在项目中链接 SQLite3 库。
辅助类头文件(DatabaseHelper.h):
```c++
#ifndef DATABASEHELPER_H
#define DATABASEHELPER_H
#include <sqlite3.h>
#include <string>
class DatabaseHelper
{
public:
DatabaseHelper(const std::string& dbname);
virtual ~DatabaseHelper();
bool open();
void close();
bool execute(const std::string& sql);
bool tableExists(const std::string& tablename);
bool createTable(const std::string& tablename, const std::string& fields);
bool dropTable(const std::string& tablename);
bool insert(const std::string& tablename, const std::string& fields, const std::string& values);
bool update(const std::string& tablename, const std::string& fields, const std::string& values, const std::string& where);
bool remove(const std::string& tablename, const std::string& where);
bool backup(const std::string& backupfile);
private:
sqlite3* db;
std::string dbname;
};
#endif // DATABASEHELPER_H
```
辅助类实现文件(DatabaseHelper.cpp):
```c++
#include "DatabaseHelper.h"
#include <iostream>
#include <fstream>
DatabaseHelper::DatabaseHelper(const std::string& dbname)
: db(nullptr), dbname(dbname)
{
}
DatabaseHelper::~DatabaseHelper()
{
close();
}
bool DatabaseHelper::open()
{
int rc = sqlite3_open(dbname.c_str(), &db);
if (rc != SQLITE_OK)
{
std::cerr << "Can't open database: " << sqlite3_errmsg(db) << std::endl;
sqlite3_close(db);
db = nullptr;
return false;
}
return true;
}
void DatabaseHelper::close()
{
if (db)
{
sqlite3_close(db);
db = nullptr;
}
}
bool DatabaseHelper::execute(const std::string& sql)
{
char* errmsg = nullptr;
int rc = sqlite3_exec(db, sql.c_str(), nullptr, nullptr, &errmsg);
if (rc != SQLITE_OK)
{
std::cerr << "SQL error: " << errmsg << std::endl;
sqlite3_free(errmsg);
return false;
}
return true;
}
bool DatabaseHelper::tableExists(const std::string& tablename)
{
std::string sql = "SELECT COUNT(*) FROM sqlite_master WHERE type='table' AND name='" + tablename + "'";
sqlite3_stmt* stmt = nullptr;
int rc = sqlite3_prepare_v2(db, sql.c_str(), -1, &stmt, nullptr);
if (rc != SQLITE_OK)
{
std::cerr << "Can't prepare SQL statement: " << sqlite3_errmsg(db) << std::endl;
return false;
}
rc = sqlite3_step(stmt);
if (rc != SQLITE_ROW)
{
std::cerr << "Can't get result: " << sqlite3_errmsg(db) << std::endl;
sqlite3_finalize(stmt);
return false;
}
int count = sqlite3_column_int(stmt, 0);
sqlite3_finalize(stmt);
return count == 1;
}
bool DatabaseHelper::createTable(const std::string& tablename, const std::string& fields)
{
if (tableExists(tablename))
{
std::cerr << "Table already exists: " << tablename << std::endl;
return false;
}
std::string sql = "CREATE TABLE " + tablename + "(" + fields + ")";
return execute(sql);
}
bool DatabaseHelper::dropTable(const std::string& tablename)
{
if (!tableExists(tablename))
{
std::cerr << "Table does not exist: " << tablename << std::endl;
return false;
}
std::string sql = "DROP TABLE " + tablename;
return execute(sql);
}
bool DatabaseHelper::insert(const std::string& tablename, const std::string& fields, const std::string& values)
{
std::string sql = "INSERT INTO " + tablename + "(" + fields + ") VALUES(" + values + ")";
return execute(sql);
}
bool DatabaseHelper::update(const std::string& tablename, const std::string& fields, const std::string& values, const std::string& where)
{
std::string sql = "UPDATE " + tablename + " SET " + fields + "='" + values + "' WHERE " + where;
return execute(sql);
}
bool DatabaseHelper::remove(const std::string& tablename, const std::string& where)
{
std::string sql = "DELETE FROM " + tablename + " WHERE " + where;
return execute(sql);
}
bool DatabaseHelper::backup(const std::string& backupfile)
{
std::ifstream src(dbname, std::ios::binary);
std::ofstream dst(backupfile, std::ios::binary);
dst << src.rdbuf();
return true;
}
```
使用示例:
```c++
#include "DatabaseHelper.h"
int main()
{
// 创建辅助类对象
DatabaseHelper dbhelper("test.db");
// 打开数据库
if (!dbhelper.open())
{
return -1;
}
// 创建表
dbhelper.createTable("user", "id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, age INTEGER");
// 插入数据
dbhelper.insert("user", "name, age", "'Tom', 20");
// 查询数据
sqlite3_stmt* stmt = nullptr;
std::string sql = "SELECT * FROM user";
int rc = sqlite3_prepare_v2(dbhelper.db, sql.c_str(), -1, &stmt, nullptr);
if (rc == SQLITE_OK)
{
while (sqlite3_step(stmt) == SQLITE_ROW)
{
int id = sqlite3_column_int(stmt, 0);
const unsigned char* name = sqlite3_column_text(stmt, 1);
int age = sqlite3_column_int(stmt, 2);
std::cout << "id=" << id << ", name=" << name << ", age=" << age << std::endl;
}
sqlite3_finalize(stmt);
}
// 更新数据
dbhelper.update("user", "age", "21", "name='Tom'");
// 删除数据
dbhelper.remove("user", "name='Tom'");
// 数据库备份
dbhelper.backup("test_backup.db");
// 关闭数据库
dbhelper.close();
return 0;
}
```
注意:上述代码只是一个简单的示例,没有进行错误处理、SQL 注入等安全性考虑,实际使用时需要根据具体需求进行完善。
阅读全文