If you know the column names and types ahead of time, you can do something 
like this:

db.define_table('mydummytable',
                Field('field1'),
                ...,
                migrate=False)

rows = db.executesql(sql_statement, fields=db.mydummytable)

The purpose of the dummy table is just for use by executesql in order to 
parse the results into a DAL Rows object (be sure to set migrate=False, as 
you don't want to actually create that table in the database). When you 
pass a table object (or a list of field objects) as the "fields" argument 
to executesql, you get back a DAL Rows object, which can then be exported 
via CSV via:

rows.export_to_csv_file(file_object, ...)

or:

csv = rows.as_csv()

Anthony

On Saturday, March 28, 2015 at 12:14:44 PM UTC-4, Adam Scarlat wrote:
>
> Hi, 
>
> I use an external mysql database in my application and use the 
> executesql(str(query), as_dict=True)) to run queries on it.
> I want to have a download link for a csv files of different queries in my 
> application. If I were to work locally I would have used the following 
> mysql command:
>
> 'SELECT * FROM SOME_TABLE INTO OUTFILE '/tmp/test1.csv' FIELDS TERMINATED 
> BY ',' ENCLOSED BY '"' LINES TERMINATED BY '\n''
>
> In my application a user can select certain criteria, a query is built in 
> the controller according to the criteria and is executed with the 
> executesql() method
> which returns a dictionary like object. 
>
> My question is: is there a way to make a download link in the view to 
> download a csv file of results returned by the executesql() method?
>
> Thank you!
> Adam
>

-- 
Resources:
- http://web2py.com
- http://web2py.com/book (Documentation)
- http://github.com/web2py/web2py (Source code)
- https://code.google.com/p/web2py/issues/list (Report Issues)
--- 
You received this message because you are subscribed to the Google Groups 
"web2py-users" group.
To unsubscribe from this group and stop receiving emails from it, send an email 
to [email protected].
For more options, visit https://groups.google.com/d/optout.

Reply via email to