sqlite3单例c++实现
时间: 2023-11-23 17:57:03 浏览: 141
以下是SQLite3单例C++实现的代码示例:
```cpp
#include <iostream>
#include <sqlite3.h>
class SQLiteDB {
public:
static SQLiteDB& getInstance() {
static SQLiteDB instance; return instance;
}
int open(const char* filename) {
return sqlite3_open(filename, &db);
}
int close() {
return sqlite3_close(db);
}
sqlite3* getDB() const {
return db;
}
private:
SQLiteDB() : db(nullptr) {}
~SQLiteDB() {}
SQLiteDB(const SQLiteDB&) = delete;
SQLiteDB& operator=(const SQLiteDB&) = delete;
sqlite3* db;
};
int main() {
SQLiteDB& db = SQLiteDB::getInstance();
int rc = db.open("test.db");
if (rc) {
std::cerr << "Can't open database: " << sqlite3_errmsg(db.getDB()) << std::endl;
return 1;
}
std::cout << "Opened database successfully" << std::endl;
db.close();
return 0;
}
```
阅读全文