Results 1 to 5 of 5

Thread: SQLite misbehaving

  1. #1
    Join Date
    Feb 2007
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default SQLite misbehaving

    I have upgraded to SQLite 3.3.13. I don't know if that is a problem or not, because Qt came with 3.2.7.

    But my problem is I run a simple query:
    Qt Code:
    1. QString sql;
    2. QSqlQuery query1(db);
    3.  
    4. sql="CREATE TABLE IF NOT EXISTS patients ("
    5. "dbID integer PRIMARY KEY AUTOINCREMENT,"
    6. "identificationNumber text,"
    7. "lastname text,"
    8. "firstname text,"
    9. "mi text);";
    10.  
    11. if (!query1.exec(sql)) {
    12. QMessageBox::critical(0, "", "Patients:" + query1.lastError().text());
    13. //return false;
    14. }
    To copy to clipboard, switch view to plain text mode 
    at application start up, then at user election, I do
    Qt Code:
    1. QSqlQuery query(db);
    2. query.prepare("INSERT INTO patients (lastname, firstname, mi, identificationNumber) "
    3. "VALUES (:lastname, :firstname, :mi, :identificationnumber);");
    4. query.bindValue(":lastname", pr.lastName);
    5. query.bindValue(":firstname", pr.firstName);
    6. query.bindValue(":mi", pr.MI);
    7. query.bindValue(":identificationnumber", pr.identificationNumber);
    8. if (!query.exec()) {
    9. QMessageBox::critical(0, "", "Patient add failed! " + query.lastError().text());
    10. return;
    11. }
    To copy to clipboard, switch view to plain text mode 

    And it tells me that database is locked.

    I also noticed that the application SEEMS to hold a lock on the database, so the application has to be exited before the tables are created... I've done ODBC with Qt with no problems. Anyone have an idea on SQLite? is ti a driver setting?

    EDIT: Added db.transaction() and db.commit(), still no joy.
    Last edited by Scorp1us; 22nd February 2007 at 23:42.

  2. #2
    Join Date
    Feb 2007
    Posts
    10
    Thanks
    1
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: SQLite misbehaving

    It seems that:
    Qt Code:
    1. QSqlQuery q(db);
    To copy to clipboard, switch view to plain text mode 

    is not right. It is better to leave off the
    (db)
    -- that's what got my program working this far.

    Ok, I've done some experimenting, I have everything working except one feature: the database does not save... Here's what I mean. I can run my program it seems to create the schemas and I can insert and query data. But when I exit, it all gets lost. When I run it again it is as if it was never written to in the first place...

    Any ideas?
    My database file DOES get created, and there is even a journal. The commandline sqlite3 that I have works just fine.

    I've upgraded to 4.2.2, and tried that sqlite verion (3.3.6) and its all the same.

  3. #3
    Join Date
    Jan 2006
    Posts
    128
    Thanked 28 Times in 27 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite misbehaving

    When I had problem with SQLite telling me that it thought the database/table was locked, it was mostly always a problem with queries that overstayed their welcome.

    My tip is, either let the query run out of scope when you do not need it anymore, or insert this little line after every SELECT (where you do not loop through ".next()" anyways)
    Qt Code:
    1. while (query.next()) {}
    To copy to clipboard, switch view to plain text mode 
    The reason is that the QtSql/Sqlite combination frees its internal result block only after the last next() has been reached.


    But could you please post the complete error message?


    Now for the not saving:
    Are you sure you do not have a transaction started somewhere that never is commited? I must say I never had that kind of problem...

  4. #4
    Join Date
    Jan 2006
    Location
    Frankfurt, Germany
    Posts
    34
    Thanks
    3
    Thanked 2 Times in 2 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11

    Default Re: SQLite misbehaving

    Probably this isn't the cause of your problem, but.. why are you using your own id field? SQLite provides rowid which does autoincrement automatically. I read a page from the SQLite developers, where they discourage use of AUTO_INCREMENT.

  5. #5
    Join Date
    Jan 2006
    Posts
    75
    Thanks
    8
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: SQLite misbehaving

    Are you sure that it is lost? Using the command line and browsing the table shows no entries?

Similar Threads

  1. SQLite driver unavailable
    By kandalf in forum Installation and Deployment
    Replies: 5
    Last Post: 11th February 2009, 16:36
  2. Bulk insert into SQLite
    By munna in forum Qt Programming
    Replies: 6
    Last Post: 19th November 2007, 03:56
  3. sqlite
    By spx2 in forum Qt Programming
    Replies: 9
    Last Post: 19th December 2006, 22:01
  4. Reading umlaut letters from sqlite database
    By Djony in forum Qt Programming
    Replies: 11
    Last Post: 17th November 2006, 10:30
  5. Sqlite & QPixmap Help please
    By munna in forum Qt Programming
    Replies: 2
    Last Post: 4th November 2006, 05:35

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.