a_jihua 2024-03-31 20:24 采纳率: 0%
浏览 14

Qt查询mysql数据库int型字段数据查询为空

img


这仨都是int型

img


我数据库也是int型

img


但是查询出来为0,输出过查询结果也就是直接用query.value("article_id").toInt();输出也是0

img


数据没有0的
这是怎么回事能,有jrm遇到过吗,其他字段都是正确的

  • 写回答

1条回答 默认 最新

  • 专家-赤兔[在线] 全栈领域优质创作者 2024-03-31 20:24
    关注

    引自免费微信小程序:皆我百晓生

    在QT中,当我们尝试从MySQL数据库中查询int型字段(如表中的article表中的'article_id'字段)时,如果数据为空,可以考虑以下几个解决方法:

    1. 确保不存在NULL值:确保每一行都包含一个int类型的值,并且不会出现NULL关键字。我们可以通过以下SQL查询检查是否存在NULL值:
    SELECT COUNT(*) as count
    FROM articles
    WHERE article_id IS NOT NULL;
    

    这个查询会返回articles表中所有(article_id)不为NULL的记录数量。

    1. 使用IFNULL代替NULL:如果在某些情况下,我们确实需要在查询中处理NULL值,可以使用IFNULL函数,如下所示:
    SELECT IFNULL(article_id, 0) as int_value
    FROM articles;
    

    在这个查询中,如果article_id字段包含NULL值,则将NULL替换为0,并将结果存储为'int_value'字段。否则,直接将其存储为int_value字段。

    1. 避免在查询语句末尾使用NULL值:确保你在查询语句的末尾不应该包含NULL值,因为这部分可能会被视为一个错误。例如:
    SELECT column_name
    FROM table_name
    WHERE column_name IS NOT NULL;
    

    为了消除这种情况,可以在查询字符串前添加一个通配符?,表示询问表中的column_name字段是否为NULL,如下所示:

    SELECT column_name
    FROM table_name
    WHERE column_name IS NOT NULL ? column_name_2;
    

    在实际使用中,由于性能限制,最好只对所需字段进行检查,对于不需要的部分可以选择忽略。

    1. 引入database_value或db_value关键字:如果你的数据库支持其他数据类型如database_value或db_value,可以使用这些关键字来直接获取int值,例如:
    #include <qvariant>
    #include <database_value>
    
    int getArticleId(const std::database_value &val)
    {
        return static_cast<int>(val);
    }
    
    int main()
    {
        // 在设置数据库连接之前,先检查数据类型并设置相应的数据库值变量
        QSqlDatabase db;
        if (!db.open(QSqlDatabase::ReadOnly))
            throw QSqlDatabaseException("Failed to open database");
    
        QSqlQuery query(db);
    
        // 设置数据库连接参数
        connect(&query, SIGNAL(textChanged(const QString &, const QVariant &)),
                this, SLOT(updateTableStatus()));
    
        // 提示用户输入article_id,并获取其值
        uint articleId;
        qPrompt("Enter article_id: ");
        articleId = static_cast<uint>(query.value("article_id"));
    
        // 检查article_id是否为NULL
        if (articleId == 0)
            throw InvalidArgumentException("Invalid article_id value");
    
        // 更新数据库表的状态
        updateTableStatus();
    
        // 查询未关联的行
        while (query.next())
        {
            Article article = Article::create();
            article->article_id = getArticleId(query.value("article_id"));
            article->title = query.value("title");
            article->abstract = query.value("abstract");
            article->keywords = query.value("Keywords");
            article->keywords_en = query.value("Keywords_EN");
            article->section = query.value("section_id");
            article->comments = query.value("Comments");
            article->-Manuscript_Fulltext_Path = query.value("Manuscript_Fulltext_path");
            article->-Manuscript_PDF_PATH = query.value("Manuscript_PDF_path");
            article->-Manuscript_Attachments_Path = query.value("Manuscript_Attachments_path");
            article->-Manuscript_CopyRight_Agreement_Path = query.value("Manuscript_CopyRight_Agreement_path");
            article->-Manuscript_UPloadsPath = query.value("Manuscript_UPloads_path");
            article->-Manuscript_Serial_number = query.value("Manuscript_Serial_number");
            article->-Manuscript_Data_type = query.value("Manuscript_Data_type");
            article->Last_operation_time = query.value("Last_operation_time").toString();
            article->Preliminary_review_savepath = query.value("Preliminary_review_savepath").toString();
            article->Review_savepath = query.value("Review_savepath").toString();
            article->Final_review_savepath = query.value("Final_review_savepath").toString();
    
            emit MySql_error(QString::number(article.article_id));
    
            insertIntoTables(article);
        }
    
        // 关闭数据库连接
        db.close();
        return 0;
    }
    

    注意:上述代码假设article表和table_name表之间存在对应的连接关系,且表结构满足Int类型。如果你的表结构与上述示例不同,可能需要相应地调整查询语句、插入语句等部分。同时,也需根据具体数据库系统(如MySQL或SQLite)的API文档来正确实现对应的数据类型转换和引用。在实际开发中,通常使用数据库驱动库(如QSqlDriver或SQLite Driver)提供的QL語句解析功能来进行类似操作。此外,还应充分测试您的代码,确保查询及更新过程的正确性和效率。

    评论 编辑记录

报告相同问题?

问题事件

  • 创建了问题 3月31日

悬赏问题

  • ¥15 关于#linux#的问题:子进程C运行“ls –l”命令,且显示“C运行ls-l命令”(语言-c语言)
  • ¥15 vs code配置c语言遇到这个问题
  • ¥15 vscode调试编译找不到gcc,只有cl,但是检查cmd是对的,控制面板的路径也更改了
  • ¥20 access中怎么分割分别获取一下图中的值
  • ¥15 keras_tcn已经安装成功,还是显示ModuleNotFoundError: No module named 'keras_tcn'
  • ¥15 ENVI高分五号去除云层的方法
  • ¥15 16进制数据如何得到奇偶校验位
  • ¥15 求合并两个字节流VB6代码
  • ¥15 Pyqt 如何正确的关掉Qthread,并且释放其中的锁?
  • ¥30 网站服务器通过node.js部署了一个项目!前端访问失败