I am trying to programmatically execute the VACUUM command in C++ using the Sqlite3 library for C++. See reference at C-language Interface Specification for SQLite.
Can someone give a code snippet of how to do this? I tried calling this but it gives an exception:
This code is in my SqliteDb.cpp helper class.
void SqliteDb::executeSql(const string& sqlStatement) { char* errMsg = NULL; sqlite3_exec(db, sqlStatement.c_str(), NULL, NULL, &errMsg); if (errMsg != NULL) { string reason = string("Error in") + sqlStatement + " " + errMsg; sqlite3_free(errMsg); __throw_sqlitedb(reason); } } In my main class I did:
try{ db = new SqliteDb(filepath); db->executeSql("VACUUM;"); } catch (std::exception e) { printf("EXCEPTION occurred %s", e.what()); } The output is
EXCEPTION occurred std::exception The SqliteDb.cpp is a tested class and works well for other components that use this class.
VACUUM;works for me in SQLite 3.22.0. Perhapsfilepathis wrong. Maybe you shouldstring reason = ...;and thenthrow std::runtime_error(reason.c_str()). It should allow you to catch something meaningful. Also see How to create a Minimal, Complete, and Verifiable example.