PDA

View Full Version : Record and row in a QSqlTableModel



QFreeCamellia
19th December 2011, 14:09
Hi all,
This is my first question in this forum, so some hospitality please :)

1/ What is the deference between record and row in a QSqlTableModel ?
2/ How can I edit/remove a QSqlTableModel record/row ?

Lykurg
19th December 2011, 15:27
Hi all,
This is my first question in this forum, so some hospitality please :)
Then you might want to ask in the Newbie section. (->moved)


1/ What is the deference between record and row in a QSqlTableModel ?
Simplified there is no difference.

2/ How can I edit/remove a QSqlTableModel record/row ?
For editing see setData() and removeRows().

QFreeCamellia
19th December 2011, 19:46
Hi,

When I insert a record in a not empty QSqlTableModel row, this row is temporally updated in the view, but in the database nothing happens.

ChrisW67
19th December 2011, 20:49
QSqlTableModel has several strategies to govern if/when it commits changes to the underlying database. What is your editStrategy() ?

Lykurg
19th December 2011, 22:45
Hi, please use the original thread for questions related to the original problem! (merged)

QFreeCamellia
20th December 2011, 00:46
I tried all of them: OnFieldChange, OnManualSubmit and OnRowChange, but without effect :(


QSqlRecord r = tableModel->record(5);
r.setValue("name", name);
r.setValue("age", age);
tableModel->setRecord(5, r);

ChrisW67
20th December 2011, 04:29
Ok, so you are using the QSqlTableModel specific interface and not the abstract model interface.

If the edit strategy is OnFieldChange then the record is committed immediately (using submitAll() internally) otherwise a manual submitAll() is required to commit the changes to the underlying database.


#include <QtGui>
#include <QtSql>
#include <QDebug>

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

QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (!db.open())
qFatal("Database not open");

QSqlQuery query;
query.exec("create table test (value varchar(100))");
query.exec("insert into test values('Foo')");

QSqlTableModel model;
model.setEditStrategy(QSqlTableModel::OnFieldChang e);
//model.setEditStrategy(QSqlTableModel::OnRowChange) ;
//model.setEditStrategy(QSqlTableModel::OnManualSubm it);
model.setTable("test");
model.select();

// Starting value
QModelIndex index = model.index(0, 0);
qDebug() << "Start" << index.data() ;

// Update the record outside the abstract model interface
QSqlRecord rec = model.record(0);
rec.setValue(0, QString("Bar"));
bool ok = model.setRecord(0, rec);
qDebug() << "setRecord OK" << ok;

// Let's see if the model is updated
index = model.index(0, 0);
qDebug() << "Update" << index.data();

// Explicit commit
//model.submitAll();

// Let's see if we query directly
QSqlQuery direct("select * from test");
while (direct.next()) {
qDebug() << "Committed" << direct.value(0);
}


return app.exec();
}
#include "main.moc"

outputs:


Start QVariant(QString, "Foo")
setRecord OK true
Update QVariant(QString, "Bar")
Committed QVariant(QString, "Bar")

as-is. If you use either of the other two edit strategies then you need to also use submitAll();


Qt 4.7.2 on Linux.

QFreeCamellia
21st December 2011, 10:03
Hi, can you check this code snippet please


tableModel->setEditStrategy(QSqlTableModel::OnRowChange);


QSqlRecord r = tableModel->record(5);

qDebug() << "name: " << r.value("name");
qDebug() << "age: " << r.value("age");

r.setValue("name", "newName");
r.setValue("age", "newAge");

bool ok = tableModel->setRecord(5, r);
qDebug() << "setRecord OK" << ok;

qDebug() << "name: " << r.value("name");
qDebug() << "age: " << r.value("age");

The result:


name: QVariant(QString, oldName)
age: QVariant(QString, oldeAge)

QSqlQuery::value: not positioned on a valid record // I don't understand this

setRecord OK true

name: QVariant(QString, newName)
age: QVariant(QString, newAge)

In the database nothing happens!

ChrisW67
21st December 2011, 23:29
The reason nothing happens in the database is explained in my last post.

Exactly which line triggers the warning message? Are there six rows in your table model? I cannot reproduce it with an example based on my code.