Skip to content

Commit

Permalink
Added --table arg to csvsql and updates docs. #86 #126.
Browse files Browse the repository at this point in the history
  • Loading branch information
onyxfish committed Oct 14, 2011
1 parent c35d874 commit 6d3358b
Show file tree
Hide file tree
Showing 2 changed files with 32 additions and 12 deletions.
12 changes: 9 additions & 3 deletions csvkit/utilities/csvsql.py
@@ -1,13 +1,14 @@
#!/usr/bin/env python

import os
import sys

from csvkit import sql
from csvkit import table
from csvkit.cli import CSVKitUtility

class CSVSQL(CSVKitUtility):
description = 'Generate a SQL CREATE TABLE statement for a CSV file.'
description = 'Generate SQL statements for a CSV file or create execute those statements directly on a database.'
override_flags = 'l'

def add_arguments(self):
Expand All @@ -19,13 +20,17 @@ def add_arguments(self):
help='If present, a sqlalchemy connection string to use to directly execute generated SQL on a database.')
self.argparser.add_argument('--insert', dest='insert', action='store_true',
help='In addition to creating the table, also insert the data into the table. Only valid when --db is specified.')
self.argparser.add_argument('--table', dest='table_name',
help='Specify a name for the table to be created. If omitted, the filename (minus extension) will be used.')

def main(self):
if self.args.file.name != '<stdin>':
if self.args.table_name:
table_name = self.args.table_name
elif self.args.file != sys.stdin:
# Use filename as table name
table_name = os.path.splitext(os.path.split(self.args.file.name)[1])[0]
else:
table_name = 'csvsql_table'
self.argparser.error('The --table argument is required when providing data over STDIN.')

if self.args.dialect and self.args.connection_string:
self.argparser.error('The --dialect option is only valid when --db is not specified.')
Expand Down Expand Up @@ -60,3 +65,4 @@ def main(self):
if __name__ == '__main__':
utility = CSVSQL()
utility.main()

32 changes: 23 additions & 9 deletions docs/scripts/csvsql.rst
Expand Up @@ -5,13 +5,18 @@ csvsql
Description
===========

Generates a SQL "CREATE TABLE" statement for a given CSV file. Supports a variety of SQL dialects::
Generate SQL statements for a CSV file or create execute those statements directly on a database. In the latter case supports both creating tables and inserting data.::

usage: csvsql [-h] [-d DELIMITER] [-t] [-q QUOTECHAR] [-u {0,1,2,3}] [-b]
[-p` ESCAPECHAR] [-e ENCODING]
[-p ESCAPECHAR] [-z MAXFIELDSIZE] [-e ENCODING] [-v]
[-y SNIFFLIMIT]
[-i {access,sybase,sqlite,informix,firebird,mysql,oracle,maxdb,postgresql,mssql}]
[--db CONNECTION_STRING] [--insert]
[FILE]

Generate SQL statements for a CSV file or create execute those statements
directly on a database.

Generate a SQL CREATE TABLE statement for a CSV file.

positional arguments:
Expand All @@ -24,20 +29,29 @@ Generates a SQL "CREATE TABLE" statement for a given CSV file. Supports a variet
Limit CSV dialect sniffing to the specified number of
bytes.
-i {access,sybase,sqlite,informix,firebird,mysql,oracle,maxdb,postgresql,mssql}, --dialect {access,sybase,sqlite,informix,firebird,mysql,oracle,maxdb,postgresql,mssql}
Dialect of SQL to generate.
--inserts In additional to generating a CREATE TABLE statement,
also generate an INSERT statement for each row of
data.
Dialect of SQL to generate. Only valid when --db is
not specified.
--db CONNECTION_STRING
If present, a sqlalchemy connection string to use to
directly execute generated SQL on a database.
--insert In addition to creating the table, also insert the
data into the table. Only valid when --db is
specified.
--table TABLE_NAME Specify a name for the table to be created. If
omitted, the filename (minus extension) will be used.

Also see: :doc:`common_arguments`.

For information on connection strings and supported dialects refer to the `SQLAlchemy documentation <http://www.sqlalchemy.org/docs/dialects/>`_.

Examples
========

Generate a statement in the postgresql dialect::
Generate a statement in the PostgreSQL dialect::

$ csvsql -i postgresql examples/realdata/FY09_EDU_Recipients_by_State.csv

Generate create and insert statements for postgres, then actually run those statements to import all data into a test database::
Create a table and import data from the CSV directly into Postgres::

$ csvsql -i postgresql --inserts examples/realdata/FY09_EDU_Recipients_by_State.csv | psql -d test
$ createdb test
$ csvsql --db postgresql:///test --name fy09 --insert examples/realdata/FY09_EDU_Recipients_by_State.csv

0 comments on commit 6d3358b

Please sign in to comment.