#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;
}
static bool BindArgs(sqlite3_stmt* statement, int idx, const char* str, int price) {
if (sqlite3_reset(statement) != SQLITE_OK) return false;
if (sqlite3_bind_int(statement, 1, idx) != SQLITE_OK) return false;
if (sqlite3_bind_text(statement, 2, str, -1, SQLITE_STATIC) != SQLITE_OK) return false;
if (sqlite3_bind_int(statement, 3, price) != SQLITE_OK) return false;
if (sqlite3_step(statement) != SQLITE_OK) return false;
return true;
}
bool SampleMain(int, char**) {
sqlite3* db;
int result;
result = sqlite3_open(":memory:", &db);
if (result != SQLITE_OK) return false;
char* errmsg = NULL;
result = sqlite3_exec(db,
"CREATE TABLE IF NOT EXISTS t_menu(id INTEGER PRIMARY KEY, name VARCHAR(32), price INTEGER)",
0, 0, &errmsg);
if (result != SQLITE_OK) {
sqlite3_free(errmsg);
sqlite3_close(db);
return false;
}
sqlite3_stmt* statement;
result = sqlite3_prepare_v2(db,
"INSERT INTO t_menu (id, name, price) VALUES ( ?, ?, ? )",
-1, &statement, NULL);
if (result != SQLITE_OK) {
sqlite3_close(db);
return false;
}
BindArgs(statement, 1, "Curry Rice", 500);
BindArgs(statement, 2, "Tsukimi Noodle", 300);
BindArgs(statement, 3, "Omelette", 700);
BindArgs(statement, 4, "Hamburg Steak", 1000);
BindArgs(statement, 5, "Ramen", 600);
BindArgs(statement, 6, "Beef Bowl", 280);
BindArgs(statement, 7, "Pork Cutlet Rice Bowl", 900);
BindArgs(statement, 8, "Tendon", 1200);
sqlite3_finalize(statement);
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