nlib
oss/readsqldb/readsqldb.cpp

An example of using SQLite from C/C++. Loads an existing database.

/*--------------------------------------------------------------------------------*
Project: CrossRoad
Copyright (C)Nintendo All rights reserved.
These coded instructions, statements, and computer programs contain proprietary
information of Nintendo and/or its licensed developers and are protected by
national and international copyright laws. 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.
The content herein is highly confidential and should be handled accordingly.
*--------------------------------------------------------------------------------*/
#include "nn/nlib/Config.h"
#include "sqlite3.h"
#if defined(CAFE) || defined(NN_PLATFORM_CTR)
#endif
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;
}
NLIB_PATHMAPPER_FORSAMPLE
bool SampleMain(int, char**) {
// Initializes g_pathmapper which NLIB_PATHMAPPER_FORSAMPLE defines
InitPathMapperForSample();
// converts an URI to the corresponding native path
char dbdir[1024];
g_pathmapper.ResolvePath(NULL, dbdir, "nlibpath:///readonly/");
nlib_printf("DBDIR is '%s'\n", dbdir);
sqlite3_nlib_set_directory(SQLITE_NLIB_DATA_DIRECTORY_TYPE, dbdir);
sqlite3_nlib_set_directory(SQLITE_NLIB_TEMP_DIRECTORY_TYPE, dbdir);
sqlite3* db;
// Reads the database from file.
// sample.db is the same as the one that sqlfromc.cpp sample generates.
int result;
result = sqlite3_open("sample.db", &db); // Reads ${DBDIR}/sample.db
if (result != SQLITE_OK) return false;
// Prints all the menus.
char* errmsg = NULL;
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.
//
#if defined(CAFE) || defined(NN_PLATFORM_CTR)
#ifdef NLIB_HAS_VIRTUALMEMORY
extern "C" const NMallocSettings g_nmalloc_settings = { 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_nmalloc_settings = { heapmem, heapmem_size, 0 };
#endif
#endif
NLIB_MAINFUNC