用qsqltablemodel的insetrow()、setdata()、submitall()函数实现增;
officeTable->insertRow(0);
officeTable->setData(officeTable->index(0, 0), row);
officeTable->setData(officeTable->index(0, 1), newWnd->imageFileEditor->currentIndex());
officeTable->setData(officeTable->index(0, 2), newWnd->locationText->text());
officeTable->setData(officeTable->index(0, 3), newWnd->countryText->currentText());
officeTable->setData(officeTable->index(0, 4), newWnd->descriptionEditor->toPlainText());
officeTable->submitAll();
用removerow()、submitall()函数实现删;
int officeCount = officeTable->rowCount();
officeTable->removeRow(id);
for(int i = id; i < officeCount - 1;i++)
{
officeTable->setData(officeTable->index(i, 0), i);
}
officeTable->submitAll();
用QSqlRecord类的setvalue实现改;
QSqlRecord recordCurrentRow = officeTable->record(id);
recordCurrentRowsetValue("id", id - 1);
officeTable->setRecord(id - 1, recordCurrentRow);
officeTable->submitAll();
用QSqlRecord类的value进行比较实现查;
int Dialog::findArtistId(const QString &artist)
{
QSqlTableModel artistModel = model->relationModel(2);
int row = 0;while (row < artistModel->rowCount()) {
QSqlRecord record = artistModel->record(row);
if (recordvalue("artist") == artist)
return recordvalue("id")toInt();
else
row++;
}
return addNewArtist(artist);
}//打开数据库
QSqlDatabase db = QSqlDatabase::addDatabase("QSQLITE");
dbsetDatabaseName("nmrdb");
if (!dbopen())
{
QMessageBox::critical(0, qApp->tr("Cannot open database"),
qApp->tr("Unable to establish a database connection\n"
"This example needs SQLite support Please read "
"the Qt SQL driver documentation for information how "
"to build it\n\n"
"Click Cancel to exit"), QMessageBox::Cancel);
return false;
}
//建个QListView显示数据库中的表
//显示数据表
void MainWindow::listDb()
{
dbListModel =new QStringListModel();
dbListModel->setStringList(QSqlDatabase::database()tables());
ui->listView->setModel(dbListModel);
ui->listView->setEditTriggers(QAbstractItemView::NoEditTriggers);
ui->listView->setResizeMode(QListView::Adjust);
connect(ui->listView,SIGNAL(clicked(QModelIndex)),this,SLOT(showDb(QModelIndex)));
}
//SLOT:数据显示在QTabelView控件中
void MainWindow::showDb(QModelIndex index)
{
dbName=indexdata()toString();
tableViewModel=new QSqlTableModel(this);
tableViewModel->setTable(dbName);
tableViewModel->select();//
ui->dbTable->setSortingEnabled(true);
ui->dbTable->setModel(tableViewModel);
ui->dbTable->sortByColumn(1,Qt::AscendingOrder);
ui->dbTable->resizeColumnsToContents();
ui->dbTable->setSelectionBehavior(QAbstractItemView::SelectRows);
}
以上就是关于QT数数据库Mysql中 QSqlQuery、QSqlQueryModel 、和QSqlTableModel实现增删改查代码全部的内容,包括:QT数数据库Mysql中 QSqlQuery、QSqlQueryModel 、和QSqlTableModel实现增删改查代码、Qt Quick中如何打开一个本地数据库、等相关内容解答,如果想了解更多相关内容,可以关注我们,你们的支持是我们更新的动力!
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)