Results 1 to 7 of 7

Thread: sqlite read lock.

  1. #1
    Join Date
    Jul 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default sqlite read lock.

    When I execute the following code:

    Qt Code:
    1. QString username = "gilgm";
    2. QSqlQuery * userQuery = new QSqlQuery("DELETE FROM users WHERE username=?");
    3. userQuery->addBindValue(username);
    4. int ret = userQuery->exec();
    5. if( !ret ) qDebug() << userQuery->lastError();
    6. return ret
    To copy to clipboard, switch view to plain text mode 

    It reports no error, and returns without problems. The copy of my database on disk doesn't get updated, and a "sqlite-journal" file is created.

    The same thing often happens when I execute an UPDATE or INSERT statement.

    Any ideas?

    -Matt

  2. #2
    Join Date
    Jul 2009
    Posts
    13
    Thanks
    1
    Thanked 3 Times in 3 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sqlite read lock.

    maybe you need a
    Qt Code:
    1. userQuery.prepare("DELETE FROM users WHERE username=?");
    To copy to clipboard, switch view to plain text mode 
    between lines 2 and 3.

  3. #3
    Join Date
    Jul 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sqlite read lock.

    Thanks for the suggestion, I tried passing in the query string to object via its "prepare" function (rather than in the constructor as above), and I have the same result. A sqlite-journal file is created, and the program returns without warning or error.

    One thing worth noting is that, when my QSqlQueryModel repopulates the view after the query, I get a duplicate of the last entry when recreating all the table rows. For example, if I have a user table that reads:

    John
    Fred
    Mark

    And I execute delete on Mark, My table repopulates like this:

    John
    Fred
    Fred

    Qt Code:
    1. QSqlQueryModel * getUsers()
    2. {
    3. QSqlQueryModel * users = new QSqlQueryModel();
    4. QString userQuery = "SELECT * FROM users";
    5. users->setQuery(userQuery, db);
    6. return users;
    7. }
    8.  
    9. void TbxUsers::populateUserList( void )
    10. {
    11. // Remove out all rows in the table.
    12. for( int i = 0; i < m_ui->tableWidget->rowCount(); i++)
    13. m_ui->tableWidget->removeRow(i);
    14.  
    15. // Get a model of users contained in the table.
    16. QSqlQueryModel * users = getUsers();
    17.  
    18. // Have to use QModelIndex to get the record count.
    19. QModelIndex index;
    20. int numUsers = users->rowCount(index);
    21.  
    22. for(int i = 0; i < numUsers; i++)
    23. {
    24. QTableWidgetItem *username;
    25. username = new QTableWidgetItem(users->record(i).value("username").toString());
    26. m_ui->tableWidget->insertRow(i);
    27. m_ui->tableWidget->setItem(i,0,username);
    28. }
    29. }
    To copy to clipboard, switch view to plain text mode 

    Something really strange is going on here. I considered the possibility of a corrupt database, but I've been using the firefox plugin to connect and manage my database outside of Qt, and things always seem to work well with it.

    PS: Adding transaction code in getUsers() didn't seem to help the matter either, though I didn't add transaction logic to every place in the software I use the QSqlQueryModel.

    Hmmm....

  4. #4
    Join Date
    Jan 2006
    Location
    Germany
    Posts
    4,380
    Thanks
    19
    Thanked 1,005 Times in 913 Posts
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows Symbian S60
    Wiki edits
    5

    Default Re: sqlite read lock.

    Hi, to your original problem, I don't the problem right now, but for deleting all rows, you can also use QTableWidget::clear() or QTableWidget::clearContents() and then you really should have a look at the docs regarding Qt Model/View. Because using a model with a QTableWidget is nonsens. Use a QTableView instead...

  5. #5
    Join Date
    Jul 2009
    Posts
    3
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: sqlite read lock.

    Lykurg:

    Thanks for the quick reply. Unfortunately QTableWidget::clear() removes the row headers. QTableWidget::clearContents() is what I should be using, but it didn't seem to clear the table entirely; or so it seemed.

    I think the real problem is with the QSqlQueryModel object. I believe that the TableWidget is doing its job, and is being populated with what the QSqlQueryModel object believes its current model is. The funny part is, that when I execute the DELETE statement, the item goes away, but the QSqlQueryModel record count stays the same.

    As far as a QTableView goes, I understand the Qt Model/View objects and what they provide, and I can tell you that there is certainly a method to my madness. QTableView really isn't a good fit for my particular application, in fact, QSqlQueryModel isn't really a good fit either.

    I am most concerned with the fact that the QSqlQueryModel object seems to believe *incorrectly* that the record has been removed from the database(until program restart). Possibly a sqlite driver issue? Is there is a command I need to perform to "close" a QSqlQueryModel query? (Otherwise it creates the *.sqlite-journal and locks the database until the program exits) Can't find any info in the Qt docs...

  6. #6
    Join Date
    Jan 2008
    Posts
    39
    Thanks
    10
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: sqlite read lock.

    Hello Gilgm

    I too am having problems with SQLite3 and QSqlRelationalTableModels. I have found that model.query().finish() before commit() works until the model has relations and then the queries for these need to be finished as well.

    I have an app with about 12 QSqlRelationalTableModels and this doesn't seem like a good solution to me.

    Cheers

  7. #7
    Join Date
    Jun 2010
    Posts
    1
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Post Re: sqlite read lock.

    THE REAL PROBLEM IS IN SOME PART OF THE PROGRAM SOME QUERY LL BE STILL EXECUTING CAUSING AN EXCLUSIVE LOCK TO THE DB ..
    THIS PREVENTS ANY FURTHER MODIFICATION OF THE DB..
    Try to commit the db ..
    Just run..

    COMMIT or END
    Or if that didnt work try to close the connection u r using ..
    and get the error report ..
    u can see that the close wont work.. and some query still active kind of error ll b raised ..

    hop this helps thank u..

Similar Threads

  1. Qt SQLite user functions
    By cevou in forum Qt Programming
    Replies: 1
    Last Post: 10th March 2009, 19:43
  2. External Lib read from QBuffer on Calback problem
    By patrik08 in forum Qt Programming
    Replies: 2
    Last Post: 2nd June 2008, 19:43
  3. How to read Raw Information from CD in MAC?
    By vishal.chauhan in forum General Programming
    Replies: 0
    Last Post: 10th July 2007, 12:26
  4. How to read CD with read?
    By vishal.chauhan in forum Qt Programming
    Replies: 6
    Last Post: 29th June 2007, 08:20
  5. QIODevice read()
    By ShaChris23 in forum Newbie
    Replies: 1
    Last Post: 3rd May 2007, 00:29

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.