QT数数据库Mysql中 QSqlQuery、QSqlQueryModel 、和QSqlTableModel实现增删改查?代码 10
用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);
recordCurrentRow.setValue("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 (record.value("artist") == artist)
return record.value("id").toInt();
else
row++;
}
return addNewArtist(artist);
}