Error: QSqlQuery::value: not positioned on a valid record
-
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.@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
@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)+";");@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 !! -
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.
@Paul-Colby
yes, also just realized that. Doesn't make sense what I did, sorry :D