38 lines
1010 B
C++
38 lines
1010 B
C++
#include "sqlite_file_executor.h"
|
|
|
|
#include "sqlite_statement.h"
|
|
#include <iostream>
|
|
|
|
SQLiteFileExecutor::SQLiteFileExecutor(const std::string &dbPath) {
|
|
// WARNING: The database is not blocked
|
|
if (sqlite3_open(dbPath.c_str(), &db_) != SQLITE_OK) {
|
|
std::cerr << "Failed to open database:" << sqlite3_errmsg(db_) << std::endl;
|
|
db_ = nullptr;
|
|
}
|
|
}
|
|
|
|
SQLiteFileExecutor::~SQLiteFileExecutor() {
|
|
if (db_) {
|
|
sqlite3_close(db_);
|
|
}
|
|
}
|
|
|
|
// Execute a SQL file
|
|
void SQLiteFileExecutor::executeSQLFile(const std::string &sqlFilePath) {
|
|
if (db_ == nullptr) {
|
|
throw std::runtime_error("Database is not open");
|
|
}
|
|
|
|
std::string sqlContent;
|
|
if (!read(sqlFilePath, sqlContent)) {
|
|
throw std::runtime_error("Failed to read SQL file: " + sqlFilePath);
|
|
}
|
|
|
|
char *errorMessage = nullptr;
|
|
if (sqlite3_exec(db_, sqlContent.c_str(), nullptr, nullptr, &errorMessage) !=
|
|
SQLITE_OK) {
|
|
std::cerr << "Error executing SQL:" << errorMessage << std::endl;
|
|
sqlite3_free(errorMessage);
|
|
}
|
|
}
|