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.
Code:
void background::on_pushButton_3_clicked()
{
QString name, age,
/*gender, pregnant,*/ weight, height
/* family*/;
name=ui->lineEdit_name->text();
age=ui->lineEdit_age->text();
QString gender
=ui
->comboBox_gender
->currentText
();
QString pregnant
=ui
->comboBox_pregnant
->currentText
();
weight=ui->lineEdit_weight->text();
height=ui->lineEdit_height->text();
QString family
=ui
->comboBox_family
->currentText
();
myqry.prepare("INSERT INTO background(Name, Age, Gender, Pregnant, Weight, Height, Family) values('"+name+"','"+age+"','"+gender+"','"+pregnant+"','"+weight+"','"+height+"','"+family+"')");
if(myqry.exec()){
ui->labelstat->setText("The information has been saved");
// QMessageBox::information(this, "Save", "The information has been saved");
}
else{
ui->labelstat->setText("Information failed to saved");
// QMessageBox::warning(this, "Failed", "Information failed to saved");
}
}
Re: adding value from Combobox and Radiobutton into sqlite
What says myqry.lastError() ?
Re: adding value from Combobox and Radiobutton into sqlite
what do you mean by that ?
1 Attachment(s)
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.
Attachment 12242
Code:
void lifestyle::on_pushButton_3_clicked()
{
myqry.prepare("CREATE TABLE IF NOT EXISTS Lifestyle (Drinking VARCHAR(15), "
"Exercise VARCHAR(15), Smoking VARCHAR(10), Diet VARCHAR(10)) ");
if(!myqry.exec())
qDebug()<<myqry.lastError();
else
qDebug()<<"Table Created!";
QString drinking
= ui
->radioButton_1
->text
();
QString exercise
= ui
->radioButton_5
->text
();
QString smoking
= ui
->radioButton_7
->text
();
QString diet
= ui
->radioButton_8
->text
();
myqry.prepare("INSERT INTO Lifestyle(Drinking, Exercise, Smoking, Diet) "
"values('"+drinking+"','"+exercise+"','"+smoking+"','"+diet+"')");
if(myqry.exec()){
ui->labelstat->setText("The information has been saved");
}
else{
ui->labelstat->setText("Information failed to saved");
}
}
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.
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..