Results 1 to 6 of 6

Thread: adding value from Combobox and Radiobutton into sqlite

  1. #1
    Join Date
    Nov 2016
    Posts
    3
    Qt products
    Qt/Embedded
    Platforms
    Windows

    Default adding value from Combobox and Radiobutton into sqlite

    Hi,
    I am new to the programming world,
    I have trouble in finding a way to put the value of the Combobox into my database.
    here's my coding. it is miserable.

    Qt Code:
    1. void background::on_pushButton_3_clicked()
    2. {
    3.  
    4. QString name, age,/*gender, pregnant,*/ weight, height/* family*/;
    5. name=ui->lineEdit_name->text();
    6. age=ui->lineEdit_age->text();
    7. QString gender=ui->comboBox_gender->currentText();
    8. QString pregnant=ui->comboBox_pregnant->currentText();
    9. weight=ui->lineEdit_weight->text();
    10. height=ui->lineEdit_height->text();
    11. QString family=ui->comboBox_family->currentText();
    12.  
    13. QSqlQuery myqry;
    14. myqry.prepare("INSERT INTO background(Name, Age, Gender, Pregnant, Weight, Height, Family) values('"+name+"','"+age+"','"+gender+"','"+pregnant+"','"+weight+"','"+height+"','"+family+"')");
    15.  
    16. if(myqry.exec()){
    17. ui->labelstat->setText("The information has been saved");
    18. // QMessageBox::information(this, "Save", "The information has been saved");
    19. }
    20. else{
    21. ui->labelstat->setText("Information failed to saved");
    22. // QMessageBox::warning(this, "Failed", "Information failed to saved");
    23.  
    24. }
    25. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,459
    Thanked 271 Times in 266 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: adding value from Combobox and Radiobutton into sqlite

    What says myqry.lastError() ?

  3. #3
    Join Date
    Nov 2016
    Posts
    3
    Qt products
    Qt/Embedded
    Platforms
    Windows

    Default Re: adding value from Combobox and Radiobutton into sqlite

    what do you mean by that ?

  4. #4
    Join Date
    Nov 2016
    Posts
    3
    Qt products
    Qt/Embedded
    Platforms
    Windows

    Default How do I insert radiobutton values in Sqlite database?

    I am very new to programming.
    I wanted to insert the values of my radiobutton into sqlite database but still doesn't found the right way to do it.
    The program consist 9 radiobutton, which is 1-3,4-5,6-7 and 8-9 in 4 different grouping/layout.
    Thank you.

    test.PNG

    Qt Code:
    1. void lifestyle::on_pushButton_3_clicked()
    2. {
    3. QSqlQuery myqry;
    4.  
    5. myqry.prepare("CREATE TABLE IF NOT EXISTS Lifestyle (Drinking VARCHAR(15), "
    6. "Exercise VARCHAR(15), Smoking VARCHAR(10), Diet VARCHAR(10)) ");
    7. if(!myqry.exec())
    8. qDebug()<<myqry.lastError();
    9. else
    10. qDebug()<<"Table Created!";
    11.  
    12. QString drinking= ui->radioButton_1->text();
    13. QString exercise = ui->radioButton_5->text();
    14. QString smoking = ui->radioButton_7->text();
    15. QString diet = ui->radioButton_8->text();
    16.  
    17. myqry.prepare("INSERT INTO Lifestyle(Drinking, Exercise, Smoking, Diet) "
    18. "values('"+drinking+"','"+exercise+"','"+smoking+"','"+diet+"')");
    19.  
    20. if(myqry.exec()){
    21. ui->labelstat->setText("The information has been saved");
    22.  
    23. }
    24. else{
    25. ui->labelstat->setText("Information failed to saved");
    26.  
    27. }
    28. }
    To copy to clipboard, switch view to plain text mode 

  5. #5
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Thanks
    13
    Thanked 153 Times in 150 Posts
    Qt products
    Qt5
    Platforms
    MacOS X

    Default Re: How do I insert radiobutton values in Sqlite database?

    Do you want the text of the button or the checked state of the button? Your example shows that you're getting the text of the button, which seems odd to me. Even if you do want the button text, saving that w/o saving the checked state of the button would be a UI issue as far as I'm concerned.

    You should also not use the string concatenation operator (+) to dynamically build your SQL statements. You should QSqlQuery::prepare() the statement then use one of the QSqlQuery::bindValue() methods (positional or named) to associate the values with the prepared statement. This prevents your app from being susceptible from SQL injections and is a best practice you should follow.
    I write the best type of code possible, code that I want to write, not code that someone tells me to write!

  6. #6
    Join Date
    Mar 2008
    Location
    Kraków, Poland
    Posts
    1,459
    Thanked 271 Times in 266 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: adding value from Combobox and Radiobutton into sqlite

    You never check the cause of the error when writing to the database fails.
    QSqlQuery::lastError() doc says : Returns error information about the last error (if any) that occurred with this query..

Similar Threads

  1. Replies: 5
    Last Post: 2nd February 2015, 20:31
  2. Replies: 3
    Last Post: 15th December 2014, 16:24
  3. Replies: 2
    Last Post: 8th April 2013, 06:40
  4. Replies: 1
    Last Post: 4th May 2011, 07:00
  5. adding image into a databse using sqlite
    By pankaj.1426 in forum Qt for Embedded and Mobile
    Replies: 0
    Last Post: 20th October 2010, 10:08

Tags for this Thread

Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.