.TH v.db.select 1grass "" "GRASS 8.2.1" "GRASS GIS User's Manual" .SH NAME \fI\fBv.db.select\fR\fR \- Prints vector map attributes. .SH KEYWORDS vector, attribute table, database, SQL, export .SH SYNOPSIS \fBv.db.select\fR .br \fBv.db.select \-\-help\fR .br \fBv.db.select\fR [\-\fBrcef\fR] \fBmap\fR=\fIname\fR [\fBlayer\fR=\fIstring\fR] [\fBcolumns\fR=\fIname\fR[,\fIname\fR,...]] [\fBwhere\fR=\fIsql_query\fR] [\fBgroup\fR=\fIstring\fR] \fBformat\fR=\fIstring\fR [\fBseparator\fR=\fIcharacter\fR] [\fBvertical_separator\fR=\fIcharacter\fR] [\fBnull_value\fR=\fIstring\fR] [\fBfile\fR=\fIname\fR] [\-\-\fBoverwrite\fR] [\-\-\fBhelp\fR] [\-\-\fBverbose\fR] [\-\-\fBquiet\fR] [\-\-\fBui\fR] .SS Flags: .IP "\fB\-r\fR" 4m .br Print minimal region extent of selected vector features instead of attributes .IP "\fB\-c\fR" 4m .br Do not include column names in output .IP "\fB\-e\fR" 4m .br Escape newline and backslash characters .IP "\fB\-f\fR" 4m .br Exclude attributes not linked to features .IP "\fB\-\-overwrite\fR" 4m .br Allow output files to overwrite existing files .IP "\fB\-\-help\fR" 4m .br Print usage summary .IP "\fB\-\-verbose\fR" 4m .br Verbose module output .IP "\fB\-\-quiet\fR" 4m .br Quiet module output .IP "\fB\-\-ui\fR" 4m .br Force launching GUI dialog .SS Parameters: .IP "\fBmap\fR=\fIname\fR \fB[required]\fR" 4m .br Name of vector map .br Or data source for direct OGR access .IP "\fBlayer\fR=\fIstring\fR" 4m .br Layer number or name .br 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. .br Default: \fI1\fR .IP "\fBcolumns\fR=\fIname[,\fIname\fR,...]\fR" 4m .br Name of attribute column(s) .IP "\fBwhere\fR=\fIsql_query\fR" 4m .br WHERE conditions of SQL statement without \(cqwhere\(cq keyword .br Example: income < 1000 and population >= 10000 .IP "\fBgroup\fR=\fIstring\fR" 4m .br GROUP BY conditions of SQL statement without \(cqgroup by\(cq keyword .IP "\fBformat\fR=\fIstring\fR \fB[required]\fR" 4m .br Output format .br Options: \fIplain, csv, json, vertical\fR .br Default: \fIplain\fR .br \fBplain\fR: Configurable plain text output .br \fBcsv\fR: CSV (Comma Separated Values) .br \fBjson\fR: JSON (JavaScript Object Notation) .br \fBvertical\fR: Plain text vertical output (instead of horizontal) .IP "\fBseparator\fR=\fIcharacter\fR" 4m .br Field separator .br Special characters: pipe, comma, space, tab, newline .IP "\fBvertical_separator\fR=\fIcharacter\fR" 4m .br Output vertical record separator .br Special characters: pipe, comma, space, tab, newline .IP "\fBnull_value\fR=\fIstring\fR" 4m .br String representing NULL value .IP "\fBfile\fR=\fIname\fR" 4m .br Name for output file (if omitted or \(dq\-\(dq output to stdout) .SH DESCRIPTION \fIv.db.select\fR prints attributes of a vector map from one or several user selected attribute table columns. .SS Output formats Four different formats can be used depending on the circumstances using the \fBformat\fR option: plain text, CSV, JSON, and vertical plain text. .SS Plain text 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 \fIv.db.select\fR GUI dialog. .PP The individual fields (attribute values) are separated by a pipe (|) which can be customized using the \fBseparator\fR option. The records (rows) are separated by newlines. .PP Example with a pipe as a separator (the default): .br .nf \fC cat|road_name|multilane|year|length 1|NC\-50|no|2001|4825.369405 2|NC\-50|no|2002|14392.589058 3|NC\-98|no|2003|3212.981242 4|NC\-50|no|2004|13391.907552 \fR .fi When escaping is enabled, the following characters in the fields are escaped: backslash (\(rs\(rs), carriage return (\(rsr), line feed (\(rsn), tabulator (\(rst), form feed (\(rsf), and backslash (\(rsb). .PP 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. .PP 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\(cqt contain separators or when the separators are set to one of the escaped characters. .SS CSV 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. .PP All full CSV parsers such as the ones in LibreOffice or Python are able to parse this format when configured to the above specification. .PP Example with default settings: .br .nf \fC cat,road_name,multilane,year,length 1,\(dqNC\-50\(dq,\(dqno\(dq,2001,4825.369405 2,\(dqNC\-50\(dq,\(dqno\(dq,2002,14392.589058 3,\(dqNC\-98\(dq,\(dqno\(dq,2003,3212.981242 4,\(dqNC\-50\(dq,\(dqno\(dq,2004,13391.907552 \fR .fi .PP 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. .PP Note that using multi\-character separator is allowed, but not recommended as it is not generally supported by CSV readers. .PP 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). .SS JSON JSON (JavaScript Object Notation) format is produced according to 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. .PP 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. .PP Example with added indentation (note that booleans are not directly supported; here, an attribute is a string with value no): .br .nf \fC { \(dqrecords\(dq: [ { \(dqcat\(dq: 1, \(dqroad_name\(dq: \(dqNC\-50\(dq, \(dqmultilane\(dq: \(dqno\(dq, \(dqyear\(dq: 2001, \(dqlength\(dq: 4825.369405 }, { \(dqcat\(dq: 2, \(dqroad_name\(dq: \(dqNC\-50\(dq, \(dqmultilane\(dq: \(dqno\(dq, \(dqyear\(dq: 2002, \(dqlength\(dq: 14392.589058 } ] } \fR .fi .PP 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. .SS 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 (\fBvertical_separator\fR option). .PP Example with (horizontal) separator = and vertical separator newline: .br .nf \fC cat=1 road_name=NC\-50 multilane=no year=2001 length=4825.369405 cat=2 road_name=NC\-50 multilane=no year=2002 length=14392.589058 \fR .fi 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 (\fB\-e\fR) 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 with \fBcolumns\fR option set to one column only and escaping (\fB\-e\fR) and no column names flags (\fB\-c\fR). It is also advantageous when you need implement the parsing yourself. .SH NOTES .RS 4n .IP \(bu 4n CSV and JSON were added in version 8.0 as new primary formats for further consumption by scripts and other applications. .IP \(bu 4n Escaping of plain and vertical formats was extended from just backslash and newlines to all escapes from JSON except for double quote character. .RE .SH EXAMPLES All examples are based on the North Carolina sample dataset. .SS Select and show entire table .br .nf \fC v.db.select map=roadsmajor cat|MAJORRDS_|ROAD_NAME|MULTILANE|PROPYEAR|OBJECTID|SHAPE_LEN 1|1|NC\-50|no|0|1|4825.369405 2|2|NC\-50|no|0|2|14392.589058 3|3|NC\-98|no|0|3|3212.981242 4|4|NC\-50|no|0|4|13391.907552 \&... \fR .fi .SS Select and show single column from table Note: multiple columns can be specified as comma separated list. .br .nf \fC v.db.select map=roadsmajor column=ROAD_NAME NC\-50 NC\-50 NC\-98 NC\-50 NC\-98 \&... \fR .fi .SS Print region extent of selected vector features .br .nf \fC v.db.select \-r map=roadsmajor where=\(dqROAD_NAME = \(cqNC\-98\(cq\(dq n=248425.389891 s=245640.640081 w=635906.517653 e=661979.801880 \fR .fi .SS Select empty vector features (no data entries) .br .nf \fC v.db.select geonames_wake where=\(dqALTERNATEN IS NULL\(dq cat|GEONAMEID|NAME|ASCIINAME|ALTERNATEN|FEATURECLA|FEATURECOD|... 8|4498303|West Raleigh|West Raleigh||P|PPL|US||NC|338759|123|... 14|4459467|Cary|Cary||P|PPL|US||NC|103945|146|152|America/Iqaluit|... 31|4452808|Apex|Apex||P|PPL|US||NC|30873|167|134|America/Iqaluit|... \&... \fR .fi .SS Select not empty vector features (no data entries) .br .nf \fC v.db.select geonames_wake where=\(dqALTERNATEN IS NOT NULL\(dq cat|GEONAMEID|NAME|ASCIINAME|ALTERNATEN|FEATURECLA|FEATURECOD|... 9|4487042|Raleigh|Raleigh|Raleigh,... 31299|4487056|Raleigh\-Durham Airport|Raleigh\-Durham Airport|... \&... \fR .fi .SS Select features with distinct road names .br .nf \fC v.db.select map=roadsmajor columns=ROAD_NAME group=ROAD_NAME ROAD_NAME I\-40 I\-440 I\-540 NC\-231 NC\-39 NC\-42 \&... \fR .fi It is also possible to combine with \fIwhere\fR option .br .nf \fC v.db.select map=roadsmajor columns=ROAD_NAME,MULTILANE group=ROAD_NAME where=\(cqROAD_NAME is not null\(cq ROAD_NAME|MULTILANE I\-40|yes I\-440|yes I\-540|yes NC\-231|no NC\-39|no NC\-42|no NC\-50|no NC\-54|no NC\-55|no NC\-96|no NC\-97|no NC\-98|no US\-1| US\-401|no US\-64|yes US\-70|yes \fR .fi It can also use more columns in \fIgroup\fR option .br .nf \fC v.db.select map=roadsmajor columns=ROAD_NAME,MULTILANE group=ROAD_NAME,MULTILANE where=\(cqROAD_NAME is not null\(cq ROAD_NAME|MULTILANE I\-40|yes I\-440|yes I\-540|yes NC\-231|no NC\-39|no NC\-42|no NC\-50|no NC\-54|no NC\-55|no NC\-96|no NC\-97|no NC\-98|no US\-1| US\-1|yes US\-401|no US\-401|yes US\-64|yes US\-70|yes \fR .fi .SS Read results in Python The \fIjson\fR package in the standard Python library can load a JSON string obtained as output from the \fIv.db.select\fR module through the \fIread_command\fR function: .br .nf \fC import json import grass.script as gs text = gs.read_command(\(dqv.db.select\(dq, map=\(dqroadsmajor\(dq, format=\(dqjson\(dq) data = json.loads(text) for row in data[\(dqrecords\(dq]: print(row[\(dqROAD_NAME\(dq]) \fR .fi .SH SEE ALSO \fI db.select \fR .PP \fI GRASS SQL interface \fR .SH AUTHORS Radim Blazek, ITC\-Irst, Trento, Italy .br Minimal region extent added by Martin Landa, FBK\-irst (formerly ITC\-irst), Trento, Italy .br Group option added by Luca Delucchi, Fondazione Edmund Mach, Trento, Italy .br Huidae Cho (JSON output, escaping and features\-only flags) .br Vaclav Petras (true CSV output, format option and documentation) .SH SOURCE CODE .PP Available at: v.db.select source code (history) .PP Accessed: Sunday Jan 22 07:36:57 2023 .PP Main index | Vector index | Topics index | Keywords index | Graphical index | Full index .PP © 2003\-2023 GRASS Development Team, GRASS GIS 8.2.1 Reference Manual