Get original SQL query from prepared statement in SQLite

You probably want to use sqlite3_trace

This will call a callback function (that you define) and on of the parameters is a char * of the SQL of the prepared statements (including bound parameters).


As per the comments in sqlite3.c (amalgamation), sqlite3_sql(myQuery) will return the original SQL text.

I don't see any function for finding the value bound at a particular index, but we can easily add one to the standard set of SQLite functions. It may look something like this:

const char* sqlite3_bound_value(sqlite3_stmt* pStmt, int index)
{
  Vdbe *p = (Vdbe *)pStmt;

  // check if &p->aVar[index - 1] points to a valid location.
  return (char*)sqlite3ValueText(&p->aVar[index - 1], SQLITE_UTF8);
}

Well, the above code shows only a possible way sqlite3_bound_value() could be implemented. I haven't tested it, it might be wrong, but it gives certain hints on how/where to start.

Tags:

C++

Sqlite