Results 1 to 5 of 5

Thread: QSqlTableModel the database does not update

  1. #1
    Join Date
    Apr 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default QSqlTableModel the database does not update

    Hello
    I've got following problem since a week:
    I have a QSqlTableModel that I use to view data from a database in a calendar
    Qt Code:
    1. model=new QSqlTableModel(0,db);
    2. query=new QSqlQuery(db);
    To copy to clipboard, switch view to plain text mode 
    db is a QSqlDatabase
    When i want to make a query from the database using a SQL statement i use " query" that is connected to db

    When i want to insert record i proceed like that:
    I create a QSqlRecord that contains values of the new record then I call
    Qt Code:
    1. model->insertRecord(-1,record)
    To copy to clipboard, switch view to plain text mode 

    The problem is that although the recording is successfull (when closing the programm and re-open it i can see it) but it seems like the database would be updated only when closing it because i can not see it through a query just after insertion although i call model->submitAll after each insertion .. and trying different EditStrategy..

    Any ideas? Don't hesitate to give me advice on how to do it easier as i'm not very good with Programming

  2. #2
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlTableModel the database does not update

    If you update the database immediately, QSqlTableModel will have to refetch all the rows from the table again and all your views connected to this model will be reset. That's usually not what you want. What is wrong with submitting the data to the database when the application closes?
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


  3. #3
    Join Date
    Apr 2010
    Posts
    8
    Qt products
    Qt4
    Platforms
    Windows

    Default Re: QSqlTableModel the database does not update

    Well the thing is that the database contain bank operation and i want to see it different ways ( see the database as she is saved, see it as a calendar) all this views have their own QAbstractItemmodel and appear in different windows. If the user insert a new operation i want it to appear in the calendar view as soon as enterred thats why I need information to be updated

  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: QSqlTableModel the database does not update

    Works for me:
    Qt Code:
    1. #include <QtGui>
    2. #include <QtSql>
    3. #include <QDebug>
    4. // following include from Qt examples
    5. #include "connection.h"
    6.  
    7. int main(int argc, char *argv[])
    8. {
    9. QApplication app(argc, argv);
    10.  
    11. if (!createConnection())
    12. return 1;
    13.  
    14. model.setTable("person");
    15. model.setEditStrategy(QSqlTableModel::OnManualSubmit);
    16. model.select();
    17. qDebug() << "rowCount() =" << model.rowCount();
    18.  
    19. QSqlQuery query;
    20. query.prepare("select count(*) from person");
    21. if (query.exec() && query.next())
    22. qDebug() << query.value(0).toInt() << "rows";
    23.  
    24. model.removeRow(0);
    25. model.submitAll();
    26. qDebug() << "rowCount() =" << model.rowCount();
    27.  
    28. if (query.exec() && query.next())
    29. qDebug() << query.value(0).toInt() << "rows";
    30.  
    31. model.insertRows(0, 1);
    32. QModelIndex index = model.index(0, 0);
    33. model.setData(index, 999);
    34. model.submitAll();
    35. qDebug() << "rowCount() =" << model.rowCount();
    36.  
    37. if (query.exec() && query.next())
    38. qDebug() << query.value(0).toInt() << "rows";
    39.  
    40.  
    41. return app.exec();
    42. }
    To copy to clipboard, switch view to plain text mode 
    outputs:
    Qt Code:
    1. rowCount() = 5
    2. 5 rows
    3. rowCount() = 4
    4. 4 rows
    5. rowCount() = 5
    6. 5 rows
    To copy to clipboard, switch view to plain text mode 
    The insert will fail if the record violates NOT NULL or other constraints (hence my inserting 999 as a value). Are you checking for failure?

  5. #5
    Join Date
    Jan 2006
    Location
    Warsaw, Poland
    Posts
    33,359
    Thanks
    3
    Thanked 5,015 Times in 4,792 Posts
    Qt products
    Qt3 Qt4 Qt5 Qt/Embedded
    Platforms
    Unix/X11 Windows Android Maemo/MeeGo
    Wiki edits
    10

    Default Re: QSqlTableModel the database does not update

    Quote Originally Posted by Zander87 View Post
    Well the thing is that the database contain bank operation and i want to see it different ways ( see the database as she is saved, see it as a calendar) all this views have their own QAbstractItemmodel and appear in different windows. If the user insert a new operation i want it to appear in the calendar view as soon as enterred thats why I need information to be updated
    You should have a single model as a base and use proxy models built over the single model to "view" the data differently.
    Your biological and technological distinctiveness will be added to our own. Resistance is futile.

    Please ask Qt related questions on the forum and not using private messages or visitor messages.


Similar Threads

  1. i can't update database,
    By xiongxiongchuan in forum Qt Programming
    Replies: 7
    Last Post: 29th June 2010, 14:44
  2. Replies: 2
    Last Post: 21st October 2009, 08:13
  3. Disconnecting from database after using QSqlTableModel
    By RobbieClarken in forum Qt Programming
    Replies: 6
    Last Post: 8th April 2009, 09:51
  4. Replies: 2
    Last Post: 23rd February 2008, 01:58
  5. How to manage QSqlTableModel database operations?
    By Abk in forum Qt Programming
    Replies: 1
    Last Post: 19th September 2007, 10:44

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.