#include <QtSql>
#include <QDebug>
static bool createTestData()
{
db.setDatabaseName(":memory:");
bool ok = db.open();
Q_ASSERT(ok);
query.exec("create table Table1 (id int primary key, data varchar(100), abc int)");
query.exec("insert into Table1 values(1, 'row data', 1)");
query.exec("insert into Table1 values(2, 'row data', 1)");
query.exec("insert into Table1 values(3, 'row data', 1)");
query.exec("insert into Table1 values(4, 'row data', 2)");
query.exec("insert into Table1 values(5, 'row data', 2)");
query.
exec(QLatin1String("CREATE TABLE Table2 (Number, String);"));
query.
exec(QLatin1String("INSERT INTO Table2(Number, String) VALUES (1, 'One');"));
query.
exec(QLatin1String("INSERT INTO Table2(Number, String) VALUES (2, 'Two');"));
query.
exec(QLatin1String("INSERT INTO Table2(Number, String) VALUES (3, 'Three');"));
return true;
}
int main(int argc, char *argv[])
{
bool ok;
if (!createTestData())
return 1;
tableModel.setTable("Table1");
tableModel.select();
qDebug() << "original row count" << tableModel.rowCount();
Q_ASSERT(ok);
QSqlRecord record
= tableModel.
record(0);
// retrieve inserted record that is empty record.
setValue(1,
QLatin1String("item added by insertRow and setRecord"));
ok = tableModel.setRecord(0, record); // record is no longer empty*/
Q_ASSERT(ok);
ok = tableModel.submitAll();
Q_ASSERT(ok);
qDebug() << "tableModel not yet updated, row count still" << tableModel.rowCount();
ok = tableModel.select();
Q_ASSERT(ok);
qDebug() << "after tableModel.select(), but row count still" << tableModel.rowCount();
ok = tableModel.select();
Q_ASSERT(ok);
qDebug() << "row count only now increased" << tableModel.rowCount();
return app.exec();
}
#include <QtSql>
#include <QDebug>
static bool createTestData()
{
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName(":memory:");
bool ok = db.open();
Q_ASSERT(ok);
QSqlQuery query;
query.exec("create table Table1 (id int primary key, data varchar(100), abc int)");
query.exec("insert into Table1 values(1, 'row data', 1)");
query.exec("insert into Table1 values(2, 'row data', 1)");
query.exec("insert into Table1 values(3, 'row data', 1)");
query.exec("insert into Table1 values(4, 'row data', 2)");
query.exec("insert into Table1 values(5, 'row data', 2)");
query.exec(QLatin1String("CREATE TABLE Table2 (Number, String);"));
query.exec(QLatin1String("INSERT INTO Table2(Number, String) VALUES (1, 'One');"));
query.exec(QLatin1String("INSERT INTO Table2(Number, String) VALUES (2, 'Two');"));
query.exec(QLatin1String("INSERT INTO Table2(Number, String) VALUES (3, 'Three');"));
return true;
}
int main(int argc, char *argv[])
{
bool ok;
QCoreApplication app(argc, argv);
if (!createTestData())
return 1;
QSqlDatabase db = QSqlDatabase::database();
QSqlRelationalTableModel tableModel;
tableModel.setTable("Table1");
tableModel.setRelation(2, QSqlRelation(QLatin1String("Table2"), QLatin1String("Number"), QLatin1String("String")));
tableModel.select();
qDebug() << "original row count" << tableModel.rowCount();
ok = tableModel.insertRow(0, QModelIndex());
Q_ASSERT(ok);
QSqlRecord record = tableModel.record(0); // retrieve inserted record that is empty
record.setValue(1, QLatin1String("item added by insertRow and setRecord"));
ok = tableModel.setRecord(0, record); // record is no longer empty*/
Q_ASSERT(ok);
ok = tableModel.submitAll();
Q_ASSERT(ok);
qDebug() << "tableModel not yet updated, row count still" << tableModel.rowCount();
ok = tableModel.select();
Q_ASSERT(ok);
qDebug() << "after tableModel.select(), but row count still" << tableModel.rowCount();
tableModel.setTable(QLatin1String("Table1"));
ok = tableModel.select();
Q_ASSERT(ok);
qDebug() << "row count only now increased" << tableModel.rowCount();
return app.exec();
}
To copy to clipboard, switch view to plain text mode
I switched to QSqlRelationalTableModel and added setRelation (to do this, I created a second table and added a column to your original table query). Now the error is reproduced. If I comment out line 38, then the program runs correctly.
Bookmarks