1.QSqlQuery的SELECT查询记录用法:
QSqlQuery q("SELECT * FROM departments");QSqlRecord rec = q.record();int idCol = rec.indexOf("departID"); // index of the field "departID"int nameCol=rec.indexOf("department");// index of the field "department"QList<QString> listStrDepartID;//部门编号列表QList<QString> listStrDepartment;//部门名称列表listStrDepartID.clear();//清空列表内容listStrDepartment.clear();//清空列表内容while (q.next()){listStrDepartID<<q.value(idCol).toString();//获取表departments中的"departID"字段数据listStrDepartment<<q.value(nameCol).toString();//获取表departments中的"department"字段数据}
2.QSqlQuery的INSERT插入记录用法:
QSqlQuery query;//新建departID记录内容query.prepare("INSERT INTO departments (departID,department) ""VALUES (:departID,:department)");query.bindValue(":departID",strNewDepartmentNum);query.bindValue(":department",ui->dbEditNewDepartmentName->text());query.exec();
3.QSqlQuery的UPDATE更新(或修改)记录用法:
QSqlQuery query;//更新departID处对应的department内容query.prepare("UPDATE departments SET department =:department WHERE departID =:departID ");query.bindValue(":departID",ui->dbEditModifyDepartmentNum->text());query.bindValue(":department",ui->dbComboModifyDepartmentName->currentText());query.exec();
4.QSqlQuery的DELETE删除记录用法:
QSqlQuery query;//删除departID记录内容query.prepare("DELETE FROM departments WHERE departID =:departID ");query.bindValue(":departID",ui->dbEditDeleteDepartmentNum->text());query.exec();