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
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
Code:
model->insertRecord(-1,record)
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
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?
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
Re: QSqlTableModel the database does not update
Works for me:
Code:
#include <QtGui>
#include <QtSql>
#include <QDebug>
// following include from Qt examples
#include "connection.h"
int main(int argc, char *argv[])
{
if (!createConnection())
return 1;
model.setTable("person");
model.select();
qDebug() << "rowCount() =" << model.rowCount();
query.prepare("select count(*) from person");
if (query.exec() && query.next())
qDebug() << query.value(0).toInt() << "rows";
model.removeRow(0);
model.submitAll();
qDebug() << "rowCount() =" << model.rowCount();
if (query.exec() && query.next())
qDebug() << query.value(0).toInt() << "rows";
model.insertRows(0, 1);
model.setData(index, 999);
model.submitAll();
qDebug() << "rowCount() =" << model.rowCount();
if (query.exec() && query.next())
qDebug() << query.value(0).toInt() << "rows";
return app.exec();
}
outputs:
Code:
rowCount() = 5
5 rows
rowCount() = 4
4 rows
rowCount() = 5
5 rows
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?
Re: QSqlTableModel the database does not update
Quote:
Originally Posted by
Zander87
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.