You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

MySQL sql::ResultSet最后元素字符串匹配失败排查求助

问题排查:ResultSet最后一个元素字符串匹配失败

问题描述

已成功建立MySQL连接并获取sql::ResultSet,文本内容可正常输出到QTableWidget单元格,但尝试将"Grass"/"Rock"文本替换为QIcon时,仅ResultSet最后一个元素的匹配逻辑触发失败,直接进入else分支输出原文本。怀疑是最后一个元素字符串末尾存在 trailing whitespace、换行符等额外字符导致匹配失效。

相关代码:

void QtPlayground::DisplayTable(sql::ResultSet* results)
{
    int count = 0;
    QTableWidgetItem* item;
    QIcon icon;

    //keep looping through the result set
    while(result->next())
    {
        // Insert the new row before we start to display to our current data in QTableWidget
        ui.displayTable->insertRow(ui.displayTable->rowCount());
            
        // Loop throught the items in this current line of the ResultSet (took out extra 'else if' checks)
        for (int i = 0; i < 10; i++)
        {
            // If GetString returns "Grass", put an icon in the cell instead of just the text
            if (results->getString((i + 1)) == "Grass")
            {
                    item = new QTableWidgetItem;
                    icon = QIcon(grassStr.c_str());
                    item->setIcon(icon);
            }
            // If GetString returns "Rock", put an icon in the cell instead of just the text
            else if (results->getString((i + 1)) == "Rock")
            {
                    item = new QTableWidgetItem;
                    icon = QIcon(rockStr.c_str());
                    item->setIcon(icon);
            }
            else
            {
                // If the type look-up fails, just output the data to the cell
                item = new QTableWidgetItem(results->getString((i + 1)).c_str());
            }
            
            // Output current item to the specific cell
            ui.displayTable->setItem(count, i, item);
        }
    }
}

解决方案

核心思路

  • 先提取字段字符串并清理首尾空白字符,消除不可见字符的干扰
  • 避免重复调用results->getString(),减少不必要的数据库交互开销

修改后的代码

#include <algorithm>
#include <cctype>
#include <string>

// 辅助函数:清理字符串首尾空白
std::string trim(const std::string &s) {
    auto start = s.begin();
    while (start != s.end() && std::isspace(*start)) {
        start++;
    }

    auto end = s.end();
    do {
        end--;
    } while (std::distance(start, end) > 0 && std::isspace(*end));

    return std::string(start, end + 1);
}

void QtPlayground::DisplayTable(sql::ResultSet* results)
{
    int count = 0;
    QTableWidgetItem* item;
    QIcon icon;

    while(results->next()) // 修正原代码的result拼写错误
    {
        ui.displayTable->insertRow(ui.displayTable->rowCount());
            
        for (int i = 0; i < 10; i++)
        {
            // 先取出当前字段值并清理首尾空白
            std::string currentStr = results->getString(i + 1);
            std::string trimmedStr = trim(currentStr);

            if (trimmedStr == "Grass")
            {
                item = new QTableWidgetItem;
                icon = QIcon(grassStr.c_str());
                item->setIcon(icon);
            }
            else if (trimmedStr == "Rock")
            {
                item = new QTableWidgetItem;
                icon = QIcon(rockStr.c_str());
                item->setIcon(icon);
            }
            else
            {
                // 这里可以选择输出原字符串或清理后的字符串,根据需求调整
                item = new QTableWidgetItem(currentStr.c_str());
            }
            
            ui.displayTable->setItem(count, i, item);
        }
        count++; // 原代码遗漏了count递增,会导致所有行数据覆盖第一行
    }
}

关键修改点

  1. 添加字符串清理函数:trim函数移除字符串首尾的所有空白字符(空格、换行、制表符等),确保匹配的准确性
  2. 提前提取字段值:只调用一次results->getString(),避免重复查询,同时方便后续处理
  3. 修正拼写错误:原代码中while(result->next())应为while(results->next())
  4. 补充count递增:原代码未在循环末尾递增count,会导致所有行数据都写入第一行,属于潜在bug

内容的提问来源于stack exchange,提问作者Jake Samz

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.07.26 05:43:26