[SOLVED] About setting a QSqlTableModel to a QStringList
-
have you tested my code?
-
have you tested my code?
[quote author="qxoz" date="1359542026"]never mind[/quote] -
Your code should work:
@QSqlTableModel *m = new QSqlTableModel();
m->setTable("table");
m->select();QStringList stringList; for(int i = 0; i < m->rowCount(); i++) stringList.append( m->record(i).value(your_index_column).toString());@
but Andre shows more optimal variant
-
Thanks for the reply guys. Why I said I'm using a model is because I have been asked to use a model instead of direct SQL queries. I will however show both versions i.e. maxoreli and Andre's code, to my mentor and see which one I can use. I'll get back to you guys and tell you which one I finally used.
Thanks a lot!
-
Hi again!
Just an update to let y'all know that I went with Andre's solution. Using models became very complicated and resulted in many many lines of code. So yeah, I used QSqlQuery directly.
On the other hand, when I tried maxoreli's solution, I had
@
model = new QSqlQueryModel();
model->setQuery(s);
label->setText(model->record(0).toString();
@and this gives me an error:
- Use of undefined type QSqlRecord
- Left of .toString must point to class/struct/union/generic type
Why?
-
Add #include<QSqlRecord> on top of your source
-
That did it! Thanks. I included QSqlQueryModel and was wondering why it isn't working. Thanks a lot.
-
If still not works do following steps:
- Build -> Clean All
- Build -> Run qmake
- Build -> Rebuild All
-
Thank you. I had to edit the previous post cuz I thought you said include QSqlQueryModel and not QSqlRecord
-
EDIT: Please ignore the following comment. There was a problem with my database and not my code.
Ok guys, I followed Andre's method and I'm not getting any errors. However, I'm not getting any output either. Here's the code
@
QSqlQuery query = QSqlQuery(db); //db is the database object
QString st = QString("SELECT TIME FROM SCHEDULE WHERE ID = %1").arg(str);
query.prepare(st);
query.exec();QStringList strlist;
while(query.next())
{
strlist.append(query.value(0).toString());
qDebug()<<query.value(0).toString();
}
@The qDebug does not display anything. Where am I going wrong? Please help.