Over a million developers have joined DZone.

SQL Export In Zope

·
Takes the result of a Zope SQL query, and an ordered list of field names, and outputs a CSV file as the resultant web page.  Should be called like this:

return context.sql.ordered-sql2csv(context.sql.exportOrders().dictionaries(), ["fullname","email","phone","address","town","postcode","children","adults","comments"])



## Script (Python) "ordered-sql2csv"
##bind container=container
##bind context=context
##bind namespace=
##bind script=script
##bind subpath=traverse_subpath
##parameters=sql, fo
##title=
##
container.REQUEST.RESPONSE.setHeader("Content-Type", "text/csv", 0)
sep = ""
for dk in fo:
  container.REQUEST.RESPONSE.write(sep + dk)
  sep = ","
sep = "\n"

for rec in sql:
  for i in range(0,len(fo)):
    data=rec[fo[i]]
    dl = str(data).split(',')
    data1 = " ".join(dl)
    container.REQUEST.RESPONSE.write(sep + str(data1))
    sep = ","
  sep="\n"
Topics:

Opinions expressed by DZone contributors are their own.

The best of DZone straight to your inbox.

SEE AN EXAMPLE
Please provide a valid email address.

Thanks for subscribing!

Awesome! Check your inbox to verify your email so you can start receiving the latest in tech news and resources.
Subscribe

{{ parent.title || parent.header.title}}

{{ parent.tldr }}

{{ parent.urlSource.name }}