bool VehicleSpecificCharacteristics::saveDataToSQL(QString & errmsg)
{
//TODO:做具体实现
//获取时间
QString m_saveTime = QDateTime::currentDateTime().toString(“yyyy-MM-dd HH:mm:ss”);
//TODO:获取被选中行的数据
QStandardItemModel model = qobject_cast<QStandardItemModel>(ui->dataTableView->model());
if (model) {
int j = ui->dataTableView->model()->rowCount();
for (int i = 0; i < model->rowCount(); ++i) {
QString DrwNo = model->data(model->index(i, 1)).toString();
QString Version = model->data(model->index(i, 2)).toString();
QString FeatureCode = model->data(model->index(i, 4)).toString();
QString Category = model->data(model->index(i, 11)).toString();
QString Drawinglocation = model->data(model->index(i, 13)).toString();
QString Specification = model->data(model->index(i, 17)).toString();
//QString Specification = QString::fromLocal8Bit(“类型:几何公差,公差符号:&00BF,公差值:&00D81.5*&00CC,第一基准:D,第二基准:E,第三基准:F,上附加文本:M6,下附加文本:,左附加文本:,右附加文本:△● <54>,是否测点:true,尺寸id:48”);
QString DimID ;
int pos = Specification.indexOf(“尺寸id:”);
if (pos != -1) {
DimID = Specification.mid(pos + 5);
}
SpecialSymbolSQLDataReplace(0, Specification);
QString DataComparison = model->data(model->index(i, 18)).toString();
QString IsHandLost = model->data(model->index(i, 19)).toString();
QString tagID = model->data(model->index(i, 20)).toString();
QString isDetectingPoint = model->data(model->index(i, 21)).toString();
QString p_FeaturesNumber, d_FeaturesNumber;
QString key, improtant;
key = ui->rockNumLabel->text(); improtant = ui->rockNumLabel_2->text();
p_FeaturesNumber = key + ‘|’ + improtant;
key = ui->localNumLabel_1->text(); improtant = ui->localNumLabel_2->text();
d_FeaturesNumber = key + ‘|’ + improtant;
QString strSql, strSql1;
QVector<QVector> dataTemp;
strSql = QString::fromLocal8Bit(“SELECT * FROM SpecialCharacteristic_Record_1 WHERE 图号=‘%1’ AND 标识ID=‘%2’”).arg(DrwNo).arg(tagID);
bool anser = SqlServer::instance_->GetSQLData(strSql, dataTemp);
if (true) {
if (dataTemp.size() > 0) {
strSql1 = QString::fromLocal8Bit(“UPDATE SpecialCharacteristic_Record_1 SET 图纸版本=‘%1’,类别 =‘%2’, 图纸位置 =‘%3’,规格 =‘%4’, 数据对比结果 =‘%5’, 是否手输 =‘%6’,是否测点=‘%7’,磐石特性数量=‘%8’,图纸特性数量=‘%9’,用户=‘%10’,保存时间=‘%11’,尺寸ID=‘%12’ WHERE 图号=‘%13’ AND 标识ID=‘%14’”).arg(Version)
.arg(Category).arg(Drawinglocation).arg(Specification).arg(DataComparison).arg(IsHandLost).arg(isDetectingPoint)
.arg(p_FeaturesNumber).arg(d_FeaturesNumber).arg(StrUserName).arg(m_saveTime).arg(DimID).arg(DrwNo).arg(tagID);
}
else {
strSql1 = QString::fromLocal8Bit(“INSERT INTO SpecialCharacteristic_Record_1 (图号, 图纸版本, 特性编码, 类别, 图纸位置, 规格, 数据对比结果, 是否手输,标识ID,是否测点,磐石特性数量,图纸特性数量,用户,保存时间,尺寸ID) VALUES (‘%1’, ‘%2’, ‘%3’, ‘%4’, ‘%5’, ‘%6’,‘%7’,‘%8’,‘%9’,‘%10’,‘%11’,‘%12’,‘%13’,‘%14’,‘%15’)”)
.arg(DrwNo).arg(Version).arg(FeatureCode).arg(Category).arg(Drawinglocation)
.arg(Specification).arg(DataComparison).arg(IsHandLost).arg(tagID)
.arg(isDetectingPoint).arg(p_FeaturesNumber).arg(d_FeaturesNumber)
.arg(StrUserName).arg(m_saveTime).arg(DimID);
}
SqlServer::instance_->RunSQL(strSql1);
}
}
}//TODO:这里没有对插入的错误判断 else { SqlServer::instance_->CloseServer(); return false; } return false;
}根据该代码修改下列代码中的数据库以及链接数据库的方式:void VehicleSpecificCharacteristics::UploadRecordToSql()
{
// 获取当前时间
QString uploadTime = QDateTime::currentDateTime().toString(“yyyy-MM-dd hh:mm:ss”);
// 读取INI配置 QSettings settings(IniFilePath, QSettings::IniFormat); QString strIP = settings.value("MDSCBB/HOSTDB2", "").toString(); // 数据库连接 QSqlDatabase db = QSqlDatabase::addDatabase("QODBC", "SpecialCharacteristicConnection"); db.setDatabaseName(QString("DRIVER={SQL Server};SERVER=%1;").arg(strIP)); bool ret = db.open(); bool ret1 = false; if (ret) { // 获取模型 QStandardItemModel* model = qobject_cast<QStandardItemModel*>(ui->dataTableView->model()); if (!model) { qWarning() << "无效的表格模型"; return; } // 遍历所有行 for (int i = 0; i < model->rowCount(); i++) { // 获取复选框状态 QStandardItem* checkItem = model->item(i, 0); if (checkItem && checkItem->checkState() == Qt::Checked) { // 获取各列数据 - 使用安全访问方式 QString DrwNo = model->item(i, static_cast<int>(COLNAME::DRAWING_CODE)) ? model->item(i, static_cast<int>(COLNAME::DRAWING_CODE))->text() : ""; // 图号 QString Version = model->item(i, static_cast<int>(COLNAME::DARWING_VERSION)) ? model->item(i, static_cast<int>(COLNAME::DARWING_VERSION))->text() : ""; // 图纸版本 QString FeatureCode = model->item(i, static_cast<int>(COLNAME::CODE)) ? model->item(i, static_cast<int>(COLNAME::CODE))->text() : ""; // 特性编码 QString Category = model->item(i, static_cast<int>(COLNAME::CLASS_NAME_LOCAL)) ? model->item(i, static_cast<int>(COLNAME::CLASS_NAME_LOCAL))->text() : ""; // 类别 QString Drawinglocation = model->item(i, static_cast<int>(COLNAME::DRAWING_LOCATION_LOCAL)) ? model->item(i, static_cast<int>(COLNAME::DRAWING_LOCATION_LOCAL))->text() : ""; // 图纸位置 QString Specification = model->item(i, static_cast<int>(COLNAME::QUALITY_SPEC_LOCAL1)) ? model->item(i, static_cast<int>(COLNAME::QUALITY_SPEC_LOCAL1))->text() : ""; // 规格 QString tagID = model->item(i, static_cast<int>(COLNAME::UNIQUE_ID)) ? model->item(i, static_cast<int>(COLNAME::UNIQUE_ID))->text() : ""; // 唯一标识ID // 处理特殊字符 Specification.replace("'", "''"); // 转义单引号 // 检查记录是否存在 QSqlQuery checkQuery(db); checkQuery.prepare("SELECT COUNT(*) FROM SpecialCharacteristic_Upload_Record " "WHERE 图号 = ? AND 图纸版本 = ? AND 标识ID = ?"); checkQuery.addBindValue(DrwNo); checkQuery.addBindValue(Version); checkQuery.addBindValue(tagID); QSqlQuery insertQuery(db); bool ret1 = false; if (checkQuery.exec() && checkQuery.next()) { int recordCount = checkQuery.value(0).toInt(); if (recordCount > 0) { // 更新现有记录 QSqlQuery updateQuery(db); updateQuery.prepare( "UPDATE SpecialCharacteristic_Upload_Record SET " "类别 = :category, 图纸位置 = :location, " "规格 = :spec, 用户 = :user, 上传时间 = :time " "WHERE 图号 = :drw AND 图纸版本 = :ver AND 标识ID = :tag" ); updateQuery.bindValue(":category", Category); updateQuery.bindValue(":location", Drawinglocation); updateQuery.bindValue(":spec", Specification); updateQuery.bindValue(":user", StrUserName); updateQuery.bindValue(":time", uploadTime); updateQuery.bindValue(":drw", DrwNo); updateQuery.bindValue(":ver", Version); updateQuery.bindValue(":tag", tagID); ret1 = updateQuery.exec(); } else { // 插入新记录 QSqlQuery insertQuery(db); insertQuery.prepare( "INSERT INTO SpecialCharacteristic_Upload_Record " "(图号, 图纸版本, 特性编码, 类别, 图纸位置, 规格, 标识ID, 用户, 上传时间) " "VALUES (:drw, :ver, :feat, :cat, :loc, :spec, :tag, :user, :time)" ); insertQuery.bindValue(":drw", DrwNo); insertQuery.bindValue(":ver", Version); insertQuery.bindValue(":feat", FeatureCode); insertQuery.bindValue(":cat", Category); insertQuery.bindValue(":loc", Drawinglocation); insertQuery.bindValue(":spec", Specification); insertQuery.bindValue(":tag", tagID); insertQuery.bindValue(":user", StrUserName); insertQuery.bindValue(":time", uploadTime); ret1 = insertQuery.exec(); } if (!ret1) { qWarning() << "SQL Error:" << insertQuery.lastError().text(); } } } } if (!ret1) { qWarning() << "保存失败"; // QMessageBox::warning(this, "错误", "保存失败", QMessageBox::Ok); } } else { qCritical() << "数据库连接失败:" << db.lastError().text(); // QMessageBox::critical(this, "错误", "数据库连接失败,请联系管理员"); } db.close(); QSqlDatabase::removeDatabase("SpecialCharacteristicConnection");
}