QTableView export to .csv number of rows fetched is limited to only 256
Asked Answered
C

1

2

I wrote a gui program which will connect to an oracle db an retrieve data after a query is typed. the retrieved data is shown in a QTableView table model widget. and later the result of the QTableView is exported to a .csv file

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();
}

now the problem is, during a query I observed there are 543 rows visible in the QTableView (which is correct cause there are 543 entries). But when the .csv file is exported, only 256 rows are there.

Is there any variable size constraint that I am unaware about ???

Which variables should be taken care of if I want to export .csv files of upto 1000 rows approx ??? thanks.

Coinsurance answered 9/12, 2014 at 5:30 Comment(6)
qDebug() is your good friend.Charpoy
qDebug() has been my good friend but during this situation the output of qDebug is-- QSqlDatabasePrivate::addDatabase: duplicate connection name 'qt_sql_default_connection', old connection removed. QDateTime("Tue Dec 9 11:16:29 2014") QUERY SUCCESSCoinsurance
your code segment of yesterday has worked wonders for me but for a few particular queries the row limitation is a little concern.Coinsurance
Try to use QSqlQueryModel::fetchMore and see if it helps.Poltroonery
@Nejat- the QTableView model i.e. the QSqlQueryModel model is working fine. I can see all the 543 rows on the GUI QTableView table. But when I export to .csv only 256 is getting exported. I dont know how fetchmore will have an effect on exporting to .csv, can you explain cuz I thougth fetchmore helped during fetching data from DB.Coinsurance
@AmarjitBiswas That's not what I meant. The information you provided was far from enough for other people to locate the problem. You should first use qDebug() to check 1. if rows got from model really equal to 543 2. if the loop for writing files really processed and write EXACTLY 543 rows. Besides, it could also be a problem that successive calls of query.exec() render the program unable to finish writing files in time.Charpoy
P
4

I think when you first read model->rowCount() the model has not fetched all the results completely. Although it will fetch more later when table view is displayed resulting in a full display of rows in the table view.

Try to use QSqlQueryModel::fetchMore before reading the row count :

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

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

[Additional information from Tay2510]:

You could just change the file open flag

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

to

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

The former will overwrite the same file while the latter append data on it.

Poltroonery answered 9/12, 2014 at 6:8 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.