db.setDatabaseName("test.db");
if (!db.open()) {
qDebug() << db.lastError().text();
return;
}
qDebug() << query.exec("CREATE TABLE test (id int PRIMARY KEY, test nvarchar)");
qDebug() << query.exec("INSERT INTO test VALUES(0, 'a')");
qDebug() << query.exec("INSERT INTO test VALUES(1, 'b')");
qDebug() << query.exec("INSERT INTO test VALUES(2, 'c')");
qDebug() << query.exec("INSERT INTO test VALUES(3, 'd')");
qDebug() << query.exec("SELECT test FROM test WHERE id = 1");
model->setQuery("SELECT id, test FROM test");
model->setHeaderData(0, Qt::Horizontal, tr("id"));
model->setHeaderData(1, Qt::Horizontal, tr("test"));
cb->setModel(model);
cb->setView(view);
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
db.setDatabaseName("test.db");
if (!db.open()) {
qDebug() << db.lastError().text();
return;
}
QSqlQuery query;
qDebug() << query.exec("CREATE TABLE test (id int PRIMARY KEY, test nvarchar)");
qDebug() << query.exec("INSERT INTO test VALUES(0, 'a')");
qDebug() << query.exec("INSERT INTO test VALUES(1, 'b')");
qDebug() << query.exec("INSERT INTO test VALUES(2, 'c')");
qDebug() << query.exec("INSERT INTO test VALUES(3, 'd')");
qDebug() << query.exec("SELECT test FROM test WHERE id = 1");
QSqlQueryModel *model = new QSqlQueryModel;
model->setQuery("SELECT id, test FROM test");
model->setHeaderData(0, Qt::Horizontal, tr("id"));
model->setHeaderData(1, Qt::Horizontal, tr("test"));
QTableView *view = new QTableView;
QComboBox *cb = new QComboBox();
cb->setModel(model);
cb->setView(view);
To copy to clipboard, switch view to plain text mode
Bookmarks