Results 1 to 6 of 6

Thread: More database problems

Hybrid View

Previous Post Previous Post   Next Post Next Post
  1. #1
    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: More database problems

    It is infinitely more likely that the database is not what you think it is. If "rider.dat" already exists then it will be opened, otherwise it is created. If it already existed and the table rider already exists then your CREATE TABLE will fail leaving whatever structure was previously there. Depending on the structure this could cause your subsequent INSERTs to also fail, leaving whatever data was in the table still in the table. Delete rider.dat before you try again.

    query.lastError() will return a QSqlError object that has some useful members. There's no point looking at it if the exec() indicated it succeeded though.
    Qt Code:
    1. void createDb()
    2. {
    3. QSqlQuery query;
    4. bool ok;
    5.  
    6. ok = query.exec("CREATE TABLE rider (id INTEGER PRIMARY KEY AUTOINCREMENT, "
    7. "LName TEXT, FName TEXT, weight REAL, notes TEXT)"); //Table creates properly with all the fields
    8. if (!ok) qWarn() << query.lastError().text();
    9. ok = query.exec("INSERT INTO rider (FName, LName, weight)"
    10. "VALUES ('Ryan', 'Villapoto',185)"); // Villapoto gets put into table as FName
    11. if (!ok) qWarn() << query.lastError().text();
    12. ok = query.exec("INSERT INTO rider (FName, LName, weight)"
    13. "VALUES ('Jeremy', 'McGrath',155)"); // Nothing gets added
    14. if (!ok) qWarn() << query.lastError().text();
    15. }
    To copy to clipboard, switch view to plain text mode 

  2. #2
    Join Date
    Aug 2010
    Posts
    107
    Thanks
    5
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: More database problems

    I have been out of town for the last week. I tried your code and got a "qWarn was not declared in this scope. I included <QSqlError>. Where do I need to declare qWarn. I did get my SQL to work finally! I thank you for your help!

  3. #3
    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: More database problems

    Qt Code:
    1. #include <QDebug>
    To copy to clipboard, switch view to plain text mode 

Similar Threads

  1. Replies: 9
    Last Post: 20th May 2010, 09:55
  2. Replies: 2
    Last Post: 15th April 2010, 14:59
  3. Simple DataBase problems
    By briang in forum Newbie
    Replies: 2
    Last Post: 31st December 2009, 09:42
  4. SQLITE database problems
    By phoenix in forum Newbie
    Replies: 3
    Last Post: 30th April 2007, 21:38

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.