duplicate field value in query.
i am building a small project having following slot :-
Code:
void DialogMemberAdd
::query(QString *qry,
int flag
) {
qry2.prepare(sql);
if(ui->lineEditName->text().isEmpty())
{
//str = "NULL";
return;
}
else
{
str = ui->lineEditName->text().trimmed();
}
qry2.bindValue(0, str) ;
int cardno = 0;
if(ui->lineEditCardNumber->text().trimmed().isEmpty())
{
cardno = 0;
}
else
{
cardno = ui->lineEditCardNumber->text().trimmed().toInt();
}
qry2.bindValue(1, cardno);
if(ui->lineEditAddress->text().trimmed().isEmpty())
{
addr = "";
}
else
{
addr = ui->lineEditAddress->text().trimmed();
}
qry2.bindValue(2, addr);
long monum = 0;
if(ui->lineEditMobileNumber->text().trimmed().isEmpty())
{
monum = 0;
}
else
{
monum = ui->lineEditMobileNumber->text().trimmed().toLong();
}
qry2.bindValue(3, monum);
float amount;
if(ui->lineEditAmopuntDeposited->text().trimmed().isEmpty())
{
amount = 0.0;
}
else
{
amount = ui->lineEditAmopuntDeposited->text().trimmed().toFloat();
}
qry2.bindValue(4, amount);
long long memno;
if(ui->lineEditMembershipNumber->text().trimmed().isEmpty())
{
memno = 0;
}
else
{
memno = ui->lineEditMembershipNumber->text().trimmed().toInt();
}
qry2.bindValue(5, memno);
if(qry2.exec())
{
if(flag == 1)
{
QMessageBox::information(this,
"insert_into member records ",
"Records inserted successfully");
}
else if(flag == 2)
{
QMessageBox::information(this,
"insert_into member records ",
"Records updated successfully");
}
}
else
{
QMessageBox::critical(this,
"insert_into member records ",
QString("error = ").
append(qry2.
lastError().
text()) );
}
}
and signal is :-
Code:
void query_sql
(QString *sql,
int flags
);
here i send this signal as follows :-
Code:
void DialogMemberAdd::on_pushButtonModifyRecord_clicked()
{
QString sql
= "update tableMemberRecords set name = ?, cardno = ?, address = ?, mobileno = ?, amountdeposited = ?, membershipno = ?";
flag = 2;
emit query_sql(&sql, flag);
}
when running and clicking on pushbutton it shows following error :-
Code:
"Duplicate entry '9630612822' for key 'MobileNo_UNIQUE' QMYSQL3: Unable to execute statement."
even it is already having that entry in database.How to get rid of this duplicate entry.
Re: duplicate field value in query.
Yours UPDATE query updates ALL record in database and the message shows that there can only be one record for a given number in the database. The WHERE condition is probably missing.
Re: duplicate field value in query.
And sending a pointer to a local variable as a signal argument is a very bad programming practice. If a slot tries to store that pointer, it will result in a crash later because after the clicked() slot exits, the local variable is gone.
Why don't you simply send the QString itself? You aren't optimizing anything by sending the address of the QString, because the QString implementation will not copy the string until it is modified, so it will not be copied if it is sent as an argument.