No query Unable to fetch row issue.



  • I have written a piece of code that loads sqlite database into qtableview. Than I've successfully implemented SELECT statement ```

     QModelIndex index = ui->tableView->selectionModel()->currentIndex();
     QString value= ui->tableView->model()->data(index).toString();
     qDebug() << "Value : " << value;
    
    QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    db.setDatabaseName("TestDatabase.db");
    
    if(!db.open())
    {
        qDebug() << db.lastError();
        qFatal("Failed to connect");
    }
    QSqlQuery qry;
    qry.prepare("SELECT * FROM movies WHERE Title='"+value+"'");
    
    if(qry.exec())
    {
        while(qry.next())
        {
            ui->titleEdit->setText(qry.value(0).toString());
        }
    

    However, when I try to delete record from database the same way it throws me 'No query Unable to fetch row'.

     QModelIndex index = ui->tableView->selectionModel()->currentIndex();
    
        QString value= ui->tableView->model()->data(index).toString();
        qDebug() << "Value : " << value;
    
        QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
        db.setDatabaseName("TestDatabase.db");
    
        if(!db.open())
        {
            qDebug() << db.lastError();
            qFatal("Failed to connect");
        }
        QSqlQuery qry;
        qry.prepare("DELETE * FROM movies WHERE Title='"+value+"'");
        qry.bindValue(":Title" , value);
         if(!qry.exec())
         {
            QMessageBox::critical(this, tr("error::") , qry.lastError().text());
    
         }
    

  • Qt Champions 2016

    hi
    You have
    qry.bindValue(":Title" , value);
    but not using the binding syntax ?
    ( in delete example)

    Also, if you look here
    http://www.sqlitetutorial.net/sqlite-delete/
    alt text

    To use "Title =" , you must be 100% sure its a 100% match.
    Big/small letters and all.

    You can use
    http://sqlitebrowser.org/

    to test out your sql.



  • I was trying to do the same thing using bind, but it was giving me 'Parameter count mismatch'. I am sure that it is 100% match, because these values are taken from the very same table.


  • Qt Champions 2016

    @MadScientist92
    Did u try in DB browser ?
    Then u know for sure if its your code or not.



  • Thank you very much for the tip. I've tried to run it in db browser and it turned out I didn't have to place '*' after DELETE.


  • Qt Champions 2016

    @MadScientist92
    oh, i completely missed that :(
    even posted picture without *
    (erhm)
    Super


Log in to reply
 

Looks like your connection to Qt Forum was lost, please wait while we try to reconnect.