Over a million developers have joined DZone.
{{announcement.body}}
{{announcement.title}}

SQL Export In Zope

DZone's Guide to

SQL Export In Zope

· ·
Free Resource
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.

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

{{ parent.tldr }}

{{ parent.urlSource.name }}