.TH sql 1grass "" "GRASS 6.4.2" "Grass User's Manual" .SH NAME sql - SQL support introduction SQL support introduction .SH SQL support in GRASS GIS GRASS can use various RDBMS and embedded databases. SQL queries are directly passed to the underlying database system. The set of supported SQL commands depends on the RDMBS and driver selected. .SH Drivers The list of available drivers can vary in various binary distributions of GRASS. .PP .br | dbf | DBF files. Data are stored in DBF files. | http://shapelib.maptools.org/dbf_api.html .br | sqlite | SQLite embedded database. | http://sqlite.org/ .br | pg | PostgreSQL RDBMS. | http://postgresql.org/ .br | mysql | MySQL RDBMS. | http://mysql.org/ .br | mesql | MySQL embedded database. | http://mysql.org/ .br | odbc | UnixODBC. (PostgreSQL, Oracle, etc.) | http://www.unixodbc.org/ .SH NOTES .RS .IP SQL does not support '.' (dots) in table names. .IP Supported table name characters are only: .br [A-Za-z][A-Za-z0-9_]* .IP A table name must start with a character, not a number. .IP Text-string matching requires the text part to be 'single quoted'. When run from the command line multiple queries should be contained in "double quotes". e.g. .br \fC .DS .br d.vect map where="individual='juvenile' and area='beach'" .br .DE \fR .IP An error message such as "dbmi: Protocol error" either indicates an invalid column name or an unsupported column type (then the GRASS SQL parser needs to be extended). .IP DBF column names are limited to 10 characters (DBF API definition) .RE .SH EXAMPLES Display all vector points except for LAMAR valley and \fIextensive trapping\fR (brackets are superfluous in this example): \fC .DS .br d.vect trapping_sites_points fcol=black icon=basic/diamond col=white size=13 \(rs .br where="valley <> 'LAMAR' OR (valley = 'LAMAR' AND description = 'extensive trapping')" .br .DE \fR .PP Select all attributes from table where str1 column values are not 'No Name': \fC .DS .br echo "SELECT * FROM archsites WHERE str1 <> 'No Name'" | db.select .br .DE \fR .PP .PP \fBExample\fR of subquery expressions from a list (does not work for DBF driver): \fC .DS .br v.db.select mysites where="id IN ('P04', 'P05')" .br .DE \fR .PP \fBExample\fR of pattern matching: \fC .DS .br .br # field contains string: .br # for DBF driver: .br v.extract rivers out=rivers_noce where="DES LIKE 'NOCE'" .br # for SQLite driver: .br v.extract rivers out=rivers_noce where="DES LIKE '%NOCE%'" .br .br # match exactly number of characters (here: 2), does not work for DBF driver: .br v.db.select mysites where="id LIKE 'P__'" .br .br #define wildcard: .br v.db.select mysites where="id LIKE 'P%'" .br .DE \fR .PP \fBExample\fR of null handling: \fC .DS .br v.db.addcol map=roads col="nulltest int" .br v.db.update map=roads col=nulltest value=1 where="cat > 2" .br d.vect roads where="nulltest is null" .br v.db.update map=roads col=nulltest value=2 where="cat <= 2" .br .DE \fR .PP \fBExamples\fR of complex expressions in updates (using v.db.* modules): \fC .DS .br v.db.addcol map=roads col="exprtest double precision" .br v.db.update map=roads col=exprtest value=cat/nulltest .br v.db.update map=roads col=exprtest value=cat/nulltest+cat where=cat=1 .br .DE \fR .PP \fBExamples\fR of complex expressions in updates (using db.* modules): \fC .DS .br echo "UPDATE roads SET exprtest=null" .br echo "UPDATE roads SET exprtest=cat/2" | db.execute .br echo "UPDATE roads SET exprtest=cat/2+cat/3" | db.execute .br echo "UPDATE roads SET exprtest=NULL WHERE cat>2" | db.execute .br echo "UPDATE roads SET exprtest=cat/3*(cat+1) WHERE exprtest IS NULL" | db.execute" .br .DE \fR .PP Instead of creating and updating new columns with an expression, you can use the expression directly in a command: \fC .DS .br d.vect roads where="(cat/3*(cat+1))>8" .br d.vect roads where="cat>exprtest" .br .DE \fR .PP \fBExample\fR of changing a SQL type (type casting, does not work for DBF driver): \fC .DS .br # North Carolina data set: convert string column to double precision .br # copy map into current mapset .br g.copy vect=geodetic_pts,mygeodetic_pts .br v.db.addcol mygeodetic_pts col="zval double precision" .br .br # the 'z_value' col contains 'N/A' strings, not to be converted .br v.db.update mygeodetic_pts col=zval \(rs .br qcol="CAST(z_value AS double precision)" \(rs .br where="z_value <> 'N/A'" .br .DE \fR .SH SEE ALSO Database management in GRASS GIS, .br Help pages for database modules, .br SQL Guide and Reference (Red Hat) .PP \fILast changed: $Date: 2010-09-22 22:14:01 +0200 (Wed, 22 Sep 2010) $\fR .br Help Index .PP © 2008 GRASS Development Team