From 6d3358bd8dca8fdbda8b674f422a5ccc505c773e Mon Sep 17 00:00:00 2001 From: Christopher Groskopf Date: Fri, 14 Oct 2011 11:24:54 -0500 Subject: [PATCH] Added --table arg to csvsql and updates docs. #86 #126. --- csvkit/utilities/csvsql.py | 12 +++++++++--- docs/scripts/csvsql.rst | 32 +++++++++++++++++++++++--------- 2 files changed, 32 insertions(+), 12 deletions(-) diff --git a/csvkit/utilities/csvsql.py b/csvkit/utilities/csvsql.py index 5958ff987..801a9a369 100644 --- a/csvkit/utilities/csvsql.py +++ b/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): @@ -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 != '': + 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.') @@ -60,3 +65,4 @@ def main(self): if __name__ == '__main__': utility = CSVSQL() utility.main() + diff --git a/docs/scripts/csvsql.rst b/docs/scripts/csvsql.rst index 4be6b89f8..4e6e5918d 100644 --- a/docs/scripts/csvsql.rst +++ b/docs/scripts/csvsql.rst @@ -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: @@ -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 `_. + 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