#include <QApplication>
#include <QSqlDatabase>
#include <QSqlTableModel>
#include <QSqlQuery>
#include <QSqlError>
#include <QTemporaryFile>
#include <QMessageBox>
#include <QDebug>
#define DBSIZE 266
int main(int argc, char* argv[]){
// create a database file in the temporary directory
dbFile->setAutoRemove(false);
bool ok = dbFile->open();
Q_ASSERT(ok);
db.setDatabaseName(dbFile->fileName());
qDebug() << dbFile->fileName();
dbFile->close();
delete dbFile;
ok = db.open();
Q_ASSERT(ok);
// create table 'main' in the database
ok = query.exec(QStringLiteral("CREATE TABLE main(id INTEGER PRIMARY KEY, colorcode INTEGER)"));
Q_ASSERT(ok);
// fill table 'main' with records
ok = query.prepare(QStringLiteral("INSERT INTO main VALUES(:id, 0)"));
Q_ASSERT(ok);
for (int i(0); i < DBSIZE; ++i) {
qDebug() << i;
query.bindValue(0, i);
query.exec();
Q_ASSERT_X(ok, query.lastQuery().toLatin1(), query.lastError().text().toLatin1());}
// use table 'main' in a QSqlTableModel; QSqlTableModel::select() will block the database
model->setTable(QStringLiteral("main"));
ok = model->select();
Q_ASSERT(ok);
// show the filename of the database to the user
QMessageBox::warning(0, db.
databaseName(), QStringLiteral
("With sufficiently high DBSIZE, the above database is now erroneously locked. On my Ubuntu machine, DBSIZE 266 is sufficient."));
return 0;}
#include <QApplication>
#include <QSqlDatabase>
#include <QSqlTableModel>
#include <QSqlQuery>
#include <QSqlError>
#include <QTemporaryFile>
#include <QMessageBox>
#include <QDebug>
#define DBSIZE 266
int main(int argc, char* argv[]){
QApplication a(argc, argv);
// create a database file in the temporary directory
QSqlDatabase db = QSqlDatabase::addDatabase(QStringLiteral("QSQLITE"));
QTemporaryFile* dbFile = new QTemporaryFile;
dbFile->setAutoRemove(false);
bool ok = dbFile->open();
Q_ASSERT(ok);
db.setDatabaseName(dbFile->fileName());
qDebug() << dbFile->fileName();
dbFile->close();
delete dbFile;
ok = db.open();
Q_ASSERT(ok);
// create table 'main' in the database
QSqlQuery query(db);
ok = query.exec(QStringLiteral("CREATE TABLE main(id INTEGER PRIMARY KEY, colorcode INTEGER)"));
Q_ASSERT(ok);
// fill table 'main' with records
ok = query.prepare(QStringLiteral("INSERT INTO main VALUES(:id, 0)"));
Q_ASSERT(ok);
for (int i(0); i < DBSIZE; ++i) {
qDebug() << i;
query.bindValue(0, i);
query.exec();
Q_ASSERT_X(ok, query.lastQuery().toLatin1(), query.lastError().text().toLatin1());}
// use table 'main' in a QSqlTableModel; QSqlTableModel::select() will block the database
QSqlTableModel* model = new QSqlTableModel(0, db);
model->setTable(QStringLiteral("main"));
ok = model->select();
Q_ASSERT(ok);
// show the filename of the database to the user
QMessageBox::warning(0, db.databaseName(), QStringLiteral("With sufficiently high DBSIZE, the above database is now erroneously locked. On my Ubuntu machine, DBSIZE 266 is sufficient."));
return 0;}
To copy to clipboard, switch view to plain text mode
Bookmarks