Get original SQL query from prepared statement in SQLite

前端 未结 3 1277
粉色の甜心
粉色の甜心 2020-12-20 18:12

I\'m using SQLite (3.6.4) from a C++ application (using the standard C api). My question is: once a query has been prepared, using sqlite3_prepare_v2(), and bou

相关标签:
3条回答
  • 2020-12-20 18:18

    Quoting the documentation:

    In the "v2" interfaces, the prepared statement that is returned (the sqlite_stmt object) contains a copy of the original SQL text.

    http://www.sqlite.org/c3ref/prepare.html

    0 讨论(0)
  • 2020-12-20 18:21

    As per the comments in sqlite3.c (amalgamation), sqlite3_sql(myQuery) will return the original SQL text.

    I don't see any function for finding the value bound at a particular index, but we can easily add one to the standard set of SQLite functions. It may look something like this:

    const char* sqlite3_bound_value(sqlite3_stmt* pStmt, int index)
    {
      Vdbe *p = (Vdbe *)pStmt;
    
      // check if &p->aVar[index - 1] points to a valid location.
      return (char*)sqlite3ValueText(&p->aVar[index - 1], SQLITE_UTF8);
    }
    

    Well, the above code shows only a possible way sqlite3_bound_value() could be implemented. I haven't tested it, it might be wrong, but it gives certain hints on how/where to start.

    0 讨论(0)
  • 2020-12-20 18:26

    You probably want to use sqlite3_trace

    This will call a callback function (that you define) and on of the parameters is a char * of the SQL of the prepared statements (including bound parameters).

    0 讨论(0)
提交回复
热议问题