Skip to content

db.select

Selects data from attribute table.

Performs SQL query statement(s).

db.select [-cdvt] [sql=sql_query] [input=name] [table=name] [driver=name] [database=name] [separator=character] [vertical_separator=character] [null_value=string] [output=name] [--overwrite] [--verbose] [--quiet] [--qq] [--ui]

Example:

db.select

grass.script.run_command("db.select", sql=None, input=None, table=None, driver="sqlite", database="$GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db", separator="pipe", vertical_separator=None, null_value=None, output=None, flags=None, overwrite=False, verbose=False, quiet=False, superquiet=False)

Example:

gs.run_command("db.select")

Parameters

sql=sql_query
    SQL SELECT statement
    Example: select * from towns where population > 10000
input=name
    Name of file containing SQL select statement(s)
    '-' for standard input
table=name
    Name of table to query
driver=name
    Name of database driver
    Allowed values: dbf, ogr, sqlite
    Default: sqlite
database=name
    Name of database
    Default: $GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db
separator=character
    Field separator
    Special characters: pipe, comma, space, tab, newline
    Default: pipe
vertical_separator=character
    Vertical record separator (requires -v flag)
    Special characters: pipe, comma, space, tab, newline
null_value=string
    String representing NULL value
output=name
    Name for output file (if omitted or "-" output to stdout)
-c
    Do not include column names in output
-d
    Describe query only (don't run it)
-v
    Vertical output (instead of horizontal)
-t
    Only test query, do not execute
--overwrite
    Allow output files to overwrite existing files
--help
    Print usage summary
--verbose
    Verbose module output
--quiet
    Quiet module output
--qq
    Very quiet module output
--ui
    Force launching GUI dialog

sql : str, optional
    SQL SELECT statement
    Example: select * from towns where population > 10000
    Used as: sql_query
input : str, optional
    Name of file containing SQL select statement(s)
    '-' for standard input
    Used as: input, file, name
table : str, optional
    Name of table to query
    Used as: input, dbtable, name
driver : str, optional
    Name of database driver
    Used as: input, dbdriver, name
    Allowed values: dbf, ogr, sqlite
    Default: sqlite
database : str, optional
    Name of database
    Used as: input, dbname, name
    Default: $GISDBASE/$LOCATION_NAME/$MAPSET/sqlite/sqlite.db
separator : str, optional
    Field separator
    Special characters: pipe, comma, space, tab, newline
    Used as: input, separator, character
    Default: pipe
vertical_separator : str, optional
    Vertical record separator (requires -v flag)
    Special characters: pipe, comma, space, tab, newline
    Used as: input, separator, character
null_value : str, optional
    String representing NULL value
    Used as: string
output : str, optional
    Name for output file (if omitted or "-" output to stdout)
    Used as: output, file, name
flags : str, optional
    Allowed values: c, d, v, t
    c
        Do not include column names in output
    d
        Describe query only (don't run it)
    v
        Vertical output (instead of horizontal)
    t
        Only test query, do not execute
overwrite: bool, optional
    Allow output files to overwrite existing files
    Default: False
verbose: bool, optional
    Verbose module output
    Default: False
quiet: bool, optional
    Quiet module output
    Default: False
superquiet: bool, optional
    Very quiet module output
    Default: False

DESCRIPTION

db.select prints result of selection from database based on SQL statement read from input file or from standard input to standard output. Each individual query has to be written on one single line and different queries have to be written on separate lines.

NOTE

If parameters for database connection are already set with db.connect, they are taken as default values and do not need to be specified each time. Output will be displayed to standard output or can be directed to a file (option output).

EXAMPLES

Basic usage

db.select sql="select * from roads"

or

echo "select * from roads" | db.select input=-

or

db.select input=file.sql

or

cat file.sql | db.select input=-

Select all from table roads:

db.select -c driver=odbc database=mydb table=hospitals \
          input=file.sql output=result.csv

Select some string attribute, exclude others:

db.select sql="SELECT * FROM archsites WHERE str1 <> 'No Name'"

Select some string attribute with ZERO length:

db.select sql="SELECT * FROM archsites WHERE str1 IS NULL"

Select coordinates from PostGIS table:

db.select sql="SELECT x(geo),y(geo) FROM localizzazione"

Execute multiple SQL statements

cat file.sql
SELECT * FROM busstopsall WHERE cat = 1
SELECT cat FROM busstopsall WHERE cat > 4 AND cat < 8

db.select input=file.sql

Count number of cases falling into same position

When multiple observation have the spatial coordinates, they can still be counted (if needed, coordinates can be uploaded to the attribute table by v.to.db:

db.select sql="SELECT long,lat,site_id,department,obs,COUNT(long) as count_cases \
               FROM diseases GROUP BY long,lat"

SEE ALSO

db.connect, db.describe, db.drivers, db.droptable, db.execute, db.login, db.tables

GRASS SQL interface

AUTHORS

Original author unknown (probably CERL)
Modifications by Radim Blazek, ITC-Irst, Trento, Italy
Support for multiple statements by Martin Landa, Czech Technical University in Prague

SOURCE CODE

Available at: db.select source code (history)
Latest change: Friday Feb 07 19:16:09 2025 in commit a82a39f