Results 1 to 4 of 4

Thread: QSqlQuery problem.

  1. #1
    Join Date
    Jul 2011
    Posts
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default QSqlQuery problem.

    Hello :

    I have the following code which connects to the Mysql database and attempts to do an insert.
    Qt Code:
    1. #include <QApplication>
    2. #include <QtSql/qsqldatabase>
    3. #include <QtSql/qsqlquery>
    4. # include <QMessageBox>
    5. # include <QString>
    6. # include <QVariant>
    7.  
    8. class Person
    9. {
    10. public:
    11. QString *fname;
    12. QString *lname;
    13. QString *mobile;
    14. Person()
    15. {
    16. this->fname = new QString();
    17. this->lname = new QString();
    18. this->mobile = new QString();
    19. };
    20. };
    21.  
    22.  
    23.  
    24.  
    25. int main(int argc, char *argv[])
    26. {
    27. QApplication a(argc, argv);
    28.  
    29. QSqlDatabase db = QSqlDatabase::addDatabase("QMYSQL");
    30. db.setHostName("localhost");
    31. db.setDatabaseName("demo");
    32. db.setUserName("root");
    33. db.setPassword("root");
    34. if(!db.open())
    35. {
    36. m->setText("Failure Connecting to Database");
    37. m->show();
    38. }
    39. else
    40. {
    41. m->setText("Successful connection");
    42. m->show();
    43. }
    44.  
    45. Person *p = new Person();
    46. p->fname->append("John");
    47. p->lname->append("Smith");
    48. p->mobile->append("9980324083");
    49.  
    50. QSqlQuery mQuery(db);
    51. mQuery.prepare("INSERT INTO PERSON (id,first_name,last_name,phone) VALUES (?,?,?,?)");
    52. mQuery.addBindValue(QVariant("NULL"));
    53. mQuery.addBindValue(QVariant(*(p->fname))) ;
    54. mQuery.addBindValue(QVariant(*(p->lname)));
    55. mQuery.addBindValue(QVariant(*(p->mobile)));
    56. mQuery.exec();
    57.  
    58. return a.exec();
    59. }
    To copy to clipboard, switch view to plain text mode 

    It corresponds to the following schema in mysql database:
    Qt Code:
    1. CREATE DATABASE DEMO
    2. CREATE TABLE PERSON
    3. ( id int NOT NULL PRIMARY KEY AUTO_INCREMENT,
    4. first_name VARCHAR(255),
    5. last_name VARCHAR(255),
    6. mobile_phone VARCHAR(255) );
    To copy to clipboard, switch view to plain text mode 
    The code compiles, links and runs fine (along with showing a successfull connection to the database) but I dont see the inserted values in the database.
    So for example when I go to the command line interface and type the following at the mysql command prompt:

    Qt Code:
    1. use demo;
    2. select * from person;
    To copy to clipboard, switch view to plain text mode 

    I get an empty set.

    What am I doing wrong?

  2. #2
    Join Date
    Feb 2008
    Posts
    491
    Thanks
    12
    Thanked 142 Times in 135 Posts
    Qt products
    Qt4 Qt5
    Platforms
    Unix/X11

    Default Re: QSqlQuery problem.

    Put a qDebug() statement in your code after the prepare statement to check for errors:
    Qt Code:
    1. qDebug() << mQuery.lastError();
    To copy to clipboard, switch view to plain text mode 
    Or check your field names.
    Last edited by norobro; 18th October 2011 at 15:06.

  3. #3
    Join Date
    Feb 2011
    Location
    Romania
    Posts
    53
    Thanks
    1
    Thanked 11 Times in 9 Posts
    Qt products
    Qt4
    Platforms
    MacOS X Unix/X11 Windows

    Default Re: QSqlQuery problem.

    Qt Code:
    1. mQuery.prepare("INSERT INTO PERSON (first_name, last_name, mobile_phone) VALUES (?,?,?)");
    To copy to clipboard, switch view to plain text mode 

  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: QSqlQuery problem.

    More than likely your query is failing because you try to insert NULL into a not null id column but it could be any number of other things. Check the return value of the exec() call and if it is false look at lastError().

    You could also save yourself a bunch of awkward syntax and memory leaks (Person has no destructor releasing resources) by storing actual QStrings in Person and not pointers.

Similar Threads

  1. Replies: 1
    Last Post: 18th July 2011, 12:12
  2. MySQL QSqlQuery problem
    By Erni35 in forum Qt Programming
    Replies: 2
    Last Post: 15th September 2010, 22:14
  3. QSqlQuery problem
    By MarkoSan in forum Qt Programming
    Replies: 14
    Last Post: 14th January 2008, 22:50
  4. QSqlQuery problem
    By MarkoSan in forum Qt Programming
    Replies: 11
    Last Post: 18th December 2007, 13:25
  5. QSqlQuery problem
    By dragon in forum Newbie
    Replies: 5
    Last Post: 22nd December 2006, 17: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.