Q_ASSERT(db.open());
bool ok;
// ok = sqlQuery.exec(QLatin1String("CREATE TABLE Table1 (Content, Identifier INTEGER PRIMARY KEY AUTOINCREMENT, Parent INTEGER NOT NULL, Details);"));
ok
= sqlQuery.
exec(QLatin1String("CREATE TABLE Table1 (Content, Identifier INTEGER PRIMARY KEY AUTOINCREMENT, Parent, Details);"));
// ok = sqlQuery.exec(QLatin1String("CREATE TABLE Table1 (Content, Identifier, Parent, Details);"));
Q_ASSERT_X(ok, "CREATE TABLE sql statement", "Table1 likely already exists");
ok
= sqlQuery.
exec(QLatin1String("INSERT INTO Table1(Content, Parent, Details) VALUES ('first item', 0, 'Details for first item');"));
Q_ASSERT(ok);
tableModel->select();
tableView->setModel(tableModel);
tableView->resizeColumnsToContents();
Q_ASSERT(ok);
ok = tableModel->submitAll();
Q_ASSERT_X(ok, "submitAll", qPrintable(tableModel->lastError().text()));
QSqlDatabase db = QSqlDatabase::addDatabase(QLatin1String("QSQLITE"));
db.setDatabaseName(QLatin1String("testdb.db"));
Q_ASSERT(db.open());
QSqlQuery sqlQuery(db);
bool ok;
// ok = sqlQuery.exec(QLatin1String("CREATE TABLE Table1 (Content, Identifier INTEGER PRIMARY KEY AUTOINCREMENT, Parent INTEGER NOT NULL, Details);"));
ok = sqlQuery.exec(QLatin1String("CREATE TABLE Table1 (Content, Identifier INTEGER PRIMARY KEY AUTOINCREMENT, Parent, Details);"));
// ok = sqlQuery.exec(QLatin1String("CREATE TABLE Table1 (Content, Identifier, Parent, Details);"));
Q_ASSERT_X(ok, "CREATE TABLE sql statement", "Table1 likely already exists");
ok = sqlQuery.exec(QLatin1String("INSERT INTO Table1(Content, Parent, Details) VALUES ('first item', 0, 'Details for first item');"));
Q_ASSERT(ok);
QSqlTableModel* tableModel = new QSqlTableModel(this, db);
tableModel->setEditStrategy(QSqlTableModel::OnManualSubmit);
tableModel->setTable(QLatin1String("Table1"));
tableModel->select();
tableView->setModel(tableModel);
tableView->resizeColumnsToContents();
ok = tableModel->insertRows(0, 1, QModelIndex());
Q_ASSERT(ok);
ok = tableModel->submitAll();
Q_ASSERT_X(ok, "submitAll", qPrintable(tableModel->lastError().text()));
To copy to clipboard, switch view to plain text mode
Any help appreciated; either to correct my code or to find a work-around.
Bookmarks