将 SQLite 与 Qt 结合使用
我正在考虑使用 SQLite 作为我正在编写的 C++ 应用程序的后端数据库。我已经阅读了 trolltech 网站和 sqlite 上的相关文档,但这些信息似乎有点脱节,没有简单的片段显示完整的 CRUD 示例。
我想编写一组辅助函数,以便我可以从我的应用程序轻松地在 SQLite 中执行 CRUD 操作。
以下 smpet 是我设想编写的辅助函数的伪代码。我将不胜感激有关如何“填充”存根函数的建议。特别令人沮丧的一件事是,在任何文档中都没有明确提及查询与运行查询的数据库之间的关系 - 因此建议使用某种默认连接/表。
在我的应用程序中,我需要能够显式指定运行查询的数据库,因此如果任何答案说明如何显式指定查询中涉及的数据库/表(或与此相关的其他数据库操作),这将很有用)。
我的伪代码如下:
#include <boost/shared_ptr.hh>
typedef boost::shared_ptr<QSqlDatabase> dbPtr;
dbPtr createConnection(const QString& conn_type = "QSQLITE", const QString& dbname = ":memory:")
{
dbPtr db (new QSQlDatabase::QSqlDatabase());
if (db.get())
{
db->addDatabase(conn_type);
db->setDatabaseName(dbname);
if (!db.get()->open)
db.reset();
}
return db;
}
bool runQuery(const Qstring& sql)
{
//How does SQLite know which database to run this SQL statement against ?
//How to iterate over the results of the run query?
}
bool runPreparedStmtQuery(const QString query_name, const QString& params)
{
//How does SQLite know which database to run this SQL statement against ?
//How do I pass parameters (say a comma delimited list to a prepared statement ?
//How to iterate over the results of the run query?
}
bool doBulkInsertWithTran(const Qstring& tablename, const MyDataRows& rows)
{
//How does SQLite know which database to run this SQL statement against ?
//How to start/commit|rollback
}
如果我问的不清楚,我问实现上述每个功能的正确方法是什么(可能第一个功能除外 - 除非它当然可以改进)。
[编辑]
通过删除显式指定表的要求澄清了问题(这已经在 SQL 查询中完成 - 我忘了。感谢 Tom 指出)
I am thinking of using SQLite as a backend DB for a C++ applicatiojn I am writing. I have read the relevant docs on both teh trolltech site and sqlite, but the information seems a little disjointed, there is no simple snippet that shows a complete CRUD example.
I want to write a set of helper functions to allow me to execute CRUD actions in SQLite easily, from my app.
The following smippet is pseudocode for the helper functions I envisage writing. I would be grateful for suggestions on how to "fill up" the stub functions. One thing that is particularly frustrating is that there is no clear mention in any of the docs, on the relationship between a query and the database on which the query is being run - thus suggesting some kind of default connection/table.
In my application, I need to be able to explicitly specify the database on which queries are run, so it would be useful if any answers spell out how to explicitly specify the database/table involved in a query (or other database action for that matter).
My pseudocode follows below:
#include <boost/shared_ptr.hh>
typedef boost::shared_ptr<QSqlDatabase> dbPtr;
dbPtr createConnection(const QString& conn_type = "QSQLITE", const QString& dbname = ":memory:")
{
dbPtr db (new QSQlDatabase::QSqlDatabase());
if (db.get())
{
db->addDatabase(conn_type);
db->setDatabaseName(dbname);
if (!db.get()->open)
db.reset();
}
return db;
}
bool runQuery(const Qstring& sql)
{
//How does SQLite know which database to run this SQL statement against ?
//How to iterate over the results of the run query?
}
bool runPreparedStmtQuery(const QString query_name, const QString& params)
{
//How does SQLite know which database to run this SQL statement against ?
//How do I pass parameters (say a comma delimited list to a prepared statement ?
//How to iterate over the results of the run query?
}
bool doBulkInsertWithTran(const Qstring& tablename, const MyDataRows& rows)
{
//How does SQLite know which database to run this SQL statement against ?
//How to start/commit|rollback
}
In case what I'm asking is not clear, I am asking what would be the correct wat to implement each of the above functions (possibly with the exception of the first - unless it can be bettered of course).
[Edit]
Clarified question by removing requirement to explicitly specify a table (this is already done in the SQL query - I forgot. Thanks for pointing that out Tom
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
默认情况下,Qt 使用应用程序的默认数据库来运行查询。这是使用默认连接名称添加的数据库。有关详细信息,请参阅 Qt 文档。我不确定默认数据库表是什么意思,因为要操作的表通常是在查询本身中指定的?
为了回答您的问题,这里是您的方法之一的实现。请注意,我不会返回
bool
,而是返回一个QSqlQuery
实例,以便能够迭代查询结果。您可以按如下方式使用它:
请注意,还可以通过显式指定相关的 QSqlDatabase 实例作为 QSqlQuery 的第二个参数来显式指定应针对哪个数据库运行查询。代码>构造函数:
By default, Qt uses the application's default database to run queries against. That is the database that was added using the default connection name. See the Qt documentation for more information. I am not sure what you mean by the default database table, since the table to operate on is normally specified in the query itself?
To answer your question, here is an implementation for one of your methods. Note that instead of returning a
bool
I would return aQSqlQuery
instance instead to be able to iterate over the results of a query.You would use this as follows:
Note that it is also possible to explicitly specify for which database a query should be run by explicitly specifying the relevant
QSqlDatabase
instance as the second parameter for theQSqlQuery
constructor: