Error: QSqlQuery::value: not positioned on a valid record
-
wrote on 6 May 2018, 09:34 last edited by
Hi! This error is not unknown, I know, but I can't find a solution working for me.
This is the code:query.exec("INSERT INTO Output (ID INTEGER, Number INTEGER)" "VALUES ("+ID+", "+Number+");"); query.exec("SELECT * FROM Output WHERE ID == "+ID+" AND Number == "+Number+";"); query.first(); query.next(); int ID = query.value("ID").toInt(); // here it prints: QSqlQuery::value: not positioned on a valid record
Any idea, what I missed?
Thanks! -
Hi
Maybe the query return no rows ? as in there is data that matches. -
@Niagarer
ok so u have error checking code even if not shown ?You could use
http://sqlitebrowser.org/
to see what is going on.also the first() + next () seems a bit odd and might
move it over the last recordnormally one do
while (query.next() )
...but its not really possible to guess at :)
you should just check data base and see if it even inserted. -
@Niagarer
ok so u have error checking code even if not shown ?You could use
http://sqlitebrowser.org/
to see what is going on.also the first() + next () seems a bit odd and might
move it over the last recordnormally one do
while (query.next() )
...but its not really possible to guess at :)
you should just check data base and see if it even inserted.wrote on 6 May 2018, 10:13 last edited by Niagarer 5 Jun 2018, 10:13@mrjj
Well recently, the insert also failed, but the query told me that. Now it does not print anymore errors there.
Ok, well the insert works kind of (at the first position in my script it works, at the second, 10 lines below, it doesn't, although it is actually the same...).
But at least, I can see one filled table in the browser. Anyway, the following query.value() still fails.
When I execute the "SELECT * FROM..."-command in the browser on the table, it works, I always get a row.
Removing the query.first() unfortunately doesn't help. -
Ok
if select * works then maybe its the where part not working
What database is it ?
I assume sqllite ? -
Ok
if select * works then maybe its the where part not working
What database is it ?
I assume sqllite ? -
@mrjj Yep. Well the complete command works in the browser, so the where does not seem to be the problem itself
Lifetime Qt Championwrote on 6 May 2018, 10:20 last edited by mrjj 5 Jun 2018, 10:23@Niagarer
ok seems fine then.
The where syntax is not the usual one. but might also work. not sure.
Could you try with bindValue ? ( shows insert here but select is the same)QSqlQuery query; int ok = query.prepare(("INSERT INTO person (id, firstname, lastname) VALUES (:name, :first, :last)")); query.bindValue(":id", 4); query.bindValue(":first", "Lars junior"); query.bindValue(":last", "Gordon"); query.exec();
also
query.exec("SELECT * FROM Output WHERE ID == "+ID+" AND Number == "+Number+";");is Number an int ?
if yes, it should be
AND Number == "+QString::number(Number)+";"); -
@Niagarer
ok seems fine then.
The where syntax is not the usual one. but might also work. not sure.
Could you try with bindValue ? ( shows insert here but select is the same)QSqlQuery query; int ok = query.prepare(("INSERT INTO person (id, firstname, lastname) VALUES (:name, :first, :last)")); query.bindValue(":id", 4); query.bindValue(":first", "Lars junior"); query.bindValue(":last", "Gordon"); query.exec();
also
query.exec("SELECT * FROM Output WHERE ID == "+ID+" AND Number == "+Number+";");is Number an int ?
if yes, it should be
AND Number == "+QString::number(Number)+";");wrote on 6 May 2018, 10:42 last edited by Niagarer 5 Jun 2018, 10:52@mrjj
The number is already converted to a string.
Ok, I've done itquery.prepare(("INSERT INTO Output (ID, Number) VALUES (:id, :number)")); query.bindValue(":id", ID); query.bindValue(":number", i); query.exec(); query.exec("SELECT * FROM Output WHERE ID == "+ID+" AND Number == "+Number+";"); query.next(); int ID = query.value("ID").toInt();
And I got the solution :)
There are two main problems with my version above.- Don't use query.first() and then query.next() like I did (damn, yes this was actually obvious sry...) The query.first() takes the first item and the query.next() is going into nowhere, because I already took the last item with query.first() -.- Use query.first() or query.next() in this case.
- The actually interesting problem: Use query.prepare() and bindValue() instead of doing it directly by calling query.exec("INSERT INTO ..."). I have no idea why... but obviously the query has a problem with that...
by doing both of these things I got it working.
Thank you @mrjj @Paul-Colby !! -
wrote on 6 May 2018, 10:42 last edited by Paul Colby 5 Jun 2018, 10:43
Hmm... I'm probably missing something (seems a little too obvious, but just in case)... isn't the problem:
query.first(); query.next();
The first line fetches the first (and presumably only) records. The second line moves past that, where there are no more records?
Cheers.
Edit: Ah, seems you beat me to it :) Cheers.
-
Hmm... I'm probably missing something (seems a little too obvious, but just in case)... isn't the problem:
query.first(); query.next();
The first line fetches the first (and presumably only) records. The second line moves past that, where there are no more records?
Cheers.
Edit: Ah, seems you beat me to it :) Cheers.
wrote on 6 May 2018, 10:44 last edited by Niagarer 5 Jun 2018, 10:47@Paul-Colby
yes, also just realized that. Doesn't make sense what I did, sorry :D
1/11