QSqlRecord:在QT中访问SQLite表中的数值,结果为空

时间:2022-10-13 09:57:37

The question I have is similar to a few I've seen, but the solutions don't seem to work for me, so I'll add a new one:

我的问题类似于我见过的一些问题,但解决方案对我来说似乎不起作用,所以我将添加一个新的:

I am trying to access a numeric value (the highest user ID) from an SQLite table into my QT Program, with the following query:

我试图通过以下查询从SQLite表访问我的QT程序中的数值(最高用户ID):

SELECT Name, MAX(ID) FROM User_lib;

This works from the command shell, with the expected result.

这可以从命令shell开始,具有预期的结果。

I then attempt to access the maximal ID value with:

然后,我尝试使用以下命令访问最大ID值:

QSqlQuery qry_ID;    
qry_ID.prepare("SELECT Name, MAX(User_lib.ID) FROM User_lib");   
qry_ID.exec()
qDebug() << qry_ID.record().value("ID").toInt();

The result is zero (and empty if I access "Name" with toString())

结果为零(如果我使用toString()访问“Name”,则为空)

Also, as a test

另外,作为测试

qDebug() << "Number of Rows: " << qry_ID.size();

which I've seen in several other answers gives me -1. On the other hand,

我在其他几个答案中看到的给了我-1。另一方面,

qDebug() << "Number of columns: " << qry_ID.record().count();

gives me "Number of columns 2" as expected. It appears that the query gives no results.

按预期给出“列数2”。看来查询没有结果。

At first I thought that I had a problem with my query, but the same thing is happening when I attempt to count the rows in queries that clearly work correctly (table is displayed, I can add elements correctly, etc), so I think I must be doing something wrong in QT.

起初我以为我的查​​询有问题,但是当我尝试计算明显正常工作的查询中的行(表格显示,我可以正确添加元素等)时,同样的事情正在发生,所以我想我必须在QT做错事。

2 个解决方案

#1


1  

Let's see what you actually get in the command-line shell:

让我们看一下你在命令行shell中实际获得的内容:

sqlite> .mode columns
sqlite> .header on
sqlite> SELECT Name, MAX(ID) FROM User_lib;
Name        MAX(ID)
----------  ----------
user30248   8562493

So in the output that you get from the query, the second column is not named ID but MAX(ID).

因此,在从查询中获得的输出中,第二列不是名称ID而是MAX(ID)。

The documentation actually says:

文档实际上说:

The name of a result column is the value of the "AS" clause for that column, if there is an AS clause. If there is no AS clause then the name of the column is unspecified and may change from one release of SQLite to the next.

如果存在AS子句,则结果列的名称是该列的“AS”子句的值。如果没有AS子句,则列的名称未指定,可能会从SQLite的一个版本更改为下一个版本。

So use AS, or access the column by its index.

所以使用AS,或通过索引访问列。


SQLite computes output rows on demand, so it is not possible to give a count of rows before they have all been read. Therefore, the QuerySize feature is not supported by the SQLite driver.

SQLite根据需要计算输出行,因此在读取所有行之前不可能给出行数。因此,SQLite驱动程序不支持QuerySize功能。

#2


0  

I haven't checked CL's answer above, which I am sure will work as well, but I have managed to find an alternative solution that worked for me and may be interesting. Since QsqlQuery's internal pointer is set to one field ahead of the first record, I have to advance with next. This is the code that worked:

我没有检查上面的CL的答案,我相信它也会起作用,但我设法找到了一个对我有用的替代解决方案,可能很有趣。由于QsqlQuery的内部指针设置为第一个记录之前的一个字段,因此我必须使用next。这是有效的代码:

QSqlQuery qry_ID;
qry_ID.prepare("SELECT Name, MAX(ID) FROM User_lib");
qry_ID.exec();   
qry_ID.next();

QString name = qry_ID.value(0).toString();
int IDmax = qry_ID.value(1).toInt();                

If you need to access various rows, a while loop is required, but that is a different question.

如果您需要访问各种行,则需要while循环,但这是一个不同的问题。

#1


1  

Let's see what you actually get in the command-line shell:

让我们看一下你在命令行shell中实际获得的内容:

sqlite> .mode columns
sqlite> .header on
sqlite> SELECT Name, MAX(ID) FROM User_lib;
Name        MAX(ID)
----------  ----------
user30248   8562493

So in the output that you get from the query, the second column is not named ID but MAX(ID).

因此,在从查询中获得的输出中,第二列不是名称ID而是MAX(ID)。

The documentation actually says:

文档实际上说:

The name of a result column is the value of the "AS" clause for that column, if there is an AS clause. If there is no AS clause then the name of the column is unspecified and may change from one release of SQLite to the next.

如果存在AS子句,则结果列的名称是该列的“AS”子句的值。如果没有AS子句,则列的名称未指定,可能会从SQLite的一个版本更改为下一个版本。

So use AS, or access the column by its index.

所以使用AS,或通过索引访问列。


SQLite computes output rows on demand, so it is not possible to give a count of rows before they have all been read. Therefore, the QuerySize feature is not supported by the SQLite driver.

SQLite根据需要计算输出行,因此在读取所有行之前不可能给出行数。因此,SQLite驱动程序不支持QuerySize功能。

#2


0  

I haven't checked CL's answer above, which I am sure will work as well, but I have managed to find an alternative solution that worked for me and may be interesting. Since QsqlQuery's internal pointer is set to one field ahead of the first record, I have to advance with next. This is the code that worked:

我没有检查上面的CL的答案,我相信它也会起作用,但我设法找到了一个对我有用的替代解决方案,可能很有趣。由于QsqlQuery的内部指针设置为第一个记录之前的一个字段,因此我必须使用next。这是有效的代码:

QSqlQuery qry_ID;
qry_ID.prepare("SELECT Name, MAX(ID) FROM User_lib");
qry_ID.exec();   
qry_ID.next();

QString name = qry_ID.value(0).toString();
int IDmax = qry_ID.value(1).toInt();                

If you need to access various rows, a while loop is required, but that is a different question.

如果您需要访问各种行,则需要while循环,但这是一个不同的问题。