Database Grid object Query export to CSV file

Hi all guys.

Is there a good way to export all rows from a grid qeury to a CSV file?

I use SQL database with have 7 Collumns 6 of them are floats and 1 string.

can be up to 1-200 rows to export...
  • You might be able to do this using the grid 'Export' functionality configured within the Grid object Advanced Dialog. There you specify a class tag (to receive grid row data) and an export trigger tag.

    Using a looping process, either in VBScript or the Built-in Scripting, you should be able to loop through the grid rows and export each row using the FileWrite() function.

    The total number of rows to process is provided by a tag you place in the 'Number of Rows:' field. You would use this as your looping limit, adjusting the 'Row Number:' tag to progress through the grid.

    This is mentioned briefly in the Technical Reference under Grid object->Advanced dialog.

    Hope this is of some use.

    Regards,
    Greg Shearer
  • Hi greg, thanks for your reply.
    Ive tryid to fidure out how to do it in the way you mentioned it. but whitout luck.

    first cant get any values in my class tags.. My grid is with 9 columns and none off theese values are shown in my class tag... all other functions in the grid view works just fine.

    could you please help me further with these filewrite funcition? maybe you have a cript that allready do this function, that I could modify for my application?

    Happe easter.

    BR
    Kasper
  • Here is how I solved a similar problem. I also first tried traversing through a grid, much like the method suggested by Greg. But I was unable to get this to work. I could not get the grid to update proper apparently, so I would constantly get the values from the first row only... anywho; I finally decided to let the MySQL database create the CSV file. Which is way faster as well. A drawback to this method though, is that it works only at the PC where the MySQL database is running.

    Create an external database connection, and let us name it "DBx". Now you want to run an SQL statement which creates the CSV file. Something akin to this:

    $CSVFileName = "mycsv.txt"

    $SQLCmd = ""
    $SQLCmd = $SQLCmd + " Time_Stamp, Field1, Field2 "
    $SQLCmd = $SQLCmd + " INTO OUTFILE '/temp/" + $CSVFileName + "'"
    $SQLCmd = $SQLCmd + " FIELDS TERMINATED BY ';'"
    $SQLCmd = $SQLCmd + " ESCAPED BY '\\' "
    $SQLCmd = $SQLCmd + " LINES TERMINATED BY '\n' "
    $SQLCmd = $SQLCmd + " FROM Table1"
    $SQLCmd = $SQLCmd + " WHERE Time_Stamp >= '" + SqlDateTimeStart + "'"
    $SQLCmd = $SQLCmd + " AND Time_Stamp <= '" + SqlDateTimeStop + "'"

    // Run SQL-statement
    $DBExecute( "DBx" , $SQLCmd )

    This will create the following file C:\temp\mycsv.txt

    The completed SQL will look something like this:

    SELECT Time_Stamp, Field1, Field2
    INTO OUTFILE '/temp/mycsv.csv'
    FIELDS TERMINATED BY ','
    ESCAPED BY '\\'
    LINES TERMINATED BY '\n'
    FROM Table1
    WHERE Time_Stamp >= '2013-12-24 09:30:00'
    AND Time_Stamp <= '2013-12-24 21:30:00';

    Check MySQL resources for additional parameters for this approach.