Small. Fast. Reliable.
Choose any three.
Search for:

SQLite C Interface

The pre-update hook.

#if defined(SQLITE_ENABLE_PREUPDATE_HOOK)
void *sqlite3_preupdate_hook(
  sqlite3 *db,
  void(*xPreUpdate)(
    void *pCtx,                   /* Copy of third arg to preupdate_hook() */
    sqlite3 *db,                  /* Database handle */
    int op,                       /* SQLITE_UPDATE, DELETE or INSERT */
    char const *zDb,              /* Database name */
    char const *zName,            /* Table name */
    sqlite3_int64 iKey1,          /* Rowid of row about to be deleted/updated */
    sqlite3_int64 iKey2           /* New rowid value (for a rowid UPDATE) */
  ),
  void*
);
int sqlite3_preupdate_old(sqlite3 *, int, sqlite3_value **);
int sqlite3_preupdate_count(sqlite3 *);
int sqlite3_preupdate_depth(sqlite3 *);
int sqlite3_preupdate_new(sqlite3 *, int, sqlite3_value **);
#endif

R-17579-15688:[These interfaces are only available if SQLite is compiled using the SQLITE_ENABLE_PREUPDATE_HOOK compile-time option. ]

R-49171-60075:[The sqlite3_preupdate_hook() interface registers a callback function that is invoked prior to each INSERT, UPDATE, and DELETE operation on a rowid table. ] R-47446-42933:[At most one preupdate hook may be registered at a time on a single database connection; each call to sqlite3_preupdate_hook() overrides the previous setting. ] R-32246-51249:[The preupdate hook is disabled by invoking sqlite3_preupdate_hook() with a NULL pointer as the second parameter. ] R-42144-12179:[The third parameter to sqlite3_preupdate_hook() is passed through as the first parameter to callbacks. ]

R-43999-41837:[The preupdate hook only fires for changes to rowid tables; the preupdate hook is not invoked for changes to virtual tables or WITHOUT ROWID tables. ]

R-36147-61857:[The second parameter to the preupdate callback is a pointer to the database connection that registered the preupdate hook. ] R-44820-17227:[The third parameter to the preupdate callback is one of the constants SQLITE_INSERT, SQLITE_DELETE, or SQLITE_UPDATE to identify the kind of update operation that is about to occur. ] R-14786-64066:[The fourth parameter to the preupdate callback is the name of the database within the database connection that is being modified. This will be "main" for the main database or "temp" for TEMP tables or the name given after the AS keyword in the ATTACH statement for attached databases. ] R-48560-31808:[The fifth parameter to the preupdate callback is the name of the table that is being modified. ] R-39474-38888:[The sixth parameter to the preupdate callback is the initial rowid of the row being changes for SQLITE_UPDATE and SQLITE_DELETE changes and is undefined for SQLITE_INSERT changes. ] R-32370-18782:[The seventh parameter to the preupdate callback is the final rowid of the row being changed for SQLITE_UPDATE and SQLITE_INSERT changes and is undefined for SQLITE_DELETE changes. ]

The sqlite3_preupdate_old(), sqlite3_preupdate_new(), sqlite3_preupdate_count(), and sqlite3_preupdate_depth() interfaces provide additional information about a preupdate event. These routines may only be called from within a preupdate callback. Invoking any of these routines from outside of a preupdate callback or with a database connection pointer that is different from the one supplied to the preupdate callback results in undefined and probably undesirable behavior.

R-11643-51568:[The sqlite3_preupdate_count(D) interface returns the number of columns in the row that is being inserted, updated, or deleted. ]

R-19120-50787:[The sqlite3_preupdate_old(D,N,P) interface writes into P a pointer to a protected sqlite3_value that contains the value of the Nth column of the table row before it is updated. ] The N parameter must be between 0 and one less than the number of columns or the behavior will be undefined. This must only be used within SQLITE_UPDATE and SQLITE_DELETE preupdate callbacks; if it is used by an SQLITE_INSERT callback then the behavior is undefined. The sqlite3_value that P points to will be destroyed when the preupdate callback returns.

R-19884-42355:[The sqlite3_preupdate_new(D,N,P) interface writes into P a pointer to a protected sqlite3_value that contains the value of the Nth column of the table row after it is updated. ] The N parameter must be between 0 and one less than the number of columns or the behavior will be undefined. This must only be used within SQLITE_INSERT and SQLITE_UPDATE preupdate callbacks; if it is used by an SQLITE_DELETE callback then the behavior is undefined. The sqlite3_value that P points to will be destroyed when the preupdate callback returns.

R-50949-63870:[The sqlite3_preupdate_depth(D) interface returns 0 if the preupdate callback was invoked as a result of a direct insert, update, or delete operation; or 1 for inserts, updates, or deletes invoked by top-level triggers; or 2 for changes resulting from triggers called by top-level triggers; and so forth. ]

See also: sqlite3_update_hook()

See also lists of Objects, Constants, and Functions.