Results 1 to 4 of 4

Thread: How to deal with foreign key in Qt

  1. #1
    Join Date
    May 2012
    Posts
    2
    Qt products
    Qt4

    Default How to deal with foreign key in Qt

    Qt Code:
    1. else
    2. {
    3. query->prepare ("insert into Courses values (?,?,?)");
    4. query->bindValue (0,courseIDLine->text ());
    5. query->bindValue (1,courseNameLine->text ());
    6. query->bindValue (2,courseTeacherIDLine->text ());
    7.  
    8. successful=query->exec ();
    9.  
    10. }
    11.  
    12. if(successful)
    13. {
    14. closeInsertCourseDlg ();
    15. }
    16. else if(!errorMsg->isVisible ())
    17. {
    18. //cannot execute the statement
    19. }
    To copy to clipboard, switch view to plain text mode 

    but if the SQL statement doesn't satisfy the foreign key, the program will end unexpectedly, it will not just return a false

    how to change the code??

  2. #2
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to deal with foreign key in Qt

    You change the code is a way that fixes whatever you have done that causes it to crash. You have not told us where it crashes or what you have done to isolate it. Run the code in your debugger and read the backtrace to find the source of the problem in your code.

    Qt will return false if the insert fails: it does not crash.

    If I assume for a moment that your code crashes somewhere in what you have presented then it will be at line 16 where the pointer errorMsg is invalid or null. This code will only be executed if the insert fails.

  3. #3
    Join Date
    May 2012
    Posts
    2
    Qt products
    Qt4

    Default Re: How to deal with foreign key in Qt

    Thanks a lot. It is the use of the null pointer that leads to the crash.
    I didn't discover that..


    Added after 36 minutes:


    I got another question.

    Qt Code:
    1. QString id=delLine->text();
    2. bool successful=false;
    3. if(studentRadio->isChecked()){
    4. successful=query->exec("delete from Students where StudentID="+id);
    5. }
    6. else if(teacherRadio->isChecked ()){
    7. successful=query->exec("delete from Teachers where TeacherID="+id);
    8. }else if(courseRadio->isChecked ()){
    9. successful=query->exec ("delete from Courses where CourseID="+id);
    10. }
    11. if(successful){
    12. qDebug ()<<query->lastError ().text (); //the output is " "
    13. delDlg->close();
    14. }
    15. else{
    16. //error
    17. }
    To copy to clipboard, switch view to plain text mode 

    if the id doesn't exist in the table, the value of successful will still be true.
    how to detect if the id doesn't exist ?


    Added after 6 minutes:


    should I select first , and if query->record ().count ()!=0, delete it; otherwise set the successful false?
    It seems works
    Last edited by sjyzhxw; 25th May 2012 at 06:49.

  4. #4
    Join Date
    Mar 2009
    Location
    Brisbane, Australia
    Posts
    7,729
    Thanks
    13
    Thanked 1,610 Times in 1,537 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows
    Wiki edits
    17

    Default Re: How to deal with foreign key in Qt

    If the aim is that the row does not exist afterward does it matter that it did not exist beforehand? If you need an optimistic offline lock or similar pattern to handle concurrent updates then that is more involved.

    Anyway, depending on the database, QSqlQuery::numRowsAffected() may tell you how many rows you actually deleted.

Similar Threads

  1. Replies: 1
    Last Post: 8th August 2011, 14:46
  2. How to deal with application-wide actions?
    By grayfox in forum Qt Programming
    Replies: 1
    Last Post: 25th July 2011, 11:15
  3. Replies: 5
    Last Post: 31st December 2010, 11:49
  4. How to deal with shorcouts in many widgets
    By kazibi in forum Qt Programming
    Replies: 1
    Last Post: 29th January 2010, 09:58
  5. Replies: 12
    Last Post: 23rd March 2007, 09:23

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.