Problem with query
-
wrote on 2 Jan 2023, 18:49 last edited by Kuji 1 Feb 2023, 19:14
This is my table
I connect to database and try to get data by using queryQSqlQuery qry; qry.prepare("SELECT * FROM user"); qInfo()<<qry.lastError(); qry.exec(); qInfo()<<qry.lastError(); if(qry.size()<1) qInfo()<<"no data"; else while (qry.next()) { qInfo()<<qry.value(0).toString(); }
After this i got this in console
QSqlError("", "", "")
QSqlError("", "", "")
"postgres"I tryed take one column with this code
QSqlQuery qry; qry.prepare("SELECT age FROM user"); qInfo()<<qry.lastError(); qry.exec(); qInfo()<<qry.lastError(); if(qry.size()<1) qInfo()<<"no data"; else while (qry.next()) { qInfo()<<qry.value(0).toString(); }
After this i got this in console
QSqlError("42703", "QPSQL: Unable to prepare statement", "ERROR: column "age" doesn't exist\nLINE 1: PREPARE qpsqlpstmt_1 AS SELECT age FROM user\n ^\n(42703)")
QSqlError("42601", "QPSQL: Unable to create query", "ERROR: syntax error at or near\nLINE 1: EXECUTE \n ^\n(42601)")
no data -
wrote on 2 Jan 2023, 22:02 last edited by
Your first code block (that returned something) with some comments inserted:
QSqlQuery qry; qry.prepare("SELECT * FROM user"); qInfo()<<qry.lastError();
Postgresql has a reserved word
user
. Best not to use that name for a table of your own. It can be done but is more trouble than it is worth.qInfo()<<qry.lastError();
OK, it executed something.
if(qry.size()<1) qInfo()<<"no data";
See QSqlQuery::size() for reasons this might be unreliable. I do not know if it is, or is not, under Postgresql.
else while (qry.next()) { qInfo()<<qry.value(0).toString(); }
This prints "postgres". That value does not appear anywhere in the table you showed us a picture of ( @SGaist's point). Either using the reserved word
user
has resulted in this, possibly user maps to the pg_user system catalog, or your default QSqlDatabase connection is not connected to the database you think it is. -
The error message does not match the query you show us above
PREPARE qpsqlpstmt_1 AS SELECT * FROM user WHERE password = ...
' PREPARE qpsqlpstmt_1 AS' is added for a prepared (postgresql) statement by the driver but the rest comes from your code. so post the correct code which triggers the error and/or fix it - as you can see your table has no column
password
. -
wrote on 2 Jan 2023, 19:16 last edited by Kuji 1 Feb 2023, 19:16
@Christian-Ehrlicher Changed console errors
-
Hi,
From the looks of it, you are not accessing the table you think you are accessing.
From the "successful" query, you are not reading the table you are showing in your picture.
-
Hi,
From the looks of it, you are not accessing the table you think you are accessing.
From the "successful" query, you are not reading the table you are showing in your picture.
-
wrote on 2 Jan 2023, 22:02 last edited by
Your first code block (that returned something) with some comments inserted:
QSqlQuery qry; qry.prepare("SELECT * FROM user"); qInfo()<<qry.lastError();
Postgresql has a reserved word
user
. Best not to use that name for a table of your own. It can be done but is more trouble than it is worth.qInfo()<<qry.lastError();
OK, it executed something.
if(qry.size()<1) qInfo()<<"no data";
See QSqlQuery::size() for reasons this might be unreliable. I do not know if it is, or is not, under Postgresql.
else while (qry.next()) { qInfo()<<qry.value(0).toString(); }
This prints "postgres". That value does not appear anywhere in the table you showed us a picture of ( @SGaist's point). Either using the reserved word
user
has resulted in this, possibly user maps to the pg_user system catalog, or your default QSqlDatabase connection is not connected to the database you think it is. -
Your first code block (that returned something) with some comments inserted:
QSqlQuery qry; qry.prepare("SELECT * FROM user"); qInfo()<<qry.lastError();
Postgresql has a reserved word
user
. Best not to use that name for a table of your own. It can be done but is more trouble than it is worth.qInfo()<<qry.lastError();
OK, it executed something.
if(qry.size()<1) qInfo()<<"no data";
See QSqlQuery::size() for reasons this might be unreliable. I do not know if it is, or is not, under Postgresql.
else while (qry.next()) { qInfo()<<qry.value(0).toString(); }
This prints "postgres". That value does not appear anywhere in the table you showed us a picture of ( @SGaist's point). Either using the reserved word
user
has resulted in this, possibly user maps to the pg_user system catalog, or your default QSqlDatabase connection is not connected to the database you think it is.
1/7