Problem with setFilter(), sql lite
-
Hi I have problem with my project, I have database and I don't know how to display in QTableView only what I want to find in database. I have button for adding and for deleting too, but button for finding don't work.
this is my code
query = new QSqlQuery(db);
query->exec("CREATE TABLE IF NOT EXISTS Filmy (Jméno TEXT,Žánr TEXT,Hodnocení INTEGER,Poznámky TEXT, PRIMARY KEY(Jméno));");model = new QSqlTableModel(this, db); model->setTable("Filmy"); model->select(); model->setHeaderData(0, Qt::Horizontal, "Název"); model->setHeaderData(1, Qt::Horizontal, "Žánr"); model->setHeaderData(2, Qt::Horizontal, "Hodnocení"); model->setHeaderData(3, Qt::Horizontal, "Poznámky"); model->setSort(2, Qt::DescendingOrder); ui->tableView->resizeColumnsToContents(); ui->tableView->horizontalHeader()->setSectionsClickable(true); ui->tableView->horizontalHeader()->setSectionsMovable(true); ui->tableView->setSortingEnabled(true); ui->tableView->setModel(model);
this is function for search button
void MainWindow::on_pushButton_3_clicked()
{ // vratim to co jsem napsal
model = new QSqlTableModel(this, db);
const QString edit = ui->lineEdit->text();
model->setTable("Filmy");
//qDebug()<<edit;
model->setFilter("Název == Pád do tmy");
//model->setFilter(QString("Název = '%1'").arg(edit));
model->select();
ui->tableView->setModel(model);I work in qt creator. Thank you for every response
-
@Pato55 said in Problem with setFilter(), sql lite:
model->setFilter("Název == Pád do tmy");
What about:
model->setFilter("Název = 'Pád do tmy'");
-
@Pato55 From documentation (https://doc.qt.io/qt-5/qsqltablemodel.html#setFilter): "The filter is a SQL WHERE clause without the keyword WHERE". But in your create table query there is no column called "Název", so how should this work?
-
@Pato55 said in Problem with setFilter(), sql lite:
thank you for saving my study life
If your issue is solved, please don't forget to mark your post as such!