Skip to content
  • Categories
  • Recent
  • Tags
  • Popular
  • Users
  • Groups
  • Search
  • Get Qt Extensions
  • Unsolved
Collapse
Brand Logo
  1. Home
  2. Qt Development
  3. General and Desktop
  4. [SOLVED] SQLite Update query from LineEdit
Forum Updated to NodeBB v4.3 + New Features

[SOLVED] SQLite Update query from LineEdit

Scheduled Pinned Locked Moved General and Desktop
4 Posts 3 Posters 3.8k Views 1 Watching
  • Oldest to Newest
  • Newest to Oldest
  • Most Votes
Reply
  • Reply as topic
Log in to reply
This topic has been deleted. Only users with topic management privileges can see it.
  • N Offline
    N Offline
    nHx4U
    wrote on 6 Jan 2014, 03:51 last edited by
    #1

    Hello;

    I have looked at (and tried) many of the responses that have touched on this issue, but I still can't get it to work. The "insert/add query works with LineEdit variables BUT the update query (edit existing record) only works properly when I hardcode a value in the where = . The other fields are read/written correctly from the LineEdit fields on the form. code segment is attached along with error messages.

    Thanks for your help.

    CUSTOMERINFO.CPP

    @#include "customerinfo.h"
    #include "ui_customerinfo.h"
    #include<QMessageBox>

    CustomerInfo::CustomerInfo(QWidget *parent) :
    QDialog(parent) ,
    ui(new Ui::CustomerInfo)
    {
    ui->setupUi(this);

    Login conn;
         if (!conn.connOpen())
                 ui->label_sec_status->setText("CONNECTION FAILED");
         else
                ui->label_sec_status->setText("CONNECTED. . . . .");
    

    }

    CustomerInfo::~CustomerInfo()
    {
    delete ui;
    }

    void CustomerInfo::changeEvent(QEvent *e)
    {

    }

    void CustomerInfo::on_pushButton_Add_clicked()
    {
    Login conn;
    QString PsW, LastName, UserName, LotNo;
    PsW=ui->txt_cid->text();
    LastName=ui->txt_lastname->text();
    UserName=ui->txt_username->text();
    LotNo=ui->txt_lotno->text();

    if(!conn.connOpen())
    {
        qDebug()<<"Connection Failed";
        return;
    }
    
    conn.connOpen();
    QSqlQuery qry;
    

    // IF MYDB.OPEN

        {
    
       qry.prepare("INSERT into Test_LOGIN (PassWord,LastName,UserName,LotNo) values (?,?,?,?)");
    
        qry.addBindValue(PsW);   // if ? as values in query then bindvalue as LineEdit name ----- txt_cid etc
        qry.addBindValue(LastName);
        qry.addBindValue(UserName);
        qry.addBindValue(LotNo);
    
        if(qry.exec&#40;&#41; &#41;
    
          {
            QMessageBox::critical(this, tr("Save"), tr("Saved"));
            conn.connClose();
           }
        else
        {
            QMessageBox::critical(this, tr("Error"),qry.lastError().text() );
        }
    

    }
    }

    void CustomerInfo::on_pushButton_Edit_clicked()
    {
    Login conn;
    QString PsW, LastName, UserName, LotNo;
    PsW=ui->txt_cid->text();
    LastName=ui->txt_lastname->text();
    UserName=ui->txt_username->text();
    LotNo=ui->txt_lotno->text();

    if(!conn.connOpen())
    {
    qDebug()<<"Connection Failed";
    return;
    }

    conn.connOpen();
    QSqlQuery qry;

    // IF MYDB.OPEN

       {
       qry.prepare("UPDATE Test_LOGIN set PassWord=?, LastName=? , UserName=?, LotNo=?  WHERE PassWord=5");
    
       qry.addBindValue(PsW);   // if ? as values in query then bindvalue as LineEdit name ----- txt_cid etc
       qry.addBindValue(LastName);
       qry.addBindValue(UserName);
       qry.addBindValue(LotNo);
    
       if(qry.exec&#40;&#41; &#41;
    
         {
    
           qDebug()<<"OK ROWS AFFECTED AAA "<<UserName<<"  "<< qry.numRowsAffected();
            QMessageBox::critical(this, tr("Edit"), tr("Updated"));
           conn.connClose();
          }
       else
       {
          qDebug()<<"ERROR ROWS AFFECTED 222 "<<pwd<<"  "<< qry.numRowsAffected();
           QMessageBox::critical(this, tr("Error"),qry.lastError().text() );
       }
    

    }
    }
    @


    *MESSAGES:
    *
    When “PassWord” field hard coded as “5”
    database location: "C:/Users/Steve/Desktop/Qt_Tut-015/SQLIte_Login/SQLIte_Login/debug/data/dbCURR_SQLite.db"
    CONNECTED. . . . .
    OK ROWS AFFECTED AAA "7" 1
    QSqlDatabasePrivate::removeDatabase: connection 'qt_sql_default_connection' is still in use, all queries will cease to work.

    *When “?” replaces “5”
    *
    Message Box Error
    Parameter count mismatch

    *qDebug error:

    *CONNECTED. . . . .
    ERROR ROWS AFFECTED 222 "" -1

    1 Reply Last reply
    0
    • J Offline
      J Offline
      jmimi
      wrote on 6 Jan 2014, 05:08 last edited by
      #2

      Why you open database several times?!!!

      For "Parameter count mismatch" error, I say when you bind values according to your case, you should obey count of calling addBindValues and count of '?' symbol.
      It's better you bind by names according below example:
      @
      qry.prepare("UPDATE Test_LOGIN set PassWord=:pass, LastName=:name WHERE PassWord=:pass");
      qry.bindValue(":pass", password);
      qry.bindValue(":name", display_name);
      @
      Now the bound name is important not count of parameters.

      K 1 Reply Last reply 19 Feb 2020, 21:44
      1
      • N Offline
        N Offline
        nHx4U
        wrote on 6 Jan 2014, 20:00 last edited by
        #3

        Hi jmimi:

        Thank you very much. It actually works! :-)

        1 Reply Last reply
        0
        • J jmimi
          6 Jan 2014, 05:08

          Why you open database several times?!!!

          For "Parameter count mismatch" error, I say when you bind values according to your case, you should obey count of calling addBindValues and count of '?' symbol.
          It's better you bind by names according below example:
          @
          qry.prepare("UPDATE Test_LOGIN set PassWord=:pass, LastName=:name WHERE PassWord=:pass");
          qry.bindValue(":pass", password);
          qry.bindValue(":name", display_name);
          @
          Now the bound name is important not count of parameters.

          K Offline
          K Offline
          KingslyEn
          wrote on 19 Feb 2020, 21:44 last edited by
          #4

          @jmimi said in [SOLVED] SQLite Update query from LineEdit:

          qry.prepare("UPDATE Test_LOGIN set PassWord=:pass, LastName=:name WHERE PassWord=:pass");

          It's useful yet :) Thanks a lot...

          1 Reply Last reply
          0

          • Login

          • Login or register to search.
          • First post
            Last post
          0
          • Categories
          • Recent
          • Tags
          • Popular
          • Users
          • Groups
          • Search
          • Get Qt Extensions
          • Unsolved