aj3423
aj3423

Reputation: 2561

get the value from the sqlite3_stmt when calling the sqlite3_step()

I need to hook a program's logging function and get it's logging params, it uses sqlite3_prepare_v2 -> sqlite3_bind_xxx -> sqlite3_step functions. I'd like to know if it's possible to get the params and the sql string from a sqlite3_stmt

here's my test code:

#include <stdio.h>

#include "sqlite3.h"

int main() {
    sqlite3* db;
    sqlite3_open("test.db", &db);
    sqlite3_exec(db, "CREATE TABLE test_table(name text, age integer, info text)", 0, 0, 0);

    sqlite3_stmt* stmt;
    sqlite3_prepare_v2(db, "INSERT INTO 'test_table' (name, age, info) VALUES (?, ?, ?);", -1, &stmt, 0);

    sqlite3_bind_text(stmt, 1, "aabb", 4, 0); // set name
    // sqlite3_bind_int(stmt, 2, 3423);
    // sqlite3_bind_text(stmt, 3, "ccdd", 4, 0);

    sqlite3_step(stmt);

    char* txt = (char*)sqlite3_column_text(stmt, 0); // try to get the name("aabb") from stmt
    printf("txt: %s\n", txt); // it prints "txt: (null)"

}

I tried the sqlite_column_xxxx functions, it prints null. So I tried to use the internal struct to decode the stmt, actually it's a pointer of Vdbe, but the Vdbe struct is vary from version to version, the old version has a "Parse' pointer inside but the latest version doesn't. So I gave up using the internal struct.

Any other way to get the params and sql string from the stmt?

Upvotes: 1

Views: 3823

Answers (1)

CL.
CL.

Reputation: 180070

You must check the return values of all functions. (If you get an error, use sqlite3_errmsg to get a useful message.)

The fifth parameter of sqlite3_bind_text must not be zero. See the documentation for the allowed values.

The SQL string is available through sqlite3_sql.

The values of the parameters cannot be read.

Upvotes: 2

Related Questions