1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
4
<title>SQL support in GRASS GIS</title>
5
<meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
6
<link rel="stylesheet" href="grassdocs.css" type="text/css">
9
<!-- This file is lib/db/sqlp/description.html -->
11
<body bgcolor="white">
13
<img src="grass_logo.png" alt="GRASS logo"><hr align=center size=6 noshade>
15
<H2>SQL support in GRASS GIS</H2>
17
GRASS can use various RDBMS and embedded databases.
18
SQL queries are directly passed to the underlying
20
The set of supported SQL commands depends on the RDMBS
24
The list of available drivers can vary in various binary
25
distributions of GRASS.
29
<tr><td><a href="grass-dbf.html">dbf</a></td><td>DBF files. Data are stored in DBF files.</td>
30
<td><a href="http://shapelib.maptools.org/dbf_api.html">http://shapelib.maptools.org/dbf_api.html</a></td></tr>
32
<tr><td><a href="grass-sqlite.html">sqlite</a></td><td>SQLite embedded database.</td>
33
<td><a href="http://sqlite.org/">http://sqlite.org/</a></td></tr>
35
<tr><td><a href="grass-pg.html">pg</a></td><td>PostgreSQL RDBMS.</td>
36
<td><a href="http://postgresql.org/">http://postgresql.org/</a></td></tr>
38
<tr><td><a href="grass-mysql.html">mysql</a></td><td>MySQL RDBMS.</td>
39
<td><a href="http://mysql.org/">http://mysql.org/</a></td></tr>
41
<tr><td><a href="grass-mesql.html">mesql</a></td><td>MySQL embedded database.</td>
42
<td><a href="http://mysql.org/">http://mysql.org/</a></td></tr>
44
<tr><td><a href="grass-odbc.html">odbc</a></td><td>UnixODBC. (PostgreSQL, Oracle, etc.)</td>
45
<td><a href="http://www.unixodbc.org/">http://www.unixodbc.org/</a></td></tr>
53
<li> SQL does not support '.' (dots) in table names.
54
<li> Supported table name characters are only:<br>
56
<li> A table name must start with a character, not a number.
57
<li> Text-string matching requires the text part to be 'single quoted'.
58
When run from the command line multiple queries should be contained
59
in "double quotes". e.g.<br>
60
<div class="code"><pre>
61
d.vect map where="individual='juvenile' and area='beach'"
63
<li> An error message such as "dbmi: Protocol error" either indicates an invalid column name
64
or an unsupported column type (then the GRASS SQL parser needs to be extended).
65
<li> DBF column names are limited to 10 characters (DBF API definition)
71
Display all vector points except for LAMAR valley and <i>extensive trapping</i> (brackets are superfluous in this example):
72
<div class="code"><pre>
73
d.vect trapping_sites_points fcol=black icon=basic/diamond col=white size=13 \
74
where="valley <> 'LAMAR' OR (valley = 'LAMAR' AND description = 'extensive trapping')"
78
Select all attributes from table where str1 column values are not 'No Name':
79
<div class="code"><pre>
80
echo "SELECT * FROM archsites WHERE str1 <> 'No Name'" | db.select
85
<p><b>Example</b> of subquery expressions from a list (does not work for DBF driver):
86
<div class="code"><pre>
87
v.db.select mysites where="id IN ('P04', 'P05')"
91
<p><b>Example</b> of pattern matching:
92
<div class="code"><pre>
94
# field contains string:
96
v.extract rivers out=rivers_noce where="DES LIKE 'NOCE'"
98
v.extract rivers out=rivers_noce where="DES LIKE '%NOCE%'"
100
# match exactly number of characters (here: 2), does not work for DBF driver:
101
v.db.select mysites where="id LIKE 'P__'"
104
v.db.select mysites where="id LIKE 'P%'"
107
<p><b>Example</b> of null handling:
108
<div class="code"><pre>
109
v.db.addcol map=roads col="nulltest int"
110
v.db.update map=roads col=nulltest value=1 where="cat > 2"
111
d.vect roads where="nulltest is null"
112
v.db.update map=roads col=nulltest value=2 where="cat <= 2"
116
<p><b>Examples</b> of complex expressions in updates (using v.db.* modules):
117
<div class="code"><pre>
118
v.db.addcol map=roads col="exprtest double precision"
119
v.db.update map=roads col=exprtest value=cat/nulltest
120
v.db.update map=roads col=exprtest value=cat/nulltest+cat where=cat=1
124
<p><b>Examples</b> of complex expressions in updates (using db.* modules):
125
<div class="code"><pre>
126
echo "UPDATE roads SET exprtest=null"
127
echo "UPDATE roads SET exprtest=cat/2" | db.execute
128
echo "UPDATE roads SET exprtest=cat/2+cat/3" | db.execute
129
echo "UPDATE roads SET exprtest=NULL WHERE cat>2" | db.execute
130
echo "UPDATE roads SET exprtest=cat/3*(cat+1) WHERE exprtest IS NULL" | db.execute"
135
Instead of creating and updating new columns with an expression,
136
you can use the expression directly in a command:
137
<div class="code"><pre>
138
d.vect roads where="(cat/3*(cat+1))>8"
139
d.vect roads where="cat>exprtest"
143
<p><b>Example</b> of changing a SQL type (type casting, does not work for DBF driver):
144
<div class="code"><pre>
145
# North Carolina data set: convert string column to double precision
146
# copy map into current mapset
147
g.copy vect=geodetic_pts,mygeodetic_pts
148
v.db.addcol mygeodetic_pts col="zval double precision"
150
# the 'z_value' col contains 'N/A' strings, not to be converted
151
v.db.update mygeodetic_pts col=zval \
152
qcol="CAST(z_value AS double precision)" \
153
where="z_value <> 'N/A'"
158
<a href="databaseintro.html">Database management in GRASS GIS</a>,<BR>
159
<a href="database.html">Help pages for database modules</a>,<BR>
160
<a href=http://www.redhat.com/docs/manuals/database/RHDB-2.1-Manual/sql/sqlreference.html>SQL
161
Guide and Reference</a> (Red Hat)
164
<p><i>Last changed: $Date: 2009-06-01 16:15:34 +0200 (Mon, 01 Jun 2009) $</i></p>
166
<BR><a href=index.html>Help Index</a>
167
<P>© 2008 <a href="http://grass.osgeo.org">GRASS Development Team</a></P>