c++ - 从数据库中提取数据时使用 QVariant 的正确方法

标签 c++ qt

我最近开始使用 QT,但遇到了一个小问题。我似乎无法理解 QVariant 的工作原理。我一直在浏览帮助并在线查看,但它只是没有沉没。在我的项目中,我试图用数据库中的制造商列表填充一个组合框。我已经打开数据库并取出条目,但我将它们全部重命名为 manValue。现在我明白了为什么会这样,问题是我不明白如何正确使用 QVariant 来获得我想要的结果。最初我认为“manValue”将成为保存数据库实际值的字符串的标识符,但它从数据库中读取值并确保它不为空,然后重命名它。在我为 QVariant 分配任何属性并将其分配我从数据库收到的文本之前,我已经尝试制作一个字符串,然后在 manValue 所在的位置插入该字符串,但仍然没有运气。任何帮助将不胜感激。抱歉,我知道这很简单,我只是个菜鸟,帮助文档经常让我感到困惑。

QSqlDatabase db = QSqlDatabase::addDatabase("QODBC");
db.setHostName("LOCALHOST\\TestERPServer");
db.setDatabaseName("TestERPConnection");

if (db.open())
{
    QMessageBox::information(this,"Connected","Connection to the Database was Established\n"
                             "\nStatus: Connected");

    QSqlQuery mfrQry;

    if (mfrQry.exec("SELECT * FROM erp_data.manufacturers;"))
    {
        if (mfrQry.value(1) == "")
        {
            QMessageBox::information(this,"No Connection","Nothing in the Manufacturer Database\n"
                                     "\nError: " + db.lastError().text());
        }
        else
        {
            while (mfrQry.next())
            {
                ui->mfrComboBox->addItem("manValue",QVariant(mfrQry.value(1)));
            }
        }
    }
    else
    {
        QMessageBox::information(this,"No Connection","Connection to the Manufacturer Database could not be Established\n"
                                 "\nError: " + db.lastError().text());
    }
}
else
{
    QMessageBox::information(this,"No Connection","Connection to the Database could not be Established\n"
                             "\nError: " + db.lastError().text());
}

最佳答案

所提供代码中的第一个问题与您操作 QSqlQuery 的方式有关。 .

Successfully executed SQL statements set the query's state to active so that isActive() returns true. Otherwise the query's state is set to inactive. In either case, when executing a new SQL statement, the query is positioned on an invalid record. An active query must be navigated to a valid record before values can be retrieved.

为了移动到有效记录,您必须使用以下方法之一:

一旦你在一个有效的记录上,你必须使用 value() 函数来获取你想要的列。返回值为 QVariant因此您需要使用 QVariant 提供的众多 toSomething 函数之一转换为所需的类型。

因此在您的情况下,代码应如下所示:

QSqlQuery mfrQry;

if (mfrQry.exec("SELECT * FROM erp_data.manufacturers;"))
{
   // This will loop through all records returned by the query
   while (mfrQry.next()) {
         //  mfrQry.value(COLID) returns a QVariant containing the data of the current 
         // record in column COLID. 
         // Using toString we convert it to String
         QString stringValue = mfrQry.value(COLID).toString();
         // Now handle the QString the way you want...
  }
}

关于c++ - 从数据库中提取数据时使用 QVariant 的正确方法,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/9794665/

相关文章:

c++ - 将用户输入的日期从 QString 转换为 QDate

qt - 如何确定 Qt 在运行时运行的平台?

c++ - 带有加密 std::string 的 sqlite 查询(无法识别的 token )

qt - 是否可以通过反转 Qt .qm 消息文件来生成源 xml?

c++ - Qimage:内存不足,返回空图像

c++ - 如何更新 QFileSystemModel 中的文件权限

c++ - 重叠 header 包含

c++ - [bsoncxx ]如何将 bsoncxx::document::element 附加到 bsoncxx::builder::basic::document?

c++ - 如何在 C++ 中的 boost::future 中添加回调

c++ - C++ 二进制代码能否通过 native C 接口(interface)变得可移植?有什么限制?