Results 1 to 6 of 6

Thread: MySQL INSERT query fails in Qt

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    Join Date
    Dec 2009
    Location
    New Orleans, Louisiana
    Posts
    791
    Qt products
    Qt5
    Platforms
    MacOS X
    Thanks
    13
    Thanked 153 Times in 150 Posts

    Default Re: MySQL INSERT query fails in Qt

    Hi, QSqlQuery:repare, and QSqlQuery::exec return boolean results indicating success or failure. I see that you are only checking the result of the query.exec() call. Try examining the result of the prepare methods to ensure they are all successful.

    Good luck.

    Jeff

  2. #2
    Join Date
    Jun 2014
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: MySQL INSERT query fails in Qt

    Quote Originally Posted by jthomps View Post
    Hi, QSqlQuery:repare, and QSqlQuery::exec return boolean results indicating success or failure. I see that you are only checking the result of the query.exec() call. Try examining the result of the prepare methods to ensure they are all successful.

    Good luck.

    Jeff
    Hello. I have checked the return values of the QSqlQuery:repare as you recommended. The query is prepared successfully i.e. query.prepare() returns true.

  3. #3
    Join Date
    Jun 2014
    Posts
    4
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11 Windows

    Default Re: MySQL INSERT query fails in Qt

    After days of struggling I ve managed to solve the issue. It was a stupid mistake I had done in the code shown above and unfortunately quite difficult to detect since I was not getting any kind of error message, except from the INSERT query failing. I am posting the solution below:

    The problem was caused by line 65 of the code:
    Qt Code:
    1. query.bindValue(":screw_type_id",(int)current_tooth->getScrewTypeID()>0? current_tooth->getScrewTypeID():0);
    To copy to clipboard, switch view to plain text mode 
    and it is related to the column
    Qt Code:
    1. [int(11)]screws_idscrews,
    To copy to clipboard, switch view to plain text mode 
    of the SQL table. This column is a foreign key to another table. Therefore, it is automatically constraint not to take any values that will point to non-existent entries in the other table. As a result, the conditional statement above failed when it returned 0, since the foreign key cannot take the value 0 as it was setup. The correct value that the statement should return is NULL. To do this, line 65 was changed to this:
    Qt Code:
    1. query.bindValue(":screw_type_id",current_tooth->getScrewTypeID()>0? current_tooth->getScrewTypeID():QVariant(QVariant::Int));
    To copy to clipboard, switch view to plain text mode 
    This solved the problem.

    Simply using NULL as the 2nd argument to QSqlQuery::bindValue() does not work. According to the documentation of QSqlQuery::bindValue()
    To bind a NULL value, use a null QVariant
    .
    Here, the column screws_idscrews is of type INT so the following must be used to get a NULL INT:
    Qt Code:
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. How to know not insert or delete query?
    By ramazangirgin in forum Qt Programming
    Replies: 2
    Last Post: 29th June 2010, 13:43
  2. Select query using mysql for searching
    By sinha.ashish in forum Qt Programming
    Replies: 4
    Last Post: 10th April 2008, 06:14
  3. INSERT query with MySQL problem
    By timmyg in forum Qt Programming
    Replies: 10
    Last Post: 20th March 2008, 21:52
  4. Mysql query question
    By twells55555 in forum Qt Programming
    Replies: 1
    Last Post: 29th June 2007, 23:41
  5. MYSQL insert
    By allensr in forum Newbie
    Replies: 4
    Last Post: 14th August 2006, 16:55

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
  •  
Qt is a trademark of The Qt Company.