Results 1 to 10 of 10

Thread: QSQLITE Questions

  1. #1
    Join Date
    Jun 2006
    Location
    San Jose, CA
    Posts
    53
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default QSQLITE Questions

    Hi,

    Im playing with sql and found some strange things

    Qt Code:
    1. mSqlModel.setTable("test");
    2. mSqlModel.setEditStrategy(QSqlTableModel::OnManualSubmit);
    3. mSqlModel.select();
    4.  
    5. for(int row=0;row<500;row++)
    6. {
    7. QSqlRecord record = mSqlModel.record();
    8. record.setValue("str1",QVariant("blah"));
    9. record.setValue("value",QVariant(row));
    10. record.setValue("str2",QVariant("123abc"));
    11. mSqlModel.insertRecord(-1,record);
    12. if(!mSqlModel.submitAll())
    13. std::cout << "Can't add row=" << std::endl;
    14. }
    To copy to clipboard, switch view to plain text mode 

    Maybe someone can help me here:
    - mSqlModel.submitAll() returns always false
    - If I run the code twice or more I only get 756 records (500 through the first run).
    - If I run that in a seperate thread I get max 256 records
    - if I use
    Qt Code:
    1. mSqlModel.insertRecord(row,record);
    To copy to clipboard, switch view to plain text mode 
    I get sometimes only 256 records added

    Can someone tell me what I'm doing wrong here?
    Thanks a lot.

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSQLITE Questions

    Could you prepare a compilable example, so we can test it on our systems?

  3. #3
    Join Date
    Jun 2006
    Location
    San Jose, CA
    Posts
    53
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSQLITE Questions

    Here it is:

    It looks like the model can't hold more than 256 records. Something strange is going on

    Run it first as is and them comment out
    mSqlModel.setEditStrategy(QSqlTableModel::OnManual Submit); and the submitAll line

    and see the difference.

    Qt Code:
    1. #include <QApplication>
    2. #include <QTranslator>
    3. #include <QMessageBox>
    4. #include <QSqlError>
    5. #include <QSqlRecord>
    6. #include <iostream>
    7. #include <QtDebug>
    8. #include <QSqlQuery>
    9. #include <QSqlTableModel>
    10.  
    11.  
    12. int main(int argc, char *argv[])
    13. {
    14. QApplication app(argc, argv);
    15.  
    16. // Set up the database connection
    17. QSqlDatabase database = QSqlDatabase::addDatabase("QSQLITE");
    18. database.setDatabaseName(":memory:");
    19. if(!database.open())
    20. {
    21. QMessageBox::critical(0, QObject::tr("Error"),
    22. QObject::tr("Could not open database; driver said: %1, database said: %2")
    23. .arg(database.lastError().driverText())
    24. .arg( database.lastError().databaseText()));
    25. return -1;
    26. }
    27.  
    28. QSqlTableModel mSqlModel;
    29. QSqlQuery query;
    30. if(!query.exec("CREATE TABLE test ("
    31. "id INTEGER PRIMARY KEY,"
    32. "str VARCHAR(100))"))
    33. {
    34. qDebug() << "Could not create table; driver said: "
    35. << database.lastError().driverText() << ", database said: "
    36. << database.lastError().databaseText();
    37. return -2;
    38. }
    39.  
    40. mSqlModel.setTable("test");
    41. mSqlModel.setEditStrategy(QSqlTableModel::OnManualSubmit);
    42. mSqlModel.select();
    43. std::cout << "ROWS=" << mSqlModel.rowCount() << std::endl;
    44.  
    45. for(int row=0;row<5000;row++)
    46. {
    47. QSqlRecord record = mSqlModel.record();
    48. record.setValue("str",QVariant("123abc"));
    49. mSqlModel.insertRecord(-1,record);
    50. }
    51.  
    52. std::cout << "ROWS=" << mSqlModel.rowCount() << std::endl;
    53. if(!mSqlModel.submitAll())
    54. std::cout << "Can't add row=" << std::endl;
    55. std::cout << "ROWS=" << mSqlModel.rowCount() << std::endl;
    56. return -1;
    57. }
    To copy to clipboard, switch view to plain text mode 

    Qt Code:
    1. TEMPLATE = app
    2. TARGET +=
    3. DEPENDPATH += .
    4. INCLUDEPATH += .
    5.  
    6. QT += xml sql network
    7.  
    8. # Input RESOURCES +=
    9. HEADERS += \
    10.  
    11. SOURCES += \
    12. main.cpp \
    13.  
    14. FORMS += \
    To copy to clipboard, switch view to plain text mode 
    Last edited by jacek; 23rd November 2006 at 16:48. Reason: wrapped too long lines

  4. #4
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSQLITE Questions

    Quite interesting

    Update your example:
    Qt Code:
    1. ...
    2. #include <QSqlDriver>
    3. #include <QTableView>
    4. ...
    5. std::cout << "ROWS=" << mSqlModel.rowCount() << std::endl;
    6. std::cout << "FM=" << mSqlModel.canFetchMore() << std::endl;
    7. if(!mSqlModel.submitAll())
    8. std::cout << "Can't add row=" << std::endl;
    9. std::cout << "ROWS=" << mSqlModel.rowCount() << std::endl;
    10. std::cout << "FM=" << mSqlModel.canFetchMore() << std::endl;
    11. std::cout << "QS=" << QSqlDatabase().driver()->hasFeature( QSqlDriver::QuerySize ) << std::endl;
    12.  
    13. v.setModel( &mSqlModel );
    14. v.show();
    15. return app.exec();
    16. }
    To copy to clipboard, switch view to plain text mode 
    and try to scroll down the view.

    It looks like SQLite doesn't provide information about the number of rows returned by the query, so QSqlTableModel loads only 256 of them and fetches the rest when needed.

  5. #5
    Join Date
    Jun 2006
    Location
    San Jose, CA
    Posts
    53
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSQLITE Questions

    If I scroll down the view the last row I see is 257 - and this row is empty. Can you confirm that?

    Here is something else: If I use
    Qt Code:
    1. QSqlQuery query;
    2. query.prepare("INSERT INTO test (str) "
    3. "VALUES (:str)");
    4. query.bindValue(":str",QVariant("abc123"));
    5. query.exec();
    To copy to clipboard, switch view to plain text mode 

    to add data into the table and the model is defined before that

    Qt Code:
    1. QSqlTableModel mSqlModel;
    2. mSqlModel.setTable("test");
    To copy to clipboard, switch view to plain text mode 

    then the model is empty and stays empty. Is the definition after the data was written into the datatable the model shows all data. It looks like the driver does not tell the model that the data table has been changed.
    Last edited by larry104; 23rd November 2006 at 22:36.

  6. #6
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSQLITE Questions

    Quote Originally Posted by larry104 View Post
    If I scroll down the view the last row I see is 257 - and this row is empty. Can you confirm that?
    No, I can see all 5000 rows using both Qt 4.1.5 and 4.2.1.

    Quote Originally Posted by larry104 View Post
    It looks like the driver does not tell the model that the data table has been changed.
    Yes and actually I've never heard about a database that does it. You have to invoke QSqlTableModel::select() to get new data.

  7. #7
    Join Date
    Jun 2006
    Location
    San Jose, CA
    Posts
    53
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSQLITE Questions

    No, I can see all 5000 rows using both Qt 4.1.5 and 4.2.1
    Hmm - Im using 4.2.0 /Linux/RHEL3.0. Let me install 4.2.1 next week.
    Thanks a lot.

  8. #8
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    5,372
    Thanks
    28
    Thanked 976 Times in 912 Posts
    Qt products
    Qt3 Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSQLITE Questions

    Quote Originally Posted by larry104 View Post
    Im using 4.2.0 /Linux/RHEL3.0. Let me install 4.2.1 next week.
    AFAIK there shouldn't be any difference in this matter between Qt 4.2.0 and 4.2.1. Maybe the problem lies in SQLite?

  9. #9
    Join Date
    Jun 2006
    Location
    San Jose, CA
    Posts
    53
    Thanks
    4
    Thanked 1 Time in 1 Post
    Qt products
    Qt4
    Platforms
    Unix/X11 Windows

    Default Re: QSQLITE Questions

    So I installed 4.2.1 and it works fine now (on windows as well as linux).

  10. #10
    Join Date
    Apr 2007
    Posts
    1
    Qt products
    Qt4
    Platforms
    MacOS X Windows

    Default Re: QSQLITE Questions

    I am using QT 4.2.3 and have a Data entry form developed with designer.
    I am using a tableView, widgetMapper, and proxy model.

    SQLite locks the database and never makes the first update.

    void EntryDialog::submit()
    {
    lineEditStatus->setText("I");

    QSqlDatabase iolta = QSqlDatabase::database();
    QSqlQuery updateInput(iolta);
    QString updateString;
    updateString = " update transinput set "
    " status = :status,"
    " rate = :rate,"
    " earningstart = :earningstart,"
    " earningend = :earningend,"
    " gross = :gross,"
    " maint = :maint,"
    " activity = :activity,"
    " handling = :handling,"
    " net = :net"
    " where transinput.banknum = :banknum"
    " and transinput.trustaccount = :trustaccount ";
    updateInput.prepare(updateString);
    updateInput.bindValue(":status",lineEditStatus->text().trimmed());
    updateInput.bindValue(":rate",lineEditRate->text().toDouble());
    updateInput.bindValue(":earningstart",QDate::fromS tring(lineEditEarningStart->text(),"yyyy-MM-dd"));
    updateInput.bindValue(":earningend",QDate::fromStr ing(lineEditEarningEnd->text(),"yyyy-MM-dd"));
    updateInput.bindValue(":gross",lineEditGross->text().toDouble());
    updateInput.bindValue(":maint",lineEditMaint->text().toDouble());
    updateInput.bindValue(":activity",lineEditActivity->text().toDouble());
    updateInput.bindValue(":handling",lineEditHandling->text().toDouble());
    updateInput.bindValue(":net",lineEditNet->text().toDouble());
    updateInput.bindValue(":banknum",lineEditBankNumbe r->text());
    updateInput.bindValue(":trustaccount",lineEditTrus tAccount->text());

    if (! updateInput.exec())
    {
    QMessageBox::critical(0, qApp->tr("Update Failed"),
    updateInput.lastError().text()+"\n"+updateString, QMessageBox::Cancel);
    return;
    };

Similar Threads

  1. Image sequence Questions...
    By Deep Thought in forum Qt Programming
    Replies: 5
    Last Post: 12th February 2016, 10:23
  2. QKeyEvent questions
    By bglidden in forum Qt Programming
    Replies: 1
    Last Post: 3rd October 2006, 06:29
  3. 2 Questions about layouts
    By SkripT in forum Qt Programming
    Replies: 1
    Last Post: 26th February 2006, 13:54
  4. Qt related questions and thoughts about getting job
    By AlexKiriukha in forum General Discussion
    Replies: 4
    Last Post: 26th January 2006, 12:25
  5. A few questions about Qt/Win
    By Bojan in forum Installation and Deployment
    Replies: 3
    Last Post: 16th January 2006, 09:54

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.