Important: Please read the Qt Code of Conduct - https://forum.qt.io/topic/113070/qt-code-of-conduct

Qt5 Code Not Inserting values into SQL database



  • We have an sample application program with codes which are originally written in Qt4, they are compiling and application program runs perfectly without any issues,but when we port it in Qt5 now, the same is not inserting any values in to the Sql database.We are using sqllite database.But whenever we select and edit the exiting data which was created earlier or default data , we are able to select & edit and the same and the same actions are performed without any issues!. Can anyone suggest the probable causes. Thanks in Advance!


  • Lifetime Qt Champion

    Hi and welcome to devnet,

    You should provide the exact Qt versions you are using.
    A minimal compilable example would also be good so people can reproduce your issue.



  • The code written in Qt 4.8.7 is working without issues, but now,we ported the same code to Qt 5.15.1 ,after completing the porting formalities , it compiles without displaying any errors.We are able to Select& edit the existing data,but not able to create a new data.Refer below our sample code.

    void adminmaster::on_save_clicked()
    {
    clearline();
    lineEditkeyboard->hide();
    lineEditkey->hide();
    empty=true;
    QSqlQueryModel * name= new QSqlQueryModel;
    QSqlQueryModel * codu= new QSqlQueryModel;
    ui->lineEdit->setReadOnly(false);
    if (ui->comboBox->isHidden())
    {
    if(ui->nameline->isEnabled())
    {
    if (ui->nameline->text().isEmpty())
    {
    empty=false;
    msgbox.setText("Enter Admin Name!");
    msgbox.exec();
    }
    }
    }
    if(ui->comboBox_2->isHidden())
    {
    if(ui->codeline->isEnabled())
    {
    if (ui->codeline->text().isEmpty())
    {
    empty=false;
    msgbox.setText("Enter Admin code!");
    msgbox.exec();
    }
    }
    }
    if(ui->lineEdit->isEnabled())
    {
    if (ui->lineEdit->text().isEmpty())
    {
    empty=false;
    msgbox.setText("Enter password!");
    msgbox.exec();
    }
    }
    if (ui->lineEdit_2->isEnabled())
    {
    if (ui->lineEdit_2->text().isEmpty())
    {
    empty=false;
    msgbox.setText("Enter Auto Expiry");
    msgbox.exec();
    }
    }
    if(ui->comboBox_3->isEnabled())
    {
    if (ui->comboBox_3->currentText().isEmpty())
    {
    empty=false;
    msgbox.setText("Select Enable or Disable!");
    msgbox.exec();
    }
    }
    user=ui->comboBox->currentText();
    able=ui->comboBox_3->currentText();
    pass=ui->lineEdit->text();
    expi=ui->lineEdit_2->text();
    aut=expi.toInt();
    currentdate=QDate::currentDate();
    cur=currentdate.toString("dd-MM-yyyy");
    dateexpiry=currentdate.addDays(aut);
    sqlexpir=dateexpiry.toString("dd-MM-yyyy");
    las=expi.toInt();
    if(las>365)
    {
    msgbox.setText("Please Enter the Value Less than 365 Days");
    msgbox.exec();
    }
    if(na==0&&co==0)
    {
    qr.prepare("Select count(admin_name) from Administrator ");
    if( !qr.exec() )
    qFatal( "Failed to get table images" );
    while(qr.next())
    {
    c_sno=qr.value(0).toInt();
    }
    }
    if(na!=0&&co==0)
    {
    qr.prepare("Select count(admin_emp_code) from Administrator ");
    if( !qr.exec() )
    qFatal( "Failed to get table images" );
    while(qr.next())
    {
    c_sno=qr.value(0).toInt();
    }
    }
    if(na==0&&co!=0)
    {
    qr.prepare("Select count(admin_name) from Administrator ");
    if( !qr.exec() )
    qFatal( "Failed to get table images" );
    while(qr.next())
    {
    c_sno=qr.value(0).toInt();
    }
    }
    if( c_sno<=3 && check==true && na==0 && co==0 && las<=365 && empty==true)
    {
    if(ch==true)
    {
    user=ui->nameline->text();
    }
    else if(ch ==false)
    {
    user=ui->comboBox->currentText();
    }
    code=ui->codeline->text();
    qr.prepare("Select * from administrator where admin_name like :user");
    qr.bindValue(":user",user);
    if( !qr.exec() )
    qFatal( "Failed to update table images" );
    if (qr.next())
    {
    if(ch==false)
    {
    update=false;
    if(auexp==true)
    {
    user=ui->comboBox->currentText();
    code=ui->codeline->text();
    qr.prepare("Select * from administrator where admin_emp_code = :code and admin_name <> :user ");
    qr.bindValue(":code",code);
    qr.bindValue(":user",user);
    if( !qr.exec() )
    qFatal( "Failed to update table images" );
    if (qr.next())
    {
    update=true;
    msgbox.setText("Admin code Already exists");
    msgbox.exec();
    }
    if(update==false)
    {
    qr.prepare("Update administrator set admin_emp_code=:code,admin_pass=:pass,auto_expiry_days=:expi,user_enable_disable=:able,current_date=:cur,expired_date=:sqlexpir where admin_name=:user");
    qr.bindValue(":user",user);
    qr.bindValue(":code",code);
    qr.bindValue(":pass",pass);
    qr.bindValue(":able",able);
    qr.bindValue(":expi",expi);
    qr.bindValue(":cur", cur);
    qr.bindValue(":sqlexpir", sqlexpir);
    if( !qr.exec() )
    qFatal( "Failed to update table images" );
    msgbox.setText("Admin Master Updated");
    msgbox.exec();
    ui->comboBox->setEditable(false);
    ui->comboBox_2->setEditable(false);
    ui->label_3->hide();
    ui->label_4->hide();
    ui->label_5->hide();
    ui->lineEdit->hide();
    ui->lineEdit_2->hide();
    ui->comboBox_3->hide();
    ui->new_2->setEnabled(true);
    ui->delete_2->setEnabled(true);
    ui->edit->setEnabled(true);
    }
    }
    else if(auexp==false)
    {
    qr.prepare("Select * from administrator where admin_emp_code = :code and admin_name <> :user ");
    qr.bindValue(":code",code);
    qr.bindValue(":user",user);
    if( !qr.exec() )
    qFatal( "Failed to update table images" );
    if (qr.next())
    {
    update=true;
    msgbox.setText("Admin code Already exists");
    msgbox.exec();
    }
    if(update==false)
    {
    qr.prepare("Update administrator set admin_emp_code=:code,admin_pass=:pass,user_enable_disable=:able where admin_name=:user");
    qr.bindValue(":user",user);
    qr.bindValue(":code",code);
    qr.bindValue(":pass",pass);
    qr.bindValue(":able",able);
    if( !qr.exec() )
    qFatal( "Failed to update table images" );
    msgbox.setText("Admin Master Updated");
    msgbox.exec();
    ui->comboBox->setEditable(false);
    ui->comboBox_2->setEditable(false);
    ui->label_3->hide();
    ui->label_4->hide();
    ui->label_5->hide();
    ui->lineEdit->hide();
    ui->lineEdit_2->hide();
    ui->comboBox_3->hide();
    ui->new_2->setEnabled(true);
    ui->delete_2->setEnabled(true);
    ui->edit->setEnabled(true);
    }
    }
    }
    if(ch==true)
    {
    user=ui->nameline->text();
    qr.prepare("Select * from administrator where admin_name like :user");
    qr.bindValue(":user",user);
    if( !qr.exec() )
    qFatal( "Failed to update table images" );
    if (qr.next())
    {
    msgbox.setText("Admin Master Already Exists");
    msgbox.exec();
    ui->new_2->setEnabled(true);
    ui->delete_2->setEnabled(true);
    ui->edit->setEnabled(true);
    }
    }
    }
    else
    {
    insert = false;
    qr.prepare("Select * from administrator where admin_emp_code = :code and admin_name <> :user ");
    qr.bindValue(":code",code);
    qr.bindValue(":user",user);
    if( !qr.exec() )
    qFatal( "Failed to update table images" );
    if (qr.next())
    {
    insert=true;
    msgbox.setText("Admin code Already exists");
    msgbox.exec();
    }
    if(insert==false)
    {
    qr.prepare ("insert into administrator(admin_name,admin_emp_code,admin_pass,auto_expiry_days,user_enable_disable,current_date,expired_date) values (:user, :code, :pass, :expi, :able, :cur, :sqlexpir)") ;
    qr.bindValue(":admin_name", user);
    qr.bindValue(":admin_emp_code", code);
    qr.bindValue(":admin_pass", pass);
    qr.bindValue(":auto_expiry_days", expi);
    qr.bindValue(":user_enable_disable", able);
    qr.bindValue(":current_date", cur);
    qr.bindValue(":expired_date", sqlexpir);
    qr.exec();
    msgbox.setText("Admin Master Created");
    msgbox.exec();
    ui->comboBox->setEditable(false);
    ui->comboBox_2->setEditable(false);
    ui->label_3->hide();
    ui->label_4->hide();
    ui->label_5->hide();
    ui->lineEdit->hide();
    ui->lineEdit_2->hide();
    ui->comboBox_3->hide();
    ui->new_2->setEnabled(true);
    ui->delete_2->setEnabled(true);
    ui->edit->setEnabled(true);
    name->setQuery("Select distinct(admin_name) from administrator");
    ui->comboBox->setModel(name);
    }
    }
    }
    if(c_sno<=3&&check==true&&na!=0&&co==0&&las<=365 &&empty==true)
    {
    if(ch==true)
    {
    code=ui->codeline->text();
    }
    else if(ch ==false)
    {
    code=ui->comboBox_2->currentText();
    }
    qr.prepare("Select * from administrator where admin_emp_code like :code");
    qr.bindValue(":code",code);
    if( !qr.exec() )
    qFatal( "Failed to update table images" );
    if (qr.next())
    {
    if(ch==false)
    {
    if(auexp==true)
    {
    qr.prepare("Update administrator set admin_pass=:pass,auto_expiry_days=:expi,user_enable_disable=:able,current_date=:cur,expired_date=:sqlexpir where admin_emp_code=:code");
    qr.bindValue(":code",code);
    qr.bindValue(":pass",pass);
    qr.bindValue(":expi",expi);
    qr.bindValue(":able",able);
    qr.bindValue(":cur", cur);
    qr.bindValue(":sqlexpir", sqlexpir);
    if( !qr.exec() )
    qFatal( "Failed to update table images" );
    msgbox.setText("Admin Master Updated");
    msgbox.exec();
    ui->comboBox->setEditable(false);
    ui->comboBox_2->setEditable(false);
    ui->label_3->hide();
    ui->label_4->hide();
    ui->label_5->hide();
    ui->lineEdit->hide();
    ui->lineEdit_2->hide();
    ui->comboBox_3->hide();
    ui->new_2->setEnabled(true);
    ui->delete_2->setEnabled(true);
    ui->edit->setEnabled(true);
    }
    else if(auexp==false)
    {
    qr.prepare("Update administrator set admin_pass=:pass,user_enable_disable=:able where admin_emp_code=:code");
    qr.bindValue(":code",code);
    qr.bindValue(":pass",pass);
    qr.bindValue(":able",able);
    if( !qr.exec() )
    qFatal( "Failed to update table images" );
    msgbox.setText("Admin Master Updated");
    msgbox.exec();
    ui->comboBox->setEditable(false);
    ui->comboBox_2->setEditable(false);
    ui->label_3->hide();
    ui->label_4->hide();
    ui->label_5->hide();
    ui->lineEdit->hide();
    ui->lineEdit_2->hide();
    ui->comboBox_3->hide();
    ui->new_2->setEnabled(true);
    ui->delete_2->setEnabled(true);
    ui->edit->setEnabled(true);
    }
    }
    else if(ch==true)
    {
    insert=false;
    code=ui->codeline->text();
    qr.prepare("Select * from administrator where admin_emp_code like :code");
    qr.bindValue(":code",code);
    if( !qr.exec() )
    qFatal( "Failed to update table images" );
    if (qr.next())
    {
    insert=true;
    msgbox.setText("Admin Master Already Exists");
    msgbox.exec();
    ui->new_2->setEnabled(true);
    ui->delete_2->setEnabled(true);
    ui->edit->setEnabled(true);
    }
    ui->new_2->setEnabled(true);
    ui->delete_2->setEnabled(true);
    ui->edit->setEnabled(true);
    }
    }
    else
    {
    qr.prepare ("insert into administrator(admin_emp_code,admin_pass,auto_expiry_days,user_enable_disable,current_date,expired_date) values ( :code, :pass, :expi, :able, :cur, :sqlexpir)") ;
    qr.bindValue(":admin_emp_code", code);
    qr.bindValue(":admin_pass", pass);
    qr.bindValue(":auto_expiry_days", expi);
    qr.bindValue(":user_enable_disable", able);
    qr.bindValue(":current_date", cur);
    qr.bindValue(":expired_date", sqlexpir);
    qr.exec();
    msgbox.setText("Admin Master Created");
    msgbox.exec();
    ui->comboBox->setEditable(false);
    ui->comboBox_2->setEditable(false);
    ui->label_3->hide();
    ui->label_4->hide();
    ui->label_5->hide();
    ui->lineEdit->hide();
    ui->lineEdit_2->hide();
    ui->comboBox_3->hide();
    ui->new_2->setEnabled(true);
    ui->delete_2->setEnabled(true);
    ui->edit->setEnabled(true);
    codu->setQuery("Select distinct(admin_emp_code) from administrator");
    ui->comboBox_2->setModel(codu);
    }
    }
    if(c_sno<=3&&check==true&&na==0&&co!=0&&las<=365 &&empty==true)
    {
    if(ch==true)
    {
    user=ui->nameline->text();
    }
    else if(ch ==false)
    {
    user=ui->comboBox->currentText();
    }
    qr.prepare("Select * from administrator where admin_name like :user");
    qr.bindValue(":user",user);
    if( !qr.exec() )
    qFatal( "Failed to update table images" );
    if (qr.next())
    {
    if(ch==false)
    {
    if(auexp==true)
    {
    user=ui->comboBox->currentText();
    qr.prepare("Update administrator set admin_pass=:pass,auto_expiry_days=:expi,user_enable_disable=:able,current_date=:cur,expired_date=:sqlexpir where admin_name=:user");
    qr.bindValue(":user",user);
    qr.bindValue(":pass",pass);
    qr.bindValue(":able",able);
    qr.bindValue(":expi",expi);
    qr.bindValue(":cur", cur);
    qr.bindValue(":sqlexpir", sqlexpir);
    if( !qr.exec() )
    qFatal( "Failed to update table images" );
    msgbox.setText("Admin Master Updated");
    msgbox.exec();
    ui->comboBox->setEditable(false);
    ui->comboBox_2->setEditable(false);
    ui->label_3->hide();
    ui->label_4->hide();
    ui->label_5->hide();
    ui->lineEdit->hide();
    ui->lineEdit_2->hide();
    ui->comboBox_3->hide();
    ui->new_2->setEnabled(true);
    ui->delete_2->setEnabled(true);
    ui->edit->setEnabled(true);
    }
    }
    else if(auexp==false)
    {
    qr.prepare("Update administrator set admin_pass=:pass,user_enable_disable=:able where admin_name =:user");
    qr.bindValue(":user",user);
    qr.bindValue(":pass",pass);
    qr.bindValue(":able",able);
    if( !qr.exec() )
    qFatal( "Failed to update table images" );
    msgbox.setText("Admin Master Updated");
    msgbox.exec();
    ui->comboBox->setEditable(false);
    ui->label_3->hide();
    ui->label_4->hide();
    ui->label_5->hide();
    ui->lineEdit->hide();
    ui->lineEdit_2->hide();
    ui->comboBox_3->hide();
    ui->new_2->setEnabled(true);
    ui->delete_2->setEnabled(true);
    ui->edit->setEnabled(true);
    }
    else if(ch==true)
    {
    user=ui->nameline->text();
    qr.prepare("Select * from administrator where admin_name like :user");
    qr.bindValue(":user",user);
    if( !qr.exec() )
    qFatal( "Failed to update table images" );
    if (qr.next())
    {
    msgbox.setText("Admin Master Already Exists");
    msgbox.exec();
    ui->new_2->setEnabled(true);
    ui->delete_2->setEnabled(true);
    ui->edit->setEnabled(true);
    }
    }
    }
    else
    {
    qr.prepare ("insert into administrator(admin_name,admin_pass,auto_expiry_days,user_enable_disable,current_date,expired_date) values (:user, :pass, :expi, :able, :cur, :sqlexpir)") ;
    qr.bindValue(":admin_name", user);
    qr.bindValue(":admin_pass", pass);
    qr.bindValue(":auto_expiry_days", expi);
    qr.bindValue(":user_enable_disable", able);
    qr.bindValue(":current_date", cur);
    qr.bindValue(":expired_date", sqlexpir);
    qr.exec();
    msgbox.setText("Admin Master Created");
    msgbox.exec();
    ui->comboBox->setEditable(false);
    ui->comboBox_2->setEditable(false);
    ui->label_3->hide();
    ui->label_4->hide();
    ui->label_5->hide();
    ui->lineEdit->hide();
    ui->lineEdit_2->hide();
    ui->comboBox_3->hide();
    ui->new_2->setEnabled(true);
    ui->delete_2->setEnabled(true);
    ui->edit->setEnabled(true);
    name->setQuery("Select distinct(admin_name) from administrator");
    ui->comboBox->setModel(name);
    }
    }
    auexp=false;
    clearline();
    lineEditkeyboard->hide();
    lineEditkey->hide();
    }


  • Lifetime Qt Champion

    This is neither readable (missing code tags) nor minimal.... :(



  • @Magendiran-Thirunavukkarasu
    As @Christian-Ehrlicher has said, do you really expect people to look through this quantity of code and spot what is wrong? Have you put in any debugging messages to indicate where it does/does not get to under Qt5 compared to Qt4?



  • Hi, We have set debug messages and found that the entered,values are appearing before bind value as well after bindvalue. But nothing was written in to the database. writing anything in to the database. I am clueless & dont know how to proceed further. Request your help friends.


  • Lifetime Qt Champion

    @Magendiran-Thirunavukkarasu said in Qt5 Code Not Inserting values into SQL database:

    Request your help friends.

    Again: provide a minimal testcase!


  • Lifetime Qt Champion

    Beside the good point of @Christian-Ehrlicher, add proper checks to all your query execution


Log in to reply