mercredi 30 septembre 2015

How to get exact SQL statement sent to database

I want to print out the exact SQL statement sent to the database for logging purposes. I have looked at sqlite3_sql but it doesn't actually print out the SQL statement, just the original SQL I use as input to sqlite3_prepare_v2.

Here is my function to add a value to a row which I want to also print the sql statement.

int add_value(sqlite3* db, sqlite3_stmt * stmt, const char* oid, const char* value)
{
    char sql[256] = {0};
    sprintf(sql, "UPDATE table1 SET Value=? WHERE Field1=\"%s\"", oid);
    printf("%s\n", sql);

    int ret = ::sqlite3_prepare_v2(
        db,
        sql,
        strlen(sql),
        &stmt,
        0 );

    /* How do I print out the sql statement to be sent to the database? */

    ret = ::sqlite3_bind_text( stmt, 1, value, strlen(value), 0 );
    ret = sqlite3_step(stmt);

    return ret;
}

Aucun commentaire:

Enregistrer un commentaire