Results 1 to 3 of 3

Thread: duplicate field value in query.

  1. #1
    Join Date
    Jun 2014
    Posts
    47
    Thanks
    6
    Qt products
    Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default duplicate field value in query.

    i am building a small project having following slot :-
    Qt Code:
    1. void DialogMemberAdd::query(QString *qry, int flag)
    2. {
    3. QString sql = *qry;
    4. QString str;
    5. QSqlQuery qry2;
    6. qry2.prepare(sql);
    7.  
    8. if(ui->lineEditName->text().isEmpty())
    9. {
    10. //str = "NULL";
    11. return;
    12. }
    13. else
    14. {
    15. str = ui->lineEditName->text().trimmed();
    16. }
    17. qry2.bindValue(0, str) ;
    18.  
    19. int cardno = 0;
    20. if(ui->lineEditCardNumber->text().trimmed().isEmpty())
    21. {
    22. cardno = 0;
    23. }
    24. else
    25. {
    26. cardno = ui->lineEditCardNumber->text().trimmed().toInt();
    27. }
    28. qry2.bindValue(1, cardno);
    29.  
    30. QString addr;
    31. if(ui->lineEditAddress->text().trimmed().isEmpty())
    32. {
    33. addr = "";
    34. }
    35. else
    36. {
    37. addr = ui->lineEditAddress->text().trimmed();
    38. }
    39. qry2.bindValue(2, addr);
    40.  
    41. long monum = 0;
    42. if(ui->lineEditMobileNumber->text().trimmed().isEmpty())
    43. {
    44. monum = 0;
    45. }
    46. else
    47. {
    48. monum = ui->lineEditMobileNumber->text().trimmed().toLong();
    49. }
    50. qry2.bindValue(3, monum);
    51.  
    52. float amount;
    53. if(ui->lineEditAmopuntDeposited->text().trimmed().isEmpty())
    54. {
    55. amount = 0.0;
    56. }
    57. else
    58. {
    59. amount = ui->lineEditAmopuntDeposited->text().trimmed().toFloat();
    60. }
    61. qry2.bindValue(4, amount);
    62. long long memno;
    63. if(ui->lineEditMembershipNumber->text().trimmed().isEmpty())
    64. {
    65. memno = 0;
    66. }
    67. else
    68. {
    69. memno = ui->lineEditMembershipNumber->text().trimmed().toInt();
    70. }
    71.  
    72. qry2.bindValue(5, memno);
    73.  
    74. if(qry2.exec())
    75. {
    76. if(flag == 1)
    77. {
    78. QMessageBox::information(this, "insert_into member records ", "Records inserted successfully");
    79. }
    80. else if(flag == 2)
    81. {
    82. QMessageBox::information(this, "insert_into member records ", "Records updated successfully");
    83. }
    84. }
    85. else
    86. {
    87. QMessageBox::critical(this,"insert_into member records ", QString("error = ").append(qry2.lastError().text()) );
    88. }
    89. }
    To copy to clipboard, switch view to plain text mode 
    and signal is :-
    Qt Code:
    1. void query_sql(QString *sql, int flags);
    To copy to clipboard, switch view to plain text mode 
    here i send this signal as follows :-
    Qt Code:
    1. void DialogMemberAdd::on_pushButtonModifyRecord_clicked()
    2. {
    3. QString sql = "update tableMemberRecords set name = ?, cardno = ?, address = ?, mobileno = ?, amountdeposited = ?, membershipno = ?";
    4. flag = 2;
    5. emit query_sql(&sql, flag);
    6. }
    To copy to clipboard, switch view to plain text mode 
    when running and clicking on pushbutton it shows following error :-
    Qt Code:
    1. "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.

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

    Default 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.

  3. The following user says thank you to Lesiok for this useful post:

    rahulvishwakarma (8th April 2020)

  4. #3
    Join Date
    Jan 2008
    Location
    Alameda, CA, USA
    Posts
    5,229
    Thanks
    302
    Thanked 864 Times in 851 Posts
    Qt products
    Qt5
    Platforms
    Windows

    Default 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.
    <=== The Great Pumpkin says ===>
    Please use CODE tags when posting source code so it is more readable. Click "Go Advanced" and then the "#" icon to insert the tags. Paste your code between them.

Similar Threads

  1. tabWidget duplicate frames
    By ilmandorlone in forum Qt Programming
    Replies: 9
    Last Post: 16th March 2016, 11:33
  2. Replies: 7
    Last Post: 16th April 2015, 18:11
  3. duplicate recordset
    By sepehr in forum Qt Programming
    Replies: 2
    Last Post: 4th February 2009, 11:44
  4. Duplicate SLOTS
    By ^NyAw^ in forum Qt Programming
    Replies: 1
    Last Post: 26th February 2008, 13:32

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.