发布网友 发布时间:2022-04-07 19:29
共3个回答
懂视网 时间:2022-04-07 23:50
QMYSQL"); db.setHostName("localhost"); db.setDatabaseName("ghcDB"); db.setUserName("root"); db.setPassword("123456");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数据库操作
标签:
热心网友 时间:2022-04-07 20:58
qt中有qtsql模块支持该操作,底层一般都是用驱动插件实现的,利用的是sqlite数据库,相关的类有热心网友 时间:2022-04-07 22:16
QT提供了很好的文档,看看就知道了啊。