- Prints vector map attributes.
, attribute table
v.db.select [-rcef] map=name [layer=string] [columns=name[,name,...]] [where=sql_query] [group=string] format=string [separator=character] [vertical_separator=character] [null_value=string] [file=name] [--overwrite] [--help] [--verbose] [--quiet] [--ui]
- Print minimal region extent of selected vector features instead of attributes
- Do not include column names in output
- Escape newline and backslash characters
- Exclude attributes not linked to features
- Allow output files to overwrite existing files
- Print usage summary
- Verbose module output
- Quiet module output
- Force launching GUI dialog
- map=name [required]
- Name of vector map
- Or data source for direct OGR access
- Layer number or name
- Vector features can have category values in different layers. This number determines which layer to use. When used with direct OGR access this is the layer name.
- Default: 1
- Name of attribute column(s)
- WHERE conditions of SQL statement without 'where' keyword
- Example: income < 1000 and population >= 10000
- GROUP BY conditions of SQL statement without 'group by' keyword
- format=string [required]
- Output format
- Options: plain, csv, json, vertical
- Default: plain
- plain: Configurable plain text output
- csv: CSV (Comma Separated Values)
- vertical: Plain text vertical output (instead of horizontal)
- Field separator
- Special characters: pipe, comma, space, tab, newline
- Output vertical record separator
- Special characters: pipe, comma, space, tab, newline
- String representing NULL value
- Name for output file (if omitted or "-" output to stdout)
prints attributes of a vector map from one or several
user selected attribute table columns.
Four different formats can be used depending on the circumstances
using the format
option: plain text, CSV, JSON, and vertical
The plain text is the default output which is most suitable for reading by humans,
e.g., when working in the command line or obtaining specific values from the attribute
table using the v.db.select
The individual fields (attribute values) are separated by a pipe (|)
which can be customized using the separator option.
The records (rows) are separated by newlines.
Example with a pipe as a separator (the default):
When escaping is enabled, the following characters in the fields are escaped:
), carriage return (\r
), line feed (\n
), form feed (\f
), and backslash (\b
No quoting or escaping is performed by default, so if these characters are in
the output, they look just like the separators.
This is usually not a problem for humans looking at the output to get a general idea
about query result or attribute table content.
Consequently, this format is not recommended for computers, e.g., for reading attribute
data in Python scripts.
It works for further parsing in limited cases when the values don't contain separators
or when the separators are set to one of the escaped characters.
CSV (comma-separated values) has many variations. This module by default produces
CSV with comma (,
) as the field separator (delimiter). All text fields
(based on the type) are quoted with double quotes. Double quotes in fields are
represented as two double quotes. Newline characters in the fields are present
as-is in the output. Header is included by default containing column names.
All full CSV parsers such as the ones in LibreOffice or Python are able to parse this
format when configured to the above specification.
Example with default settings:
If desired, the separator can be customized and escaping can be enabled
with the same characters being escaped as for the plain text.
Notably, newlines and tabs are escaped, double quotes are not, and the separator
is not escaped either (unless it is a tab).
However, the format is guaranteed only for the commonly used separators
such as comma, semicolon, pipe, and tab.
Note that using multi-character separator is allowed, but not recommended
as it is not generally supported by CSV readers.
CSV is the recommended format for further use in another analytical applications,
especially for use with spreadsheet applications. For scripting, it is advantageous
when tabular data is needed (rather than key-value pairs).
the specification so it is readily readable by JSON parsers.
The standard JSON escapes are performed (backslash, carriage return, line feed,
tabulator, form feed, backslash, and double quote) for string values.
Numbers in the database such as integers and doubles are represented as numbers,
while texts (TEXT, VARCHAR, etc.) and dates in the database are represented
as strings in JSON. NULL values in database are represented as JSON null
Indentation and newlines in the output are minimal and not guaranteed.
Records which are the result of the query are stored under key records
as an array (list) of objects (collections of key-value pairs).
The keys for attributes are lowercase or uppercase depending on how
the columns were defined in the database.
Example with added indentation (note that booleans are not directly supported;
here, an attribute is a string with value no):
JSON is the recommended format for reading the data in Python
and for any uses and environments where convenient access to individual values
is desired and JSON parser is available.
Vertical plain text
In the vertical plain text format, each value is on a single line
and is preceded by the name of the attribute (column) which is
separated by separator. The individual records can be separated by
the vertical separator (vertical_separator
Example with (horizontal) separator = and vertical separator newline:
Newline is automatically added after a vertical separator unless it is a newline
which allows for separating the records, e.g., by multiple dashes.
The escaping (-e
) need to should be enabled in case the output
is meant for reading by a computer rather than just as a data overview
for humans. Escaping will ensure that values with newlines will be
contained to a single line.
This format is for special uses in scripting, for example, in combination
option set to one column only and escaping (-e
and no column names flags (-c
). It is also advantageous when you
need implement the parsing yourself.
CSV and JSON were added in version 8.0 as new primary formats for further
consumption by scripts and other applications.
Escaping of plain and vertical formats was extended from just backslash
and newlines to all escapes from JSON except for double quote character.
All examples are based on the North Carolina sample dataset.
Note: multiple columns can be specified as comma separated list.
v.db.select map=roadsmajor column=ROAD_NAME
v.db.select -r map=roadsmajor where="ROAD_NAME = 'NC-98'"
v.db.select geonames_wake where="ALTERNATEN IS NULL"
8|4498303|West Raleigh|West Raleigh||P|PPL|US||NC|338759|123|...
v.db.select geonames_wake where="ALTERNATEN IS NOT NULL"
31299|4487056|Raleigh-Durham Airport|Raleigh-Durham Airport|...
v.db.select map=roadsmajor columns=ROAD_NAME group=ROAD_NAME
It is also possible to combine with where
v.db.select map=roadsmajor columns=ROAD_NAME,MULTILANE group=ROAD_NAME where='ROAD_NAME is not null'
It can also use more columns in group
v.db.select map=roadsmajor columns=ROAD_NAME,MULTILANE group=ROAD_NAME,MULTILANE where='ROAD_NAME is not null'
package in the standard Python library can load
a JSON string obtained as output from the v.db.select
through the read_command
import grass.script as gs
text = gs.read_command("v.db.select", map="roadsmajor", format="json")
data = json.loads(text)
for row in data["records"]:
GRASS SQL interface
Radim Blazek, ITC-Irst, Trento, Italy
Minimal region extent added by Martin Landa,
FBK-irst (formerly ITC-irst), Trento, Italy
Group option added by Luca Delucchi,
Fondazione Edmund Mach, Trento, Italy
Huidae Cho (JSON output, escaping and features-only flags)
Vaclav Petras (true CSV output, format option and documentation)
v.db.select source code
Latest change: Thursday Feb 03 11:10:06 2022 in commit: 547ff44e6aecfb4c9cbf6a4717fc14e521bec0be
Main index |
Vector index |
Topics index |
Keywords index |
Graphical index |
GRASS Development Team,
GRASS GIS 8.2.0 Reference Manual