GRASS logo

NAME

db.execute - Executes any SQL statement.

KEYWORDS

database, SQL

SYNOPSIS

db.execute
db.execute help
db.execute [-i] [input=name] [driver=name] [database=name] [--verbose] [--quiet]

Flags:

-i
Ignore SQL errors and continue
--verbose
Verbose module output
--quiet
Quiet module output

Parameters:

input=name
Name of file containing SQL statements
driver=name
Driver name
Options: ogr,sqlite,mysql,pg,odbc,dbf
Default: dbf
database=name
Database name
Default: $GISDBASE/$LOCATION_NAME/$MAPSET/dbf/

DESCRIPTION

db.execute allows the user to execute SQL statements.

NOTES

db.execute only executes SQL statements and does not return any data. If you need data returned from the database, use db.select.

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.

If you have a large number of SQL commands to process, it is much much faster to place all the SQL statements into a text file and use db.execute's input file parameter than it is to process each statement individually in a loop. If multiple instruction lines are given, each SQL line must end with a semicolon.

Please see the individual sql driver pages for how to create a new database.

EXAMPLES

Create a new table with columns 'cat' and 'soiltype':
echo 'create table soils (cat integer, soiltype varchar(10) )' | db.execute

Create a new table using a file with SQL statements:

db.execute driver=odbc database=g60test input=file.sql

Insert new row into attribute table:

echo "INSERT INTO nobugs (id,name,east_gb,north_gb) values (30,'Ala',1657340,5072301)" | db.execute

Update attribute entries to new value based on SQL rule:

echo "UPDATE roads SET travelcost=5 WHERE cat=1" | db.execute

Update attribute entries to new value based on SQL rule:

echo "UPDATE dourokukan SET testc=50 WHERE testc is NULL" | db.execute

Delete selected rows from attribute table:

echo "DELETE FROM gsod_stationlist WHERE latitude < -91" | db.execute

Add new column to attribute table:

echo "ALTER TABLE roads ADD COLUMN length double" | db.execute

Column type conversion - update new column from existing column (all drivers except for DBF):

# 'z_value' is varchar and 'z' is double precision:
echo "update geodetic_pts SET z = CAST(z_value AS numeric)" | db.execute

Drop column from attribute table:

echo "ALTER TABLE roads DROP COLUMN length" | db.execute

Drop table (not supported by all drivers)

echo "DROP TABLE fmacopy" | db.execute

Update attribute with multiple SQL instructions in file (e.g., file.sql, instruction line must end with a semicolon):

UPDATE roads SET travelcost=5 WHERE cat=1;
UPDATE roads SET travelcost=2 WHERE cat=2;
 
cat file.sql | db.execute

SEE ALSO

db.columns, db.describe, db.drivers, db.droptable, db.login, db.select, db.tables, GRASS SQL interface

AUTHOR

CERL

Last changed: $Date: 2007-10-08 16:30:41 -0700 (Mon, 08 Oct 2007) $


Main index - database index - Full index

© 2003-2008 GRASS Development Team