Works for me:
#include <QtGui>
#include <QtSql>
#include <QDebug>
// following include from Qt examples
#include "connection.h"
int main(int argc, char *argv[])
{
if (!createConnection())
return 1;
model.setTable("person");
model.select();
qDebug() << "rowCount() =" << model.rowCount();
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);
model.setData(index, 999);
model.submitAll();
qDebug() << "rowCount() =" << model.rowCount();
if (query.exec() && query.next())
qDebug() << query.value(0).toInt() << "rows";
return app.exec();
}
#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::OnManualSubmit);
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();
}
To copy to clipboard, switch view to plain text mode
outputs:
rowCount() = 5
5 rows
rowCount() = 4
4 rows
rowCount() = 5
5 rows
rowCount() = 5
5 rows
rowCount() = 4
4 rows
rowCount() = 5
5 rows
To copy to clipboard, switch view to plain text mode
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?
Bookmarks