如何在C ++中的单类方法中获取SQLite记录 [英] How To Get SQLite Records in Single Class Method in C++
问题描述
在此示例中...
http ://www.sqlite.org/quickstart.html
...我看到了如何使用 sqlite3_exec()
使用回调类方法或函数,以获取记录集并通过它进行迭代。
...I see how to use sqlite3_exec()
with a callback class method or function in order to get a recordset and iterate through it.
好吧,如果我想创建一个类 DB
,并有一个静态类方法
Well, what if I want to create a class DB
and have a static class method
static void * getRS(void * hDB,std :: string sSQL)
?
我是一个新的C ++,但得到它的死亡非常迅速。在C ++中有一种方法来创建一个Lambda函数,从中获得结果,并将它们传回?或者,有没有另一种技术来返回std :: map,std :: multimap等,我可以遍历每一行?
I'm kind of new with C++ but getting the hang of it extremely rapidly. Is there a way in C++ to create like a Lambda function, get the results from that, and pass them back? Or, is there another technique to return a std::map, std::multimap, etc. that I can iterate through each row?
(注意,我使用XCode并从Objective.C项目中的db.static.mm文件中调用C ++,如果这很重要。)
(Note, I'm using XCode and calling C++ from a db.static.mm file in my Objective C project, if that matters.)
这个问题不是 sqlite3_exec()回调函数澄清。在这个问题中,作者在 sqlite3_exec()
中基本上询问了如何使用回调。在我的问题,我要问如何做一个自顶向下的方法SQLite3代码,而不是使用回调。
This question is not a duplicate of sqlite3_exec() Callback function Clarification. In that question, the author asks essentially how the callback is used in sqlite3_exec()
. In my question, I'm asking how to do a top-down approach of SQLite3 code instead of using a callback. It just so happens that someone left an answer (not the approved answer, mind you) that solves my problem, not that author's problem.
推荐答案
以自上而下(而不是回调)方式执行此操作的最佳解决方案是不使用 sqlite3_exec()。而应使用 sqlite3_prepare_v2(), sqlite3_step(),然后 sqlite3_finalize()。或者,可以通过各种 sqlite3_bind _ *()调用注入/绑定查询参数。
The best solution to do this in a top-down (rather than callback) manner is to not use sqlite3_exec(). Instead, do it with sqlite3_prepare_v2(), sqlite3_step(), and then sqlite3_finalize(). Optionally, one can inject/bind query parameters with various sqlite3_bind_*() calls.
这篇关于如何在C ++中的单类方法中获取SQLite记录的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!