PDA

View Full Version : qt and postgresql



kamienna.gora
30th August 2016, 10:55
Hi everybody!
I have been trying to solve strange problem with "SELECT" command from qt application. I use postgresql server (9.3.14 on kubuntu). There is no troubles to use "SELECT" command from psql nor pgAdmin III but my qt application with "exec()" method gives only "false".

The SELECT example from psql:
* * * * * * * * * * * * * * * * * * * * * * * *
test=# select * from "dbo.people";
ID | FirstName | LastName
----+-----------+----------
1 | Will | Hope
2 | Phill | Smith
(2 rows)
* * * * * * * * * * * * * * * * * * * * * * * *

My application:
* * * * * * * * * * * * * * * * * * * * * * * *
#include <QCoreApplication>
#include <QtSql>
#include <QtDebug>

int main(int argc, char *argv[])
{
QCoreApplication a(argc, argv);

QSqlDatabase db = QSqlDatabase::addDatabase("QPSQL");
db.setDatabaseName("test");
db.setPort(5432);

bool ok = db.open();

if(ok != true)
{
qDebug() << "Database is not opened";
}
else
{
qDebug() << "Database is opened";

QSqlQuery qry;

if(qry.exec("select * from dbo.people;"))
{
while(qry.next())
{
qDebug() << qry.value(0).toString();
}
}
else
{
qDebug() << "Connection Failed!" << db.lastError().text();
}
qDebug() << "Closing...";
db.close();
}
return a.exec();
}
* * * * * * * * * * * * * * * * * * * * * * * *

Finally I got the answer in XTerm:
* * * * * * * * * * * * * * * * * * * * * * * *
Database is opened
Connection Failed
Closing...
* * * * * * * * * * * * * * * * * * * * * * * *

After a few days I have no idea how to solve it...

Lesiok
30th August 2016, 13:49
What says qry.lastError() ?

kamienna.gora
31st August 2016, 08:28
... lastError() says:

* * * * * * * * * * * * * * * * * * * * * * * *
QSqlError("42P01", "QPSQL: Unable to create query", "ERROR: relation \"dbo.people\" does not exist\nLINE 1: select * from dbo.people;\n^\n(42P01)")
* * * * * * * * * * * * * * * * * * * * * * * *

when, I have put to the name of table quotation marks \"dbo.people\" -> whole line of the application should be: if(qry.exec("select * from \"dbo.people\";")) ->
obtained result of the application:

* * * * * * * * * * * * * * * * * * * * * * * *
Database is opened
"Will"
"Phill"
Closing...
* * * * * * * * * * * * * * * * * * * * * * * *

So, problem was solved. Lesiok - Thank you so much :)

Lesiok
31st August 2016, 09:00
This is what differed queries in the console and in the program.