C++ 使用 Qt 从 SQLite 中选择

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/2311218/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-27 22:59:12  来源:igfitidea点击:

Select from SQLite with Qt

c++sqlqtsqlite

提问by vas3k

I try to deal with SQLite database on Qt 4.5.3 on Linux. I've already created the databsae.

我尝试在 Linux 上的 Qt 4.5.3 上处理 SQLite 数据库。我已经创建了数据库。

Then, I try to perform select on Qt:

然后,我尝试在 Qt 上执行选择:

db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(filename); // Here is FULL path to the database. I've checked it twice :)
bool ok = db.open();
qDebug() << db.tables();

QSqlQuery query;
query.exec("select * from lessons");
qDebug() << query.size();
qDebug() << query.isSelect();
qDebug() << query.isValid();

But debug console says:

但是调试控制台说:

("lessons", "weeklessons", "weeks") 
-1 
true 
false 

Why it's select nothing? What I have doing wrong?

为什么什么都不选?我做错了什么?

回答by Leiaz

The isValid() method returns true if the query is positionned on a valid record, but after calling exec(), it isn't : you have to move to a valid record first, for example with query.first() or query.next(). See Qt docs : http://doc.qt.io/archives/4.6/qsqlquery.html

如果查询定位在有效记录上,则 isValid() 方法返回 true,但在调用 exec() 后,它不是:您必须首先移动到有效记录,例如使用 query.first() 或查询。下一个()。请参阅 Qt 文档:http: //doc.qt.io/archives/4.6/qsqlquery.html

The size() returning -1 doesn't mean there is no result : SQLite is one of the databases for which the size of the query is not directly available (look in the documentation for QSqlDriver::hasFeature()). You can check that rows are returned and find the size with a loop and query.next().

size() 返回 -1 并不意味着没有结果:SQLite 是无法直接获得查询大小的数据库之一(查看 QSqlDriver::hasFeature() 的文档)。您可以检查行是否返回并使用循环和 query.next() 查找大小。

Depending on what you want to do with the result of your select, you could also use QSqlQueryModel instead of QSqlQuery.

根据您想对选择的结果做什么,您还可以使用 QSqlQueryModel 而不是 QSqlQuery。

回答by matt_man22

In your "QSqlQuery query;" declaration, you have to specify the database connection, e.g. "QSqlQuery query(db)"

在您的“QSqlQuery 查询”中;声明,您必须指定数据库连接,例如“QSqlQuery query(db)”

回答by Ton van den Heuvel

Given that your program reports that the query is invalid, have a look at the error message as follows:

鉴于您的程序报告查询无效,请查看错误消息,如下所示:

QDebug() << query.lastError().text();

This should help you in debugging the problem.

这应该可以帮助您调试问题。

回答by poke

Without knowing much about Qt, I was asking myself one question: How does the QSqlQueryknow which connection to use. Looking that up in the manual revealed that there are basically two possibilities to execute a query:

在不太了解 Qt 的情况下,我问自己一个问题:如何QSqlQuery知道使用哪个连接。在手册中查找发现执行查询基本上有两种可能性:

  1. query = db.exec("select * from lessons");
    Using the database connection, and executingfrom there.
  2. QSqlQuery query( db );
    Constructing the query using the existing databaseand then executing the query:
    query.exec("select * from lessons");
  1. query = db.exec("select * from lessons");
    使用数据库连接,并从那里执行
  2. QSqlQuery query( db );
    使用现有数据库构建查询,然后执行查询:
    query.exec("select * from lessons");

edit:After reading a bit more, it seems that default connections are supported, so your example should work…

编辑:阅读更多内容后,似乎支持默认连接,因此您的示例应该可以工作...

Could you try passing the query as the constructor's value instead? Maybe it works then.

您可以尝试将查询作为构造函数的值传递吗?也许它会起作用。

QSqlQuery query("select * from lessons");