Reputation: 39
I want to show only a single row from my database in qt in a tableview
.
This is my current code:
void Favorites::on_pushButton_load_fav_clicked()
{
MainWindow conn;
QSqlQueryModel *modal = new QSqlQueryModel();
conn.connOpen();
QSqlQuery *qry = new QSqlQuery(conn.mydb);
qry->prepare("select username from Waehrung_MMI");
qry->exec();
modal->setQuery(*qry);
ui->tableView_favs->setModel(modal);
conn.connClose();
qDebug () << (modal->rowCount());
}
Now it shows the whole column but I only want to show for example row 17 of this column.
Upvotes: 3
Views: 88
Reputation: 4380
You can use the SQL's limit functionality in order to get a single row.
void Favorites::on_pushButton_load_fav_clicked() {
MainWindow conn;
QSqlQueryModel * modal = new QSqlQueryModel();
conn.connOpen();
QSqlQuery * qry = new QSqlQuery(conn.mydb);
qry->prepare("select username from Waehrung_MMI limit 1");
qry->exec();
modal->setQuery( * qry);
ui->tableView_favs->setModel(modal);
conn.connClose();
qDebug() << (modal->rowCount());
}
Upvotes: 1
Reputation: 7170
As @h-gomaa said, you need to write your query properly.
When you call prepare
, it should be something like this, assuming you have an id
in your table:
qry->prepare(QString("SELECT username FROM Waehrung_MMI WHERE id = :id"));
qry->bindValue(":id", 17);
Upvotes: 0