## gda-sql

The gda-sql tool is an interactive command line tool to run SQL commands and work with dictionaries. This tool has got several options, use the `--help` option to list them all, or consult the man page (man gda-sql)

For example to list the installed providers, do:

    [prompt]> gda-sql-5.0 -L
    Provider    | Description                       | DSN parameters     | File
    ------------+-----------------------------------+--------------------+-----------------------------------------------
    SQLite      | Provider for SQLite databases     | DB_NAME,           | /usr/lib/libgda-5.0/providers/libgda-sqlite.so
                                                      DB_DIR,
                                                      EXTRA_FUNCTIONS
    Berkeley-DB | Provider for Berkeley databases   | FILE,              | /usr/lib/libgda-5.0/providers/libgda-bdb.so
                                                      DATABASE
    [...]

To list the configured data sources (DSN):

    [prompt]> gda-sql-5.0 -l
    DSN      | Provider   | Description  | Connection string      | Username
    ---------+------------+--------------+------------------------+---------
    Sales    | PostgreSQL | Sales        | DB_NAME=sales          |
    [...]

To run an interactive session, just specify a DSN or a connection string using the "<provider>://<connection string>" format (such as for example "Firebird://DATABASE=/path/to/dbfile.fdb"), or set the GDA_SQL_CNC environment variable to contain that string, and run the command without any argument, for example:

    [prompt]> gda-sql-5.0 PostgreSQL://DB_NAME=sales
    Welcome to the GDA SQL console, version 3.99.3

    Type: .copyright to show usage and distribution terms
          .? for help with internal commands
          .q (or CTRL-D) to quit
          (the '.' can be replaced by a '\')
          or any query terminated by a semicolon

    Opening connection 'c0' for: PostgreSQL://DB_NAME=sales
            Getting database schema information, this may take some time... Done.
    c0>

Note that the "c0" is the prompt for the first opened connection.

Several connections can be opened at once (with only one "active" at any given time), by specifying all of them on the command line, or by using the `\c` and `\close` to manage (open, change, close) the connections; the prompt contains the name of the current connection used. The following example opens two connections, one for the "pgsales" DSN, and one for the "PostgreSQL://DB_NAME=sales" connection string, and shows the usage of the ".c" command to list the opened connections:

    [prompt]> gda-sql-5.0 pgsales PostgreSQL://DB_NAME=sales
    Welcome to the GDA SQL console, version 3.99.3

    Type: .copyright to show usage and distribution terms
          .? for help with internal commands
          .q (or CTRL-D) to quit
          (the '.' can be replaced by a '\')
          or any query terminated by a semicolon

    Opening connection 'pgsales' for: pgsales
    Opening connection 'c1' for: PostgreSQL://DB_NAME=sales
            Getting database schema information, this may take some time... Done.
    c1> .c
                 List of opened connections
    Name    | Provider   | DSN or connection string | Username
    --------+------------+--------------------------+---------
    pgsales | PostgreSQL | pgsales                  |
    c1      | PostgreSQL | DB_NAME=sales            |
    (2 rows)

    c1>

Here is another sample session showing how to use variables in statements:

    [prompt]> gda-sql-5.0 -p SQLite -c "DB_DIR=.;DB_NAME=sales_test"
    Welcome to the GDA SQL console, version 3.1.2

    Type: \copyright to show usage and distribution terms
          \? for help with internal commands
          \q (or CTRL-D) to quit
          or any query terminated by a semicolon

    c0> select * from customers;
    id | name            | default_served_by | country | city
    ---+-----------------+-------------------+---------+-----
     2 | Ed Lamton       |                 4 | SP      | MDR
     3 | Lew Bonito      |                 1 | FR      | TLS
     4 | Mark Lawrencep  |              NULL | SP      | MDR
     9 | Greg Popoff     |                 2 | SP      | MDR
    10 | Vladimir Zirkov |                 4 | NULL    | NULL
    c0> \set the_id 9
    c0> select * from customers where id= ##the_id;
    id | name        | default_served_by | country | city
    ---+-------------+-------------------+---------+-----
     9 | Greg Popoff |                 2 | SP      | MDR
    c0>

See the Gda SQL console tool manual section for more information.