Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Get original SQL query from prepared statement in SQLite

Tags:

c++

sqlite

I'm using SQLite (3.6.4) from a C++ application (using the standard C api). My question is: once a query has been prepared, using sqlite3_prepare_v2(), and bound with parameters using sqlite3_bind_xyz() - is there any way to get a string containing the original SQL query?

The reason is when something goes wrong, I'd like to print the query (for debugging - this is an in-house developer only test app).

Example:

sqlite3_prepare_v2(db, "SELECT * FROM xyz WHERE something = ? AND somethingelse = ?", -1, &myQuery, NULL);
sqlite3_bind_text(myQuery, 1, mySomething);
sqlite3_bind_text(myQuery, 2, mySomethingElse);
// ....

// somewhere else, in another function perhaps
if (sqlite3_step(myQuery) != SQLITE_OK)
{
     // Here i'd like to print the actual query that failed - but I 
     // only have the myQuery variable
     exit(-1);
}

Bonus points if it could also print out the actual parameters that was bound. :)

like image 413
Isak Savo Avatar asked Sep 09 '09 06:09

Isak Savo


1 Answers

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).

like image 86
Murph Avatar answered Sep 29 '22 11:09

Murph