108 lines
2.0 KiB
C++
108 lines
2.0 KiB
C++
#include "sql.hpp"
|
|
|
|
#include <cstdio>
|
|
#include <cstdlib>
|
|
|
|
#include <sqlite3.h>
|
|
|
|
void sql::init()
|
|
{
|
|
sqlite3_initialize();
|
|
sqlite3 *db;
|
|
char *zErrMsg = 0;
|
|
int rc;
|
|
|
|
/* Open database */
|
|
rc = sqlite3_open(DB_NAME, &db);
|
|
|
|
if (rc)
|
|
{
|
|
fprintf(stderr, "Can't open database: %s\n", sqlite3_errmsg(db));
|
|
return;
|
|
}
|
|
else
|
|
{
|
|
fprintf(stdout, "Opened database successfully\n");
|
|
}
|
|
|
|
/* Execute SQL statement */
|
|
rc = sqlite3_exec(db, create_table, nullptr, 0, &zErrMsg);
|
|
|
|
if (rc != SQLITE_OK)
|
|
{
|
|
fprintf(stderr, "SQL error: %s\n", zErrMsg);
|
|
sqlite3_free(zErrMsg);
|
|
}
|
|
else
|
|
{
|
|
fprintf(stdout, "Table created successfully\n");
|
|
}
|
|
|
|
/* Execute SQL statement */
|
|
rc = sqlite3_exec(db, create_index, nullptr, 0, &zErrMsg);
|
|
|
|
if (rc != SQLITE_OK)
|
|
{
|
|
fprintf(stderr, "SQL error: %s\n", zErrMsg);
|
|
sqlite3_free(zErrMsg);
|
|
}
|
|
else
|
|
{
|
|
fprintf(stdout, "Index created successfully\n");
|
|
}
|
|
|
|
sqlite3_close(db);
|
|
}
|
|
|
|
void sql::shutdown()
|
|
{
|
|
sqlite3_shutdown();
|
|
}
|
|
|
|
namespace sql
|
|
{
|
|
int open(const char *filename, sqlite3 **ppDb)
|
|
{
|
|
return sqlite3_open(filename, ppDb);
|
|
}
|
|
int prepare_v2(sqlite3 *db, const char *zSql, int nByte, sqlite3_stmt **pStmt, const char **pzTail)
|
|
{
|
|
return sqlite3_prepare_v2(db, zSql, nByte, pStmt, pzTail);
|
|
}
|
|
int bind_int64(sqlite3_stmt *pStmt, int column, int64_t value)
|
|
{
|
|
return sqlite3_bind_int64(pStmt, column, value);
|
|
}
|
|
int step(sqlite3_stmt *pStmt)
|
|
{
|
|
return sqlite3_step(pStmt);
|
|
}
|
|
int column_count(sqlite3_stmt *pStmt)
|
|
{
|
|
return sqlite3_column_count(pStmt);
|
|
}
|
|
int column_type(sqlite3_stmt *pStmt, int column)
|
|
{
|
|
return sqlite3_column_type(pStmt, column);
|
|
}
|
|
int64_t column_int64(sqlite3_stmt *pStmt, int column)
|
|
{
|
|
return sqlite3_column_int64(pStmt, column);
|
|
}
|
|
int finalize(sqlite3_stmt *pStmt)
|
|
{
|
|
return sqlite3_finalize(pStmt);
|
|
}
|
|
int reset(sqlite3_stmt *pStmt)
|
|
{
|
|
return sqlite3_reset(pStmt);
|
|
}
|
|
int close(sqlite3 *db)
|
|
{
|
|
return sqlite3_close(db);
|
|
}
|
|
const char *errmsg(sqlite3 *db)
|
|
{
|
|
return sqlite3_errmsg(db);
|
|
}
|
|
} |