Source code for uw.dbtools

'''General-purpose utility routines related to the database.

This module is for general-purpose utility routines that relate to the
Postgres database.
'''

from io import StringIO

[docs]def copy_to_csv (cursor, query, header=True): '''Run a query and return the result as a CSV document. :param cursor: DB connection cursor. :param query: SQL query to run. ''' result = StringIO () params = { 'query': query, 'header': ' header' if header else '', } cursor.copy_expert ("copy (%(query)s) to stdout csv%(header)s" % params, result) return result