nlib
oss/sqlfromc/sqlfromc.cpp

An example of using SQLite from C/C++. For simplicity, the database is constructed in memory.

/*---------------------------------------------------------------------------*
Project: CrossRoad
Copyright (C)2012-2016 Nintendo. All rights reserved.
These coded instructions, statements, and computer programs contain
proprietary information of Nintendo of America Inc. and/or Nintendo
Company Ltd., and are protected by Federal copyright law. They may
not be disclosed to third parties or copied or duplicated in any form,
in whole or in part, without the prior written consent of Nintendo.
*---------------------------------------------------------------------------*/
#include "nn/nlib/Config.h"
#include "sqlite3.h"
static int callback(void* arg, int columns, char** value, char** name) {
NLIB_UNUSED(arg);
for (int i = 0; i < columns; ++i) {
nlib_printf("[%s=%s] ", name[i], value[i]);
}
nlib_printf("\n");
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;
// Creates a database
int result;
result = sqlite3_open(":memory:", &db); // on memory database
if (result != SQLITE_OK) return false;
// Creates a table
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) {
nlib_printf("%s\n", errmsg);
sqlite3_free(errmsg);
sqlite3_close(db);
return false;
}
// Compiles a statment.
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;
}
// Registers the menus by binding 'name' and 'price' to the compiled statement.
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); // Rereases statement
// Prints all the menus.
nlib_printf("> SELECT * FROM t_menu;\n");
result = sqlite3_exec(db,
"SELECT * FROM t_menu",
callback,
NULL,
&errmsg);
nlib_printf("\n");
if (result != SQLITE_OK) {
nlib_printf("%s\n", errmsg);
sqlite3_free(errmsg);
sqlite3_close(db);
return false;
}
// Prints all the menus beginning with 't'.
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);
nlib_printf("\n");
if (result != SQLITE_OK) {
nlib_printf("%s\n", errmsg);
sqlite3_free(errmsg);
sqlite3_close(db);
return false;
}
// Sorts by price.
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);
nlib_printf("\n");
if (result != SQLITE_OK) {
nlib_printf("%s\n", errmsg);
sqlite3_free(errmsg);
sqlite3_close(db);
return false;
}
sqlite3_close(db);
return true;
}
//
// sqlite3 in nlib uses nmalloc/nfree in libnx_oss.a.
// You have to link libnx_oss.a, and set up nmalloc.
//
#ifdef NLIB_HAS_VIRTUALMEMORY
extern "C" const NMallocSettings g_NMallocSettings = { NULL, 1024 * 1024 * 16, 0 };
#else
const size_t heapmem_size = 1024 * 1024 * 16;
NLIB_ALIGNAS(4096) static char heapmem[heapmem_size];
extern "C" const NMallocSettings g_NMallocSettings = { heapmem, heapmem_size, 0 };
#endif
NLIB_MAINFUNC