PDA

View Full Version : QPSQL: Unable to find table set in a QSqlTableModel



andreabiscuola
16th January 2011, 20:59
Hi all!

I'm trying to fetch a record set from a postgreSQL database. Connection to the database was established successfully but when i try to fetch a recordset using a QSqlTableModel i obtain this error: "Unable to find table prod_lotto".
The table "prod_lotto" exist in the selected database and is correctly initialized.
Here is the code relative to the QSqlTableModel:

ptrProductionModel = new QSqlTableModel(this, currentConnection);
ptrProductionMapper = new QDataWidgetMapper(this);
ptrProductionModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
ptrProductionMapper->setModel(ptrProductionModel);

// Setup the production table
ptrProductionModel->setTable("prod_lotto");
ptrProductionModel->setSort(lot_number, Qt::AscendingOrder);
ptrProductionModel->setHeaderData(lot_id, Qt::Horizontal, tr("Lot id"));
ptrProductionModel->setHeaderData(lot, Qt::Horizontal, tr("Lot"));
ptrProductionModel->setHeaderData(lot_model, Qt::Horizontal, tr("Lot model"));
ptrProductionModel->setHeaderData(lot_number, Qt::Horizontal, tr("Lot number"));
ptrProductionModel->setHeaderData(customer, Qt::Horizontal, tr("Customer"));
ptrProductionModel->setHeaderData(order_date, Qt::Horizontal, "Order date");
ptrProductionModel->setHeaderData(site, Qt::Horizontal, tr("Site"));
ptrProductionModel->setHeaderData(guarantee_start, Qt::Horizontal, tr("Guarantee start"));
ptrProductionModel->setHeaderData(cabinet_model, Qt::Horizontal, tr("Cabinet model"));
ptrProductionModel->setHeaderData(front_model, Qt::Horizontal, tr("Front model"));
ptrProductionModel->setHeaderData(lot_id, Qt::Horizontal, tr("Lot id"));
ptrProductionModel->setHeaderData(banknote_reader, Qt::Horizontal, tr("Banknote reader"));
ptrProductionModel->setHeaderData(banknote_reader_num, Qt::Horizontal, tr("Banknote reader num."));
ptrProductionModel->setHeaderData(money_reader, Qt::Horizontal, tr("Money reader"));
ptrProductionModel->setHeaderData(money_reader_num, Qt::Horizontal, tr("Money reader num."));
ptrProductionModel->setHeaderData(hopper, Qt::Horizontal, tr("Hopper"));
ptrProductionModel->setHeaderData(hopper_num, Qt::Horizontal, tr("Hopper num."));
ptrProductionModel->setHeaderData(card_dispenser, Qt::Horizontal, tr("Card dispenser"));
ptrProductionModel->setHeaderData(card_dispenser_num, Qt::Horizontal, tr("Card dispenser num."));
ptrProductionModel->setHeaderData(card_system_model, Qt::Horizontal, tr("Card system model"));
ptrProductionModel->setHeaderData(card_system_model_num, Qt::Horizontal, tr("Card system model num."));
ptrProductionModel->setHeaderData(code_card, Qt::Horizontal, tr("Code card"));
ptrProductionModel->setHeaderData(card_release, Qt::Horizontal, tr("Card release"));
ptrProductionModel->setHeaderData(card_release_num, Qt::Horizontal, tr("Card release num."));
ptrProductionModel->setHeaderData(test_date, Qt::Horizontal, tr("Test date"));
ptrProductionModel->setHeaderData(tester, Qt::Horizontal, tr("Tester"));
ptrProductionModel->setHeaderData(notes, Qt::Horizontal, tr("Notes"));
ptrProductionModel->setHeaderData(active_flag, Qt::Horizontal, tr("Active flag"));
ptrProductionModel->setFilter("FLAG_ATTIVO = 1");
ptrProductionModel->select();


I think that is a problem with QSqlTableModel::setTable function. Can anyone help me?

Best regards.
Andrea

norobro
17th January 2011, 02:21
I don't see anything wrong with the code you posted. Did you properly initialize currentConnection?
Something like this:
QSqlDatabase currentConnection = QSqlDatabase::database("connectionName");
//where connectionName is the string you used in your call to addDatabase()

Use a qDebug statement to see what database you are operating on:
qDebug() << currentConnection.databaseName();
ptrProductionModel = new QSqlTableModel(this, currentConnection);
...

andreabiscuola
17th January 2011, 11:23
Hi norobro.

currentConnection is properly initialized and database test is going ok. I'm working with PostgreSQL 9.0.2.
I think that this is a problem relative to the query syntax posted from QPSQL to the database engine. Have you read nothing abount this? Do you know a method to show the last query executed on the database?

Thanks

andreabiscuola
17th January 2011, 14:32
Updates:

I tried to debug my database stack with a QDebug object and the result are:

- There is no errors for database connection, it result correctly up and running.
- There is no errors for the last database transaction executed. Operation required to the QSqlDatabase connection is ok.
- If i try to execute a QSqlQuery statement with my required query (wroted by hand), it fetch data from PostgreSQL correctly and i can bind all the values in the record.

My Opnion:
I think that this is a problem only of the QSqlTableModel stack and for query inserted with model functions. Now i try to give to the model a Query written by hand.

norobro
17th January 2011, 21:24
Hi Andrea,

Take a look this (http://bugreports.qt.nokia.com/browse/QTBUG-14206) bug report.

My test app worked fine on postgres 8.4 but after upgrading 9.0.1 I got the following warning on the command line: "This version of PostgreSQL is not supported and may not work." Plus lastError() reported "unable to find table foo".

After applying the patch in the bug report and recompiling the psql driver it works.

BTW I'm using Qt 4.7.0

HTH
Norm

ChrisW67
17th January 2011, 22:32
I think that this is a problem relative to the query syntax posted from QPSQL to the database engine. Have you read nothing abount this? Do you know a method to show the last query executed on the database?

QSqlTableModel::selectStatement() should give you the query the model will try to execute.

andreabiscuola
18th January 2011, 10:58
Hi norobro!

The bug report you indicated respond completely to my problem! I want to try the patch suggested but i have a doubt: Why the problem appear only using the QSqlTableModel and not with the QSqlQuery?

norobro
18th January 2011, 23:33
Here (http://lists.qt.nokia.com/pipermail/qt-interest/2010-November/028730.html) is a thread on Qt-interest about this.


Why the problem appear only using the QSqlTableModel and not with the QSqlQuery?I guess you could dig into the source code to figure out why :D