Lykurg
13th July 2010, 10:15
Hi,
I have following simple QSqlRelationalTableModel example which works for OnRowChange but not for OnManualSubmit. Any clue why?
#include <QtGui>
#include <QtSql>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (!db.open())
{
qDebug() << db.lastError();
return 0;
}
QSqlQuery query;
query.exec("create table employee(id int primary key, namex varchar(20), city int)");
query.exec("insert into employee values(1, 'Espen', 5000)");
query.exec("insert into employee values(2, 'Harald', 80000)");
query.exec("insert into employee values(3, 'Sam', 100)");
query.exec("create table city(id int, name varchar(20))");
query.exec("insert into city values(100, 'San Jose')");
query.exec("insert into city values(5000, 'Oslo')");
query.exec("insert into city values(80000, 'Munich')");
QSqlRelationalTableModel model;
model.setTable("employee");
model.setRelation(2, QSqlRelation("city", "id", "name"));
// model.setEditStrategy(QSqlTableModel::OnManualSubm it);
model.setEditStrategy(QSqlTableModel::OnRowChange) ;
model.select();
// Now add a new record
QSqlRecord r;
QSqlField f;
f.setName("id");
f.setType(QVariant::Int);
f.setValue(4);
r.append(f);
f.setName("namex");
f.setType(QVariant::String);
f.setValue("Lykurg");
r.append(f);
f.setName("city");
f.setType(QVariant::Int);
f.setValue(80000);
r.append(f);
qDebug() << "insertRecord" << model.insertRecord(-1, r);
qDebug() << " -->" << model.lastError();
qDebug() << "submitAll" << model.submitAll();
qDebug() << " -->" << model.lastError();
query.exec("SELECT id, namex, city FROM employee WHERE id = 4");
query.next();
qDebug() << "id:" << query.value(0).toString();
qDebug() << "name:" << query.value(1).toString();
qDebug() << "city:" << query.value(2).toString();
QTableView v;
v.setModel(&model);
v.show();
return a.exec();
}
Debug for OnRowChange:
insertRecord true
--> QSqlError(-1, "", "")
submitAll false
--> QSqlError(-1, "No Fields to update", "")
id: "4"
name: "Lykurg"
city: "80000"
And for OnManualSubmit:
insertRecord false
--> QSqlError(-1, "", "")
submitAll true
--> QSqlError(-1, "", "")
id: "4"
name: "Lykurg"
city: ""
As you see city is empty and so it failed...
Thanks,
Lykurg
I have following simple QSqlRelationalTableModel example which works for OnRowChange but not for OnManualSubmit. Any clue why?
#include <QtGui>
#include <QtSql>
int main(int argc, char* argv[])
{
QApplication a(argc, argv);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
if (!db.open())
{
qDebug() << db.lastError();
return 0;
}
QSqlQuery query;
query.exec("create table employee(id int primary key, namex varchar(20), city int)");
query.exec("insert into employee values(1, 'Espen', 5000)");
query.exec("insert into employee values(2, 'Harald', 80000)");
query.exec("insert into employee values(3, 'Sam', 100)");
query.exec("create table city(id int, name varchar(20))");
query.exec("insert into city values(100, 'San Jose')");
query.exec("insert into city values(5000, 'Oslo')");
query.exec("insert into city values(80000, 'Munich')");
QSqlRelationalTableModel model;
model.setTable("employee");
model.setRelation(2, QSqlRelation("city", "id", "name"));
// model.setEditStrategy(QSqlTableModel::OnManualSubm it);
model.setEditStrategy(QSqlTableModel::OnRowChange) ;
model.select();
// Now add a new record
QSqlRecord r;
QSqlField f;
f.setName("id");
f.setType(QVariant::Int);
f.setValue(4);
r.append(f);
f.setName("namex");
f.setType(QVariant::String);
f.setValue("Lykurg");
r.append(f);
f.setName("city");
f.setType(QVariant::Int);
f.setValue(80000);
r.append(f);
qDebug() << "insertRecord" << model.insertRecord(-1, r);
qDebug() << " -->" << model.lastError();
qDebug() << "submitAll" << model.submitAll();
qDebug() << " -->" << model.lastError();
query.exec("SELECT id, namex, city FROM employee WHERE id = 4");
query.next();
qDebug() << "id:" << query.value(0).toString();
qDebug() << "name:" << query.value(1).toString();
qDebug() << "city:" << query.value(2).toString();
QTableView v;
v.setModel(&model);
v.show();
return a.exec();
}
Debug for OnRowChange:
insertRecord true
--> QSqlError(-1, "", "")
submitAll false
--> QSqlError(-1, "No Fields to update", "")
id: "4"
name: "Lykurg"
city: "80000"
And for OnManualSubmit:
insertRecord false
--> QSqlError(-1, "", "")
submitAll true
--> QSqlError(-1, "", "")
id: "4"
name: "Lykurg"
city: ""
As you see city is empty and so it failed...
Thanks,
Lykurg