时间:2021-07-01 10:21:17 帮助过:5人阅读
2、增
QSqlTableModel *modelTable=new QSqlTableModel(this); modelTable->setTable(strTableName); modelTable->select();
int row=modelTable->rowCount();
modelTable->insertRows(row,1); modelTable->setData(modelTable->index(row,0),addDataDlg.editName->text()); if("男"==addDataDlg.comboSex->currentText()) modelTable->setData(modelTable->index(row,1),"M"); if("女"==addDataDlg.comboSex->currentText()) modelTable->setData(modelTable->index(row,1),"F"); modelTable->setData(modelTable->index(row,2),addDataDlg.editAge->text());
modelTable->submitAll();
3、删
QSqlTableModel *modelTableInfoOldSelected=new QSqlTableModel(this);
modelTableInfoOldSelected->setTable(strTableName);
modelTableInfoOldSelected->select();
modelTableInfoOldSelected->removeRows(rowSelected,1);
modelTableInfoOldSelected->submitAll();
4、改
QSqlTableModel *modelTable=new QSqlTableModel(this); modelTable->setTable(strTableName); modelTable->select(); int row=rowSelected;
modelTable->setData(modelTable->index(row,0),changeDataDlg.editName->text()); if("男"==changeDataDlg.comboSex->currentText()) modelTable->setData(modelTable->index(row,1),"M"); if("女"==changeDataDlg.comboSex->currentText()) modelTable->setData(modelTable->index(row,1),"F"); modelTable->setData(modelTable->index(row,2),changeDataDlg.editAge->text());
modelTable->submitAll();
5、查
QSqlQueryModel *modelQuery=new QSqlQueryModel; modelQuery->setQuery(tr("select Name,Sex,Age from %1").arg(strTableName));
modelQuery->setHeaderData(0,Qt::Horizontal,tr("姓名")); modelQuery->setHeaderData(1,Qt::Horizontal,tr("性别")); modelQuery->setHeaderData(2,Qt::Horizontal,tr("年龄"));
tableView->setModel(modelQuery);
6、读取数据库某一条记录中各字段的值给控件
QSqlTableModel *modelTableInfoOldSelected=new QSqlTableModel(this); modelTableInfoOldSelected->setTable(strTableName); modelTableInfoOldSelected->select(); QSqlRecord record=modelTableInfoOldSelected->record(rowSelected); editName->setText(record.value("Name").toString()); if("M"==record.value("Sex").toString()) comboSex->setCurrentIndex(0); else if("F"==record.value("Sex").toString()) comboSex->setCurrentIndex(1); editAge->setText(record.value("Age").toString());
参考连接:http://blog.csdn.net/light1028/article/details/8164476
Qt数据库操作
标签: