PDA

View Full Version : adding value from Combobox and Radiobutton into sqlite



aniqbear
4th December 2016, 11:35
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.



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();

QSqlQuery myqry;
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");

}
}

Lesiok
4th December 2016, 14:39
What says myqry.lastError() ?

aniqbear
7th December 2016, 21:56
what do you mean by that ?

aniqbear
7th December 2016, 23:14
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.

12242


void lifestyle::on_pushButton_3_clicked()
{
QSqlQuery myqry;

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");

}
}

jefftee
8th December 2016, 00:33
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.

Lesiok
8th December 2016, 08:11
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..