【问题标题】:QTableView export to .csv number of rows fetched is limited to only 256QTableView 导出到 .csv 提取的行数仅限于 256
【发布时间】:2023-12-12 22:32:01
【问题描述】:

我编写了一个 gui 程序,它将连接到 oracle 数据库并在输入查询后检索数据。检索到的数据显示在 QTableView 表模型小部件中。稍后将 QTableView 的结果导出到 .csv 文件

QString MyQuery = ui->lineQuery->text();
db.open();
QSqlQuery query(MyQuery,db);

if(query.exec())
{
    qDebug()<<QDateTime::currentDateTime()<<"QUERY SUCCESS ";
    ui->queryButton->setStyleSheet("QPushButton {background-color: rgb(0, 255, 0);}");

    this->model=new QSqlQueryModel();
    model->setQuery(MyQuery);
    ui->tableViewOra->setModel(model);

    QString textData;
    int rows=model->rowCount();
    int columns=model->columnCount();

    for (int i = 0; i < rows; i++)
    {
        for (int j = 0; j < columns; j++)
        {
            textData += model->data(model->index(i,j)).toString();
            textData += ", ";      // for .csv file format
        }
        textData += "\n";             // (optional: for new line segmentation)
    }

    QFile csvfile("/home/aj/ora_exported.csv");
    if(csvfile.open(QIODevice::WriteOnly|QIODevice::Truncate))
    {
        QTextStream out(&csvfile);
        out<<textData;
    }
    csvfile.close();
}

现在的问题是,在查询期间我观察到 QTableView 中有 543 行可见(这是正确的,因为有 543 个条目)。但是当导出 .csv 文件时,那里只有 256 行。

是否有任何我不知道的可变大小限制???

如果我想导出最多 1000 行的 .csv 文件,应该注意哪些变量???谢谢。

【问题讨论】:

  • qDebug() 是你的好朋友。
  • qDebug() 一直是我的好朋友,但在这种情况下,qDebug 的输出是-- QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection',旧连接已删除。 QDateTime("2014 年 12 月 9 日星期二 11:16:29") 查询成功
  • 你昨天的代码段对我来说效果很好,但对于一些特定的查询,行限制有点问题。
  • 尝试使用QSqlQueryModel::fetchMore 看看是否有帮助。
  • @Nejat- QTableView 模型,即 QSqlQueryModel 模型工作正常。我可以在 GUI QTableView 表上看到所有 543 行。但是当我导出到 .csv 时,只有 256 被导出。我不知道 fetchmore 对导出到 .csv 有何影响,你能解释一下吗,因为我认为 fetchmore 在从数据库中获取数据时有所帮助。

标签: c++ qt csv qt4 export-to-csv


【解决方案1】:

我认为当您第一次阅读model-&gt;rowCount() 时,模型并没有完全获取所有结果。虽然它会在稍后显示表格视图时获取更多信息,从而导致表格视图中的行完整显示。

在读取行数之前尝试使用QSqlQueryModel::fetchMore

while (model->canFetchMore())
   model->fetchMore();

int rows=model->rowCount();
int columns=model->columnCount();

[来自 Tay2510 的其他信息]:

你可以改变文件打开标志

if(csvfile.open(QIODevice::WriteOnly|QIODevice::Truncate))

if(csvfile.open(QIODevice::WriteOnly))

前者将覆盖同一个文件,而后者追加数据。

【讨论】:

    最近更新 更多