i am building a small project having following slot :-
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()) );
}
}
void DialogMemberAdd::query(QString *qry, int flag)
{
QString sql = *qry;
QString str;
QSqlQuery qry2;
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);
QString addr;
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()) );
}
}
To copy to clipboard, switch view to plain text mode
and signal is :-
void query_sql
(QString *sql,
int flags
);
void query_sql(QString *sql, int flags);
To copy to clipboard, switch view to plain text mode
here i send this signal as follows :-
void DialogMemberAdd::on_pushButtonModifyRecord_clicked()
{
QString sql
= "update tableMemberRecords set name = ?, cardno = ?, address = ?, mobileno = ?, amountdeposited = ?, membershipno = ?";
flag = 2;
emit query_sql(&sql, flag);
}
void DialogMemberAdd::on_pushButtonModifyRecord_clicked()
{
QString sql = "update tableMemberRecords set name = ?, cardno = ?, address = ?, mobileno = ?, amountdeposited = ?, membershipno = ?";
flag = 2;
emit query_sql(&sql, flag);
}
To copy to clipboard, switch view to plain text mode
when running and clicking on pushbutton it shows following error :-
"Duplicate entry '9630612822' for key 'MobileNo_UNIQUE' QMYSQL3: Unable to execute statement."
"Duplicate entry '9630612822' for key 'MobileNo_UNIQUE' QMYSQL3: Unable to execute statement."
To copy to clipboard, switch view to plain text mode
even it is already having that entry in database.How to get rid of this duplicate entry.
Bookmarks