28

I need to log queries, not only inserts/updates/deletes but also selects and other queries, from a number of applications that use SQLite. Introducing logging to the applications would in this case not be a feasible solution in practice. So how can I enable query logging in SQLite itself?

1
  • 1
    Just to add a bit more info, if you're using SQLite in your application, you should probably use a different logging system for the app as a whole, rather than asking for built-in SQLite functions. Commented Dec 23, 2010 at 21:07

1 Answer 1

13

Take a look at the sqlite Trace API. You have to implement the callback yourself.

void *sqlite3_trace(sqlite3*, void(*xTrace)(void*,const char*), void*);

The callback function registered by sqlite3_trace() is invoked at various times when an SQL statement is being run by sqlite3_step(). The callback returns a UTF-8 rendering of the SQL statement text as the statement first begins executing. Additional callbacks occur as each triggered subprogram is entered.

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

5 Comments

Ok, so you are basically saying that I would need to write the code for logging. I wonder if someone has already done that, for a common use case like this?
You have to write the COMMAND to invoke the inbuilt trace mechanism.
The sqlite3_trace api doesn't show you bindings. If your using a wrapper class, you may want to put in your own logging of the sql statements and the parameters.
Just wanted to drop in a small snippet for basic logging : void trace_callback( void* udp, const char* sql ) { printf("{SQL} [%s]\n", sql); } sqlite3_trace(g_dbConf, trace_callback, NULL);
Quite hard to write - my basic attempt stackoverflow.com/questions/66410173/…

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.