How to use sqlite3_update_hook
I have to use sqlite3 update hook in my application. Can you please suggest or help me how to implement or give me an example
1 Answer
Know the answer? Post it. Somebody with the same question will find it here.
Know the answer? Post it. Somebody with the same question will find it here.
Answer a question in the forums and earn +20 SHARP for every answer.
See unanswered questions
DevSocialUpcoming · 3rd editionAI Agents ConferenceDec 1–3, 2026Live onlineFree to attend5days leftRegister
Amira BedhiafiRank #601 · 4K reputationPosted Mar 22, 2025, 6:04 PM EDT
Here’s a basic example in C to use sqlite3_update_hook:
#include #include // Callback function to be called on update/insert/delete void update_hook(void *arg, int operation, const char *dbname, const char *tablename, sqlite3_int64 rowid) { const char *op_str = "UNKNOWN"; switch (operation) { case SQLITE_INSERT: op_str = "INSERT"; break; case SQLITE_UPDATE: op_str = "UPDATE"; break; case SQLITE_DELETE: op_str = "DELETE"; break; } printf("Operation: %s\n", op_str); printf("Database: %s\n", dbname); printf("Table: %s\n", tablename); printf("Row ID: %lld\n", rowid); } int main() { sqlite3 *db; char *err_msg = 0; // Open SQLite database if (sqlite3_open("test.db", &db) != SQLITE_OK) { fprintf(stderr, "Cannot open database: %s\n", sqlite3_errmsg(db)); return 1; } // Register the update hook sqlite3_update_hook(db, update_hook, NULL); // Create a table and insert data (for demo purposes) const char *sql = "CREATE TABLE IF NOT EXISTS users(id INTEGER PRIMARY KEY, name TEXT);" "INSERT INTO users(name) VALUES('Alice');" "UPDATE users SET name = 'Bob' WHERE id = 1;" "DELETE FROM users WHERE id = 1;"; if (sqlite3_exec(db, sql, 0, 0, &err_msg) != SQLITE_OK) { fprintf(stderr, "SQL error: %s\n", err_msg); sqlite3_free(err_msg); } sqlite3_close(db); return 0; }