用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);
}