QSql Update



  • Hi,
    I have the following code to update the db:

    model->setData (index,edit->text ());
                QVariant f(index.data(Qt::DisplayRole));
                QString modified;
                modified = f.toString ();
                qDebug() << "Modified: " << modified;
    
                QModelIndex updateIndex(index.model ()->index(index.row (),0,index.parent ()));
    
                QString fixID;
                QVariant v(updateIndex.data (Qt::DisplayRole));
                fixID = v.toString ();
                qDebug() << "FixID: " << fixID;
    
                QSqlQuery fixquery;
    
                fixquery.prepare("UPDATE Items SET (Name) VALUES :Name WHERE ID = :fixID");
                fixquery.bindValue (":Name", modified);
    

    I ran debug and all the values are correct (in fixID and modified) but there are no changes in the db. What did I miss? Thank you.



  • hi,

    did u call fixquery.exec();



  • @Pradeep-Kumar
    Hi,
    Now I did:

        fixquery.prepare("UPDATE Items SET (Name) VALUES :Name WHERE ID = :fixID");
                fixquery.bindValue (":Name", modified);
    
                fixquery.exec ();
    

    but same results.



  • Syntax for update query:

    UPDATE tablename SET columnname = 'value' WHERE columnname = 'value';

    can u try

            QSqlQuery fixquery;
            fixquery.prepare("UPDATE Items SET Name =  :Name WHERE ID = :fixID");
            fixquery.bindValue (":Name", modified);
            fixquery.bindValue (":fixID", fixID );
            fixquery.exec ();
    

    Hope this helps you,

    Thanks,



  • @Pradeep-Kumar
    Thank you. It worked.



  • Hi
    @gabor53

    Good to hear,it helped you.

    Cheers,
    Thanks,


Log in to reply
 

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