这仨都是int型
我数据库也是int型
但是查询出来为0,输出过查询结果也就是直接用query.value("article_id").toInt();输出也是0
数据没有0的
这是怎么回事能,有jrm遇到过吗,其他字段都是正确的
引自免费微信小程序:皆我百晓生
在QT中,当我们尝试从MySQL数据库中查询int型字段(如表中的article表中的'article_id'字段)时,如果数据为空,可以考虑以下几个解决方法:
NULL
关键字。我们可以通过以下SQL查询检查是否存在NULL值:SELECT COUNT(*) as count
FROM articles
WHERE article_id IS NOT NULL;
这个查询会返回articles表中所有(article_id)不为NULL的记录数量。
SELECT IFNULL(article_id, 0) as int_value
FROM articles;
在这个查询中,如果article_id字段包含NULL值,则将NULL替换为0,并将结果存储为'int_value'字段。否则,直接将其存储为int_value字段。
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;
在实际使用中,由于性能限制,最好只对所需字段进行检查,对于不需要的部分可以选择忽略。
#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語句解析功能来进行类似操作。此外,还应充分测试您的代码,确保查询及更新过程的正确性和效率。