How to use SQLite with Qt

I want to show only one row from my database in qt in 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 entire column, but I only want to show row 17 of that column.

+3


source to share


2 answers


As @ h-gomaa said, you need to write your query correctly.

When you call prepare

it should be something like this if you have id

in your table:



qry->prepare(QString("SELECT username FROM Waehrung_MMI WHERE id = :id"));
qry->bindValue(":id", 17);

      

0


source


If you want a single line then set the limit as 1 like



   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());
}

      

0


source







All Articles