An example of using SQLite from C/C++. Loads an existing database.
#ifndef DBDIR
# error
#endif
#define STRING_(str) #str
#define STRING(str) STRING_(str)
#include "sqlite3.h"
static int callback(void* arg, int columns, char** value, char** name) {
NLIB_UNUSED(arg);
for (int i = 0; i < columns; ++i) {
}
return 0;
}
bool SampleMain(int, char**) {
const char* dbdir = STRING(DBDIR);
sqlite3* db;
int result;
result = sqlite3_open("sample.db", &db);
if (result != SQLITE_OK) return false;
char* errmsg = NULL;
result = sqlite3_exec(db,
"SELECT * FROM t_menu",
callback,
NULL,
&errmsg);
if (result != SQLITE_OK) {
sqlite3_free(errmsg);
sqlite3_close(db);
return false;
}
nlib_printf(
"> SELECT * FROM t_menu WHERE LIKE 't%%';\n");
result = sqlite3_exec(db,
"SELECT * FROM t_menu WHERE name LIKE 't%'",
callback,
NULL,
&errmsg);
if (result != SQLITE_OK) {
sqlite3_free(errmsg);
sqlite3_close(db);
return false;
}
nlib_printf(
"> SELECT * FROM t_menu ORDER BY price ASC;\n");
result = sqlite3_exec(db,
"SELECT * FROM t_menu ORDER BY price ASC",
callback,
NULL,
&errmsg);
if (result != SQLITE_OK) {
sqlite3_free(errmsg);
sqlite3_close(db);
return false;
}
sqlite3_close(db);
return true;
}
#ifdef NLIB_HAS_VIRTUALMEMORY
extern "C" const NMallocSettings g_NMallocSettings = { NULL, 1024 * 1024 * 16, 0 };
#else
const size_t heapmem_size = 1024 * 1024 * 16;
extern "C" const
NMallocSettings g_NMallocSettings = { heapmem, heapmem_size, 0 };
#endif
NLIB_MAINFUNC