从SQLite中的准备语句获取原始SQL查询 [英] Get original SQL query from prepared statement in SQLite

查看:257
本文介绍了从SQLite中的准备语句获取原始SQL查询的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用SQLite(3.6.4)从C + +应用程序(使用标准C api)。我的问题是:一旦使用 sqlite3_prepare_v2(),并使用 sqlite3_bind_xyz() - 有没有办法得到一个包含原始SQL查询的字符串?

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 bound with parameters using sqlite3_bind_xyz() - is there any way to get a string containing the original SQL query?

原因是当出现错误时,我打印查询(用于调试 - 这是一个内部开发人员测试应用程序。

The reason is when something goes wrong, I'd like to print the query (for debugging - this is an in-house developer only test app).

示例:

sqlite3_prepare_v2(db, "SELECT * FROM xyz WHERE something = ? AND somethingelse = ?", -1, &myQuery, NULL);
sqlite3_bind_text(myQuery, 1, mySomething);
sqlite3_bind_text(myQuery, 2, mySomethingElse);
// ....

// somewhere else, in another function perhaps
if (sqlite3_step(myQuery) != SQLITE_OK)
{
     // Here i'd like to print the actual query that failed - but I 
     // only have the myQuery variable
     exit(-1);
}

如果它也可以打印出绑定的实际参数, :

Bonus points if it could also print out the actual parameters that was bound. :)

推荐答案

根据sqlite3.c(amalgamation)中的注释, sqlite3_sql(myQuery)将返回原始的SQL文本。

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

看到任何函数找到在特定索引绑定的值,但我们可以轻松地添加一个标准的SQLite函数集。它可能看起来像这样:

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);
}

好吧,上面的代码只显示了一个可能的方法sqlite3_bound_value 。我没有测试它,它可能是错误的,但它给出了一些提示如何/从哪里开始。

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.

这篇关于从SQLite中的准备语句获取原始SQL查询的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆