Query to CSV

How i can made on Web App to run the query and download it
COPY (SELECT * FROM tracks WHERE genre_id = 6) TO '/Users/dave/Downloads/blues_tracks.csv' DELIMITER ',' CSV HEADER;


I thought there was a sample program for this but I couldn’t find it. Here’s some sample (untested) code that I simplified from my web app that does this. You need to read the data and format it appropriately. Then tell the client side (browser) to download it. Note that variable db needs to be declared and connected to your specific database.

' Create a CSV file and download it
Dim rs As RowSet, sql As String
Dim Output As String, Header As String
Dim ExportFile As WebFile

Header = "Firstname, Lastname"

rs = db.SelectSQL("SELECT Firstname, Lastname FROM mytable;")

If rs <> Nil Then
  Output = Header + EndOfLine
  While Not rs.AfterLastRow
    Output = Output + """" + rs.Column("Firstname").StringValue) + """,""" + rs.Column("Lastname").StringValue) + """," + EndOfLine
  // Database error, could not retrieve data

ExportFile = New WebFile
ExportFile.MimeType = "text/plain"
ExportFile.ForceDownload = True
ExportFile.FileName = "names.csv"
ExportFile.Data = Output
' Firefox requires InNewWindow=True, also popup blockers may interfere
Session.GoToURL(ExportFile.URL, True)
1 Like

that is big help