PDA

View Full Version : QSqlTableModel the database does not update



Zander87
26th February 2011, 12:12
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

model=new QSqlTableModel(0,db);
query=new QSqlQuery(db);
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

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

wysota
27th February 2011, 08:02
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?

Zander87
27th February 2011, 21:34
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

ChrisW67
27th February 2011, 22:03
Works for me:


#include <QtGui>
#include <QtSql>
#include <QDebug>
// following include from Qt examples
#include "connection.h"

int main(int argc, char *argv[])
{
QApplication app(argc, argv);

if (!createConnection())
return 1;

QSqlTableModel model;
model.setTable("person");
model.setEditStrategy(QSqlTableModel::OnManualSubm it);
model.select();
qDebug() << "rowCount() =" << model.rowCount();

QSqlQuery query;
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);
QModelIndex index = model.index(0, 0);
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:

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?

wysota
27th February 2011, 22:34
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.