[SOLVED]QSqlTableModel and QTableView
-
I've been bashing my head for many hours over this and I can't find any info anywhere. I'm just trying to get the data out of a mySQL database and stick it into the qtableview. I just can't get any data there though. I'm hoping someone here can help out.
here's my code
@
QSqlDatabase *db = new QSqlDatabase();
void MainWindow::connectRoot(QString rootPw)
{*db = QSqlDatabase::addDatabase("QMYSQL","myconnection"); db->setHostName("localhost"); db->setDatabaseName("testTable"); db->setUserName("root"); db->setPassword(rootPw);
if (!db->open())
qDebug() << "Failed to connect to root mysql admin";
else
{
qDebug()<<"Opened the connection";
db->close();
}}
void MainWindow::onLoadData()
{
if(db->isValid()==false)
{
connectRoot("");
}
db->open();
QSqlTableModel *model=new QSqlTableModel(ui->tableView,*db);
qDebug()<<"opened the database";
model->setTable("table");if(model->select()==TRUE) { qDebug()<<"Selected"; } else { qDebug()<<"Select Failed"; }
int rowcount= model->rowCount();
qDebug()<<rowcount;
int columncount = model->columnCount();
qDebug()<<columncount;if(db->isOpen() ) { db->close(); qDebug()<<"closed the database"; } ui->tableView->setModel(model);
}
@And here's the debug output
Opened the connection
opened the database
Select Failed
0
2
closed the databaseso for some reason, the select statement is failing. There is data in the mySQL database and the the columns are being displayed by the control. Also, I've tried in vein to get the SQL Queary that the model is using, but that has also proven too difficult for me to figure out.
Anybody here have any idea what I've been doing wrong?
-
A shot in the dark here, but is your table actually named "table" or was that just a pseudo expression? If it is actually named table, try using back ticks around the name: "
table
" (table is a reserved word) and be sure that you use the correct case as it is case sensitive by default (though you may be able to override that behavior in your config settings). -
That works too, Volker.
Thanks! I should have thought of that too...I just spent some time playing with this, it's probably because this is my first trip into the QT world (I've written almost everything in C#.NET) , but it seems odd to me that
@
qDebug()<<db->lastError();
@
does not return any error.
where
@
qDebug() << model->lastError();
@
does return an error.
Aren't these equivalent since I declared it the qsqltablemodel explicitly?
@
QSqlTableModel *model=new QSqlTableModel(ui->tableView,*db);
@I really like the QT framework so far, but the documentation leaves a little to be desired to me. Is there a book that you might recommend?
-
I'm not 100 percent sure, but I would say that there was no error on the database connection, and thus db->lastErorr() yields nothing, whereas there was an error on the model/SQL query, so this returns an error. I would expect the database to return an error if, say, the password is wrong. At least this would make sense to me :-)
The Qt docs on http://doc.qt.nokia.com/4.7/index.html have an introduction, there is a "getting started":http://doc.qt.nokia.com/4.7/gettingstartedqt.html guide online (with some translations here in the wiki). The wiki also has a list of books.