jeudi 19 janvier 2012

How can I write the results from a QueryRun object to a CSV file?

you can write to text file using the TextIo class:
#FileTextIo textIo = new TextIO("xyz123.txt", #IO_Write);
;
textIo.outFieldDelimiter(';');
while (queryRun.next())
{
//... get data
textIo.write(yourTable.AccountNumber, yourTable.AccountName);
}

If you want to implement a generic solution, you can iterate through datasources/fields like this:

setPrefix("List query fields");
for (i = 1; i <= query.dataSourceCount(); i++)
{
qbds = query.dataSourceNo(i);
setPrefix(tableId2name(qbds.table()));

for (j = 1; j <= qbds.fields().fieldCount(); j++)
{
fieldId = qbds.fields().field(j);
info(new SysDictField(qbds.table(), fieldId).name());
}
}

When you have TableId and FieldId, you can write data by the following code:

common = queryRun.get(tableId);
textIo.write(common.(fieldId));

You can build a container and write it down by writeExp() when it is complete.

Aucun commentaire:

Enregistrer un commentaire