Six command line Database Utilities:

Copyright (C) 2017 by Walter L. Chester.

I have noticed that there often is a need to dump database information into a CSV file.  Users then can use
Excel or a similar program to manipulate the data.  These utilities are easy to use, flexible, and fast.
There are 2 utilities for Oracle, 2 for Microsoft SQL, and 2 for SQLite.  They convert a table or SQL into a CSV file.

Oracle:
  OracleTableToCSV.exe       Converts an entire table into a CSV file.
  OracleQueryToCSV.exe       Converts an SQL query result into a CSV file.

Microsoft:
  MSTableToCSV.exe           Converts an entire table into a CSV file.
  MSQueryToCSV.exe           Converts an SQL query result into a CSV file.

SQLite:
  SqliteTableToCSV.exe       Converts an entire table into a CSV file.
  SqliteQueryToCSV.exe       Converts an SQL query result into a CSV file.

You can make batch (.BAT or .CMD) files to simplify frequent use.

The discussion below assumes all EXE files and batch files are located in the C:\DC folder.  Adjust accordingly.
----------------------------------
Here's how they work:
  OracleTableToCSV.exe       Converts an entire table into a CSV file.

    USAGE:  OracleTableToCSV <TableName> <DB_Instance> <UserName> <Password>
    CSV file name is Tablename_YYYY-MM-DD-hh-MM.csv
    For example:
      OracleTableToCSV my_table DB_Instance my_username my_password
    where DB_Instance is in your tnsnames.ora file.

    Assume EXE is in C:\DC folder, possible batch file, named C:\DC\o2c.bat:
      C:\DC\OracleTableToCSV %1 DB_Instance my_username my_password
    User types from any folder:
      C:\DC\o2c my_table
    which produces in the current folder a file in the format:
      SQLFile_YYYY-MM-DD-hh-MM.csv

    The program puts double-quotes around each field, which prevents problems with
    commas in the data messing up the columns in the CSV file.  The program replaces
    double-quotes with single-quotes in the data fields to prevent messing up the
    columns in the CSV file.

    The usual problems are:
       1. DB_Instance not set up in tnsnames.ora file.
       2. Database down.
       3. Connection issue.

    A table with a million rows takes longer than a few seconds, and the CSV file may
    need to be split up with a text editor if Excel cannot handle such a huge CSV file.
    I have tested a table with over a million rows successfully.

----------------------------------
  MSTableToCSV.exe           Converts an entire table into a CSV file.

    USAGE:  MSTableToCSV <TableName> <Server> <Database>
    CSV file name is Tablename_YYYY-MM-DD-hh-MM.csv
    For example:
      MSTableToCSV my_table myserver my_database

    Assume EXE is in C:\DC folder, possible batch file, named C:\DC\m2c.bat:
      C:\DC\MSTableToCSV %1 myserver my_database
    User types from any folder:
      C:\DC\m2c my_table
    which produces in the current folder a file in the format:
      SQLFile_YYYY-MM-DD-hh-MM.csv

    The program puts double-quotes around each field, which prevents problems with
    commas in the data messing up the columns in the CSV file.  The program replaces
    double-quotes with single-quotes in the data fields to prevent messing up the
    columns in the CSV file.

    The usual problems are:
       1. Connection set up correctly.
       2. Database down.
       3. Connection issue.

    A table with a million rows takes longer than a few seconds, and the CSV file may
    need to be split up with a text editor if Excel cannot handle such a huge CSV file.
    I have tested a table with over a million rows successfully.

----------------------------------
  SqliteTableToCSV.exe       Converts an entire table into a CSV file.

    USAGE:  SqliteTableToCSV <TableName> <DB_FileName>
    CSV file name is Tablename.csv
    For example:
      SqliteTableToCSV my_table DB_FileName

    Assume EXE is in C:\DC folder, possible batch file, named C:\DC\s2c.bat:
      C:\DC\SqliteTableToCSV %1 DB_FileName
    User types from any folder:
      C:\DC\s2c my_table
    which produces in the current folder a file in the format:
      my_table_YYYY-MM-DD-hh-MM.csv

    The program puts double-quotes around each field, which prevents problems with
    commas in the data messing up the columns in the CSV file.  The program replaces
    double-quotes with single-quotes in the data fields to prevent messing up the
    columns in the CSV file.

    The usual problems are:
       1. Wrong TableName or DB_FileName.

    A table with a million rows takes longer than a few seconds, and the CSV file may
    need to be split up with a text editor if Excel cannot handle such a huge CSV file.
    I have tested a table with over a million rows successfully.

----------------------------------
  OracleQueryToCSV.exe       Converts an SQL query result into a CSV file.

    USAGE:  OracleQueryToCSV <SQLFile> [DB_Instance] [UserName] [Password]
      CSV file name is SQLFile_YYYY-MM-DD-hh-MM.csv
      SQLFile without file extension on command line.
      SQLFile.hdr is file of column names, one per line.
      SQLFile.sql is used to produce output SQLFile.csv file.
      The SQL file and the HDR file must be in the same folder as the EXE file.
      The program hangs if the HDR and SQL files are not in sync.
      Use <CTRL>C and fix the files.

    For example, with SQL like this:
      SELECT F1 FirstName, (SELECT F2 FROM T2 WHERE F1=F2) LastName FROM ...
    Must have any of the following in the HDR file:
      FirstName                                      Equal number of columns...
      LastName
        OR
      FirstName                                      Fewer number of columns...
        OR
      LastName                                       More columns, some repeated...
      FirstName
      LastName

    I use all CAPS in the HDR file: it looks more professional.  Example files
      SQLFile.hdr
      SQLFile.sql
    are included.  They are text files.  It is VERY important that the SQL file and
    HDR files "sync up".  Every line in the HDR file must be either a field name or
    an alias in the SELECT statement of the SQL file.  The HDR file consists of each
    field, one per line.  If the database is up, and the UserName and Password are
    correct (a connection was made), the CSV file is made in a few seconds.  if the
    program takes more than a few seconds for a result set of a few thousand records,
    the usual problems are:
       1. DB_Instance not set up in tnsnames.ora file.
       2. Database down.
       3. SQL file and HDR files are NOT syncronizeded.
       4. Bad SQL in the SQL file (of course, you tested it first...).
       5. Connection issue.

    The program reports errors such as missing SQL or HDR files, or bad SQL, if
    possible.  All bets are off for issues 1, 2, and 5 above.

    The SQL file can end with a semicolon, or it can be left off: either way is fine.
    A table with a million rows takes longer than a few seconds, and the CSV file may
    need to be split up with a text editor if Excel cannot handle such a huge CSV file.
    I have tested a table with over a million rows successfully.  The program puts
    double-quotes around each field, which prevents problems with commas in the data.
    You will need to have your SQL replace double-quotes in the data, if they exist,
    as columns won't line up correctly if the data contains double-quotes.

    Make a batch file if you repeatedly run the same SQL: the CSV file includes a
    timestamp in the file name.  For example, a possible batch file, named C:\DC\oq2c.bat:
      C:\DC\OracleQueryToCSV %1 SQLFile DB_Instance my_username my_password
    User types from any folder:
      C:\DC\oq2c SQLFile
    which produces in the current folder a file in the format:
      SQLFile_YYYY-MM-DD-hh-MM.csv
    Of course, this only works if the SQL file and HDR files are present.

----------------------------------
  MSQueryToCSV.exe       Converts an SQL query result into a CSV file.

  USAGE:  MSQueryToCSV <SQLFile> [Server] [Database]
    CSV file name is SQLFile_YYYY-MM-DD-hh-MM.csv
    SQLFile without file extension on command line.
    SQLFile.hdr is file of column names, one per line.
    SQLFile.sql is used to produce output SQLFile.csv file.
    The SQL file and the HDR file must be in the same folder as the EXE file.
    The program hangs if the HDR and SQL files are not in sync.
    Use <CTRL>C and fix the files.

  For example, with SQL like this:
    SELECT F1 FirstName, (SELECT F2 FROM T2 WHERE F1=F2) LastName FROM ...
  Must have any of the following in the HDR file:
    FirstName                                      Equal number of columns...
    LastName
      OR
    FirstName                                      Fewer number of columns...
      OR
    LastName                                       More columns, some repeated...
    FirstName
    LastName

  Everything in the above section for OracleQueryToCSV.exe also applies here: just
  change refrences from Oracle to Microsoft SQL.

----------------------------------
  SqliteQueryToCSV.exe   Converts an SQL query result into a CSV file.

  USAGE:  SqliteQueryToCSV <SQLFile> <DB_FileName>
    CSV file name is SQLFile_YYYY-MM-DD-hh-MM.csv
    SQLFile without file extension on command line.
    SQLFile.hdr is file of column names, one per line.
    SQLFile.sql is used to produce output SQLFile.csv file.
    The SQL file and the HDR file must be in the same folder as the EXE file.
    The program hangs if the HDR and SQL files are not in sync.
    Use <CTRL>C and fix the files.

  For example, with SQL like this:
    SELECT F1 FirstName, (SELECT F2 FROM T2 WHERE F1=F2) LastName FROM ...
  Must have any of the following in the HDR file:
    FirstName                                      Equal number of columns...
    LastName
      OR
    FirstName                                      Fewer number of columns...
      OR
    LastName                                       More columns, some repeated...
    FirstName
    LastName

  Everything in the above section for OracleQueryToCSV.exe also applies here: just
  change refrences from Oracle to SQLite.

  A sample database is SqliteSamp.db.  Sample SQL file is The_SQLFile.sql.  Sample
  HDR file is The_SQLFile.hdr.  Thus, a sample run would be:
    SqliteQueryToCSV The_SQLFile SqliteSamp.db
  which produces this CSV file, for example:
    THE_SQLFILE_2017-07-28-09-01.csv
