[SOLVED]Change a single value in a table (SQLITE update not work)



  • in a previus post I analyze these code:

    @ int p = 0, roid, p1 = 0;
    QSqlQuery qy(db);
    qy.prepare("SELECT id_pro1 FROM program WHERE pro1 = :pro1");
    qy.bindValue(":pro1", "ffff");
    qy.exec();
    qy.next();
    p = qy.record().toInt();
    ui->lcdNumber_2->display(p);
    qy.clear();
    @
    the above code, it is ok, and in these void work perfectly other two SELECT and one INSERT, but another .... not work ...

    @ q1y.prepare("UPDATE program SET id_pro1 = :id_pro1 WHERE pro1 = ffff"); /these code not work/
    q1y.bindValue(":id_pro1", roid);
    q1y.exec();
    q1y.next();
    p1 = qy.record().toInt();
    ui->lcdNumber->display(p1);
    q1y.clear();@

    If I canghe these snip into:

    @ q1y.prepare("INSERT program VALUES id_pro1 = :id_pro1 WHERE pro1 = ffff"); /these code not work/
    q1y.bindValue(":id_pro1", roid);
    q1y.exec();
    q1y.next();
    p1 = qy.record().toInt();
    ui->lcdNumber->display(p1);
    q1y.clear();@

    I see the same result: id_pro1 does not change.

    some ideas??


  • Moderators

    is "ffff" a column in your sql table? or is it supposed to be a value? If it's a value shouldn't it look like 'value'?
    I may be wrong my SQL skills got a bit rusted over the last years ;)



  • no problem, ffff is a value .... the right code is (error by cntrlC/V from old post):

    @ q1y.prepare("INSERT program VALUES id_pro1 = :id_pro1 WHERE pro1 = ffff"); /these code not work/
    q1y.bindValue(":id_pro1", roid);
    q1y.exec();
    q1y.next();
    p1 = qy.value().toInt(); //// (error by cntrlC/V from old post)
    ui->lcdNumber->display(p1);
    q1y.clear();
    @

    And ffff is a value (actually is no a text but a int number)....


  • Moderators

    could you please post the (effective) query which goes to the SQL server.



  • But is the correct way to write these update?
    8program=table, id_pro1 value to change, pro1=fff ->set correct row)

    @q1y.prepare("UPDATE program SET id_pro1 = :id_pro1 WHERE pro1 = ffff");@



  • of course:

    @
    ui->lcdNumber->display(roid); //////////control & display roid int variable -> ok
    // ui->lcdNumber_2->display(p);

    QSqlQuery qram(db);
    qram.prepare("UPDATE valori SET (valore = :valore) WHERE (nome_valore = :nome_valore)");
       qram.bindValue(":nome_valore", "id_ricetta1"); /*id_ricetta1 is an existing value in the column nome_valore*/
       qram.bindValue(":valore", roid); /* I can not do the update of this cell*/
       qrom.exec();
       qDebug() << roid;
       qram.clear();@


  • @ QSqlQuery qram(db);
    qram.prepare("UPDATE valori SET valore=:valore WHERE nome_valore=:nome_valore");
    qram.bindValue(":nome_valore", "id_ricetta1");
    qram.bindValue(":valore", roid);
    qram.exec();
    qDebug() << roid;
    qram.lastError();
    qram.clear();@

    These work perfect!!!!


Log in to reply
 

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