Results 1 to 7 of 7

Thread: QSqlQuery::exec() returns false all the time

  1. #1
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default QSqlQuery::exec() returns false all the time

    hi there, the function exec() in the code below returns false all the time and I've tried everything to fix it but without any success. Could I be missing something here. please help.
    Qt Code:
    1. void MainWindow::query(QStringList qry)
    2. {
    3. QString qryStr = QString("INSERT INTO Modules VALUES(\"%1, %2 %3 %4\")").arg(qry.at(0)).arg(qry.at(1)).arg(qry.at(2)).arg(qry.at(3));
    4. QSqlQuery query;
    5. query.prepare(qryStr);
    6.  
    7. if(query.exec())
    8. QMessageBox::critical(0, "DB Status","DATABASE UPDATED", QMessageBox::Ok);
    9. else
    10. QMessageBox::critical(0, "DB Status","DATABASE NOT UPDATED", QMessageBox::Ok);
    11. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery::exec() returns false all the time

    Have u add database connection to sqlQuery?

  3. #3
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery::exec() returns false all the time

    I'm not sure if i know what you mean by "adding a database connection to SqlQuery" but i am going to tell you what I have done. I have pointed my program to the database and I have confirmed that the program is able to open the database and make changes to it, but this particular code I posted above is not doing what it should.

  4. #4
    Join Date
    Jan 2012
    Location
    Iran, Tehran
    Posts
    308
    Thanks
    75
    Thanked 24 Times in 21 Posts
    Qt products
    Qt4 Qt5 PyQt3 PyQt4
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery::exec() returns false all the time

    please show output of QSqlQuery::lastError()->text()

  5. #5
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery::exec() returns false all the time

    Never mind, I have fixed the problem. For those who might encounter the same problem is future, the problem was that i added double quotation marks inside the brackets instead of single quotation marks on this line of code QString qryStr = QString("INSERT INTO Modules VALUES(\"%1, %2 %3 %4\")").arg(qry.at(0)).arg(qry.at(1)).arg(qry.at(2 )).arg(qry.at(3)); the correct line is QString qryStr = QString("INSERT INTO Modules VALUES('%1', '%2', '%3', '%4')").arg(qry.at(0)).arg(qry.at(1)).arg(qry.at(2 )).arg(qry.at(3));

  6. #6
    Join Date
    Apr 2008
    Posts
    45
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt3 Qt4 Qt/Embedded
    Platforms
    Unix/X11 Windows

    Default Re: QSqlQuery::exec() returns false all the time

    DO NOT DO WHAT YOU ARE DOING. You have multiple bad practices in play.
    1. Always provide the fields for insert. What happens when the table gets altered and the column order changes?
    2. It is VERY bad form to construct SQL as you are going. See this as for why. This is why/how people get online databases hacked.

    ALWAYS ALWAYS ALWAYS use the bindValue interface.


    Qt Code:
    1. void MainWindow::query(QStringList qry)
    2. {
    3. QSqlQuery query;
    4. query.prepare("INSERT INTO Modules (A,B,C,D) VALUES(:a, :b, :c, d)");
    5.  
    6. query.bindValue(":a", qry[0]);
    7. query.bindValue(":b", qry[1]);
    8. query.bindValue(":c", qry[2]);
    9. query.bindValue(":d", qry[3]);
    10.  
    11. if(query.exec())
    12. QMessageBox::critical(0, "DB Status","DATABASE UPDATED", QMessageBox::Ok);
    13. else
    14. QMessageBox::critical(0, "DB Status","DATABASE NOT UPDATED", QMessageBox::Ok);
    15. }
    To copy to clipboard, switch view to plain text mode 

    The bind interface:
    1. prevents exploits, by performing the proper escaping in a database-engine specific manner.
    2. is more potable between databases
    3. does not use that ugly string of embedded quotes

    The only time is it ok to not use it, is when you are sure the content is not ever user supplied.

    If "Robert'); drop table students" is used in your query, what happens? your query fails, and if you have a students table, it sets dropped. Using bind parameters, you will get a field value of "Robert'); drop table students". And YES, I have seen this on systems with software I've written. (it wasn't Qt, but PHP at the time)

  7. The following user says thank you to Scorp2us for this useful post:

    ayanda83 (1st March 2014)

  8. #7
    Join Date
    Jul 2012
    Posts
    201
    Thanks
    26
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlQuery::exec() returns false all the time

    thank you Scorp2us. good advise.

Similar Threads

  1. database open returns false
    By ahmetturan in forum Newbie
    Replies: 2
    Last Post: 22nd January 2014, 08:24
  2. Replies: 1
    Last Post: 18th July 2011, 12:12
  3. QSslSocke::supportSsl() returns false
    By oscar in forum Qt Programming
    Replies: 1
    Last Post: 9th September 2008, 18:51
  4. QSqlQuery::isValid returns false
    By mismael85 in forum Qt Programming
    Replies: 24
    Last Post: 7th September 2008, 23:43
  5. connect returns false
    By krivenok in forum Qt Programming
    Replies: 6
    Last Post: 21st February 2006, 20:01

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.