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.

3
  • 2
    Consider adding a minimal reproducible example, and include the error! Commented Oct 14, 2017 at 1:24
  • 1
    Added some sample code. Commented Oct 17, 2017 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. Commented Oct 31, 2019 at 15:02

2 Answers 2

2

you can use sqlite3_exec() api from sqlite3.

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

Sign up to request clarification or add additional context in comments.

Comments

-3
PRAGMA auto_vacuum = FULL; 

Then you won't have to worry about it.

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.