I am having problems using the select function for QSqlTableModel. It always returns false and I have been unable to figure out why.
I have setup a SQL Database using PostgreSQL. I was able to add the database using QSqlDatabase::addDatabase and I have been able to use QSqlQuery to pull data from the database and create new tables and new rows to tables. I am now trying to display the database in a TableView. I found that you can create the QSqlTableModel to plug into the TableView. However, it is failing on the "select" step.
Here is the code I am running:
_database = loadDatabase();
qDebug()<<"table database name: "<<_database.databaseName()<<endl; //Returns: table database name: "postgres"
qDebug()<<"table database isOpen: "<<_database.isOpen()<<endl; //Returns: table database isOpen: true
qDebug()<<"table database tables: "<<_database.tables()<<endl; //Returns: table database tables: ("Test", "testtable")
qDebug()<<"last error: "<<_database.lastError().text()<<endl; //Returns: last error: " "
_tableModel = new QSqlTableModel(this,_database);
_tableModel->setTable(_database.tables().at(1));
_tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
if(_tableModel->select()){
qDebug()<<"Table was selected!"<<endl;
}
else{
qDebug()<<"Table could not be selected"<<endl;
}
qDebug()<<"is database valid: "<<_database.isValid()<<endl; //Returns: is database valid: true
"_database" is defined as a QSqlDatabase. It always returns "Table could not be selected".
I found an answer on Stack Overflow that suggested to check whether the database isOpen(), the table name is correct, and if there is any error messages with lastError(). I included those answers in the debug statements in my code. Everything appears to be correct, but yet it returns a false.
Any help would be greatly appreciated. Thanks!