Results 1 to 6 of 6

Thread: More database problems

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

    Angry More database problems

    Things are getting closer, but for some reason my SQL statments are not working properly. I am using SQLite. Not all of the data is getting put into the table.

    code to create table:
    Qt Code:
    1. void createDb()
    2. {
    3. QSqlQuery query;
    4.  
    5. query.exec("CREATE TABLE rider (id INTEGER PRIMARY KEY AUTOINCREMENT, "
    6. "LName TEXT, FName TEXT, weight REAL, notes TEXT)"); //Table creates properly with all the fields
    7. query.exec("INSERT INTO rider (FName, LName, weight)"
    8. "VALUES ('Ryan', 'Villapoto',185)"); // Villapoto gets put into table as FName
    9. query.exec("INSERT INTO rider (FName, LName, weight)"
    10. "VALUES ('Jeremy', 'McGrath',155)"); // Nothing gets added
    11. }
    To copy to clipboard, switch view to plain text mode 
    I am sure it is something simple....but I can't find it

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

    The code posted above functions and produces a two row table for me.

    Line 7: If Villapoto ends up in the FName column with this query then either something is very broken with your Sqlite engine or plugin, you are not looking at the same database as the program, or you mistyped your comment.
    Line 9: Does the query return true or false (You really should get into the habit of looking at the return value). If false, then what does QSqlQuery::lastError() give you?

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

    Default Re: More database problems

    here is the connection...I am sure the connection is working because I have the database created from the program and if the SQLite engine is broken (I am sure it came with Creator) how do I fix it?? I cut and pasted the code so no mistyping.
    I did use qDebug() << query.lastError(); and it returned QSqlError(-1, "", "") .
    Qt Code:
    1. bool createConnection()
    2. {
    3. QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
    4. db.setDatabaseName("rider.dat");
    5. if (!db.open()) {
    6. QMessageBox::warning(0, QObject::tr("Database Error"),
    7. db.lastError().text());
    8. return false;
    9. }
    10. return true;
    11. }
    To copy to clipboard, switch view to plain text mode 
    I am not sure how to use QSqlQuery::lastError() I used qDebug() <<QSqlQuery::lastError(); and got an error...I used qDebug() <<QSqlQuery::lastError(query); and it still didn't work. I did include <QSqlError>.
    Thanks for your help!

  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: 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 

  5. #5
    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!

  6. #6
    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
  •  
Digia, Qt and their respective logos are trademarks of Digia Plc in Finland and/or other countries worldwide.