4

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.

jww
  • 97,681
  • 90
  • 411
  • 885
  • 2
    Consider adding a [mcve], and include the error! – Tas Oct 14 '17 at 01:24
  • 1
    Added some sample code. – Hiten Naresh Vasnani Oct 17 '17 at 17:04
  • `VACUUM;` works for me in SQLite 3.22.0. Perhaps `filepath` is wrong. Maybe you should `string reason = ...;` and then `throw 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](http://stackoverflow.com/help/mcve). – jww Oct 31 '19 at 15:02

2 Answers2

1

you can use sqlite3_exec() api from sqlite3.

sqlite3_exec(db, "VACUUM", 0, 0, 0);

rakesh.sahu
  • 465
  • 8
  • 18
-3
PRAGMA auto_vacuum = FULL;

Then you won't have to worry about it.

slfan
  • 8,950
  • 115
  • 65
  • 78