Debian Codegraph @

db/db.execute/db.execute.html

raw
1 <h2>DESCRIPTION</h2>
2
3 <em>db.execute</em> allows the user to execute SQL statements.
4
5 <h2>NOTES</h2>
6
7 <em>db.execute</em> only executes SQL statements and does not return
8 any data. If you need data returned from the database, use
9 <em><a href="db.select.html">db.select</a></em>.
10 <p>
11 If parameters for database connection are already set with
12 <em><a href="db.connect.html">db.connect</a></em>, they are taken as default
13 values and do not need to be specified each time.
14 <p>
15 If you have a large number of SQL commands to process, it is much
16 faster to place all the SQL statements into a text file and
17 use <b>input</b> file parameter than it is to process each statement
18 individually in a loop. If multiple instruction lines are given, each
19 SQL line must end with a semicolon.
20 <p>
21 Please see the individual <em><a href="sql.html">GRASS SQL interface</a></em>
22 for how to create a new database.
23
24 <h2>EXAMPLES</h2>
25
26 Create a new table with columns 'cat' and 'soiltype':
27 <div class="code"><pre>
28 db.execute sql="CREATE TABLE soils (cat integer, soiltype varchar(10))"
29 </pre></div>
30
31 Create a new table using a file with SQL statements
32 <div class="code"><pre>
33 db.execute driver=odbc database=grassdb input=file.sql
34 </pre></div>
35
36 Insert new row into attribute table:
37 <div class="code"><pre>
38 db.execute sql="INSERT INTO mysites (id,name,east,north) values (30,'Ala',1657340,5072301)"
39 </pre></div>
40
41 Update attribute entries to new value based on SQL rule:
42 <div class="code"><pre>
43 db.execute sql="UPDATE roads SET travelcost=5 WHERE cat=1"
44 </pre></div>
45
46 Update attribute entries to new value based on SQL rule:
47 <div class="code"><pre>
48 db.execute sql="UPDATE dourokukan SET testc=50 WHERE testc is NULL"
49 </pre></div>
50
51 Delete selected rows from attribute table:
52 <div class="code"><pre>
53 db.execute sql="DELETE FROM gsod_stationlist WHERE latitude &lt; -91"
54 </pre></div>
55
56 Add new column to attribute table:
57 <div class="code"><pre>
58 db.execute sql="ALTER TABLE roads ADD COLUMN length double"
59 </pre></div>
60
61 Column type conversion - update new column from existing column (all
62 drivers except for DBF):
63 <div class="code"><pre>
64 # 'z_value' is varchar and 'z' is double precision:
65 echo "UPDATE geodetic_pts SET z = CAST(z_value AS numeric)" | db.execute input=-
66 </pre></div>
67
68 Drop column from attribute table:
69 <div class="code"><pre>
70 db.execute sql="ALTER TABLE roads DROP COLUMN length"
71 </pre></div>
72
73 Drop table (not supported by all drivers):
74 <div class="code"><pre>
75 db.execute sql="DROP TABLE fmacopy"
76 </pre></div>
77
78 Update attribute with multiple SQL instructions in file
79 (e.g., <code>file.sql</code>, instruction line must end with a semicolon):
80 <div class="code"><pre>
81 UPDATE roads SET travelcost=5 WHERE cat=1;
82 UPDATE roads SET travelcost=2 WHERE cat=2;
83
84 db.execute input=file.sql
85 </pre></div>
86
87 Join table 'myroads' to table 'extratab' based on common 'cat' column
88 values (not supported by DBF driver):
89 <div class="code"><pre>
90 db.execute sql="UPDATE extratab SET names=(SELECT label FROM myroads WHERE extratab.cat=myroads.cat)"
91 </pre></div>
92
93 <h2>SEE ALSO</h2>
94
95 <em>
96 <a href="db.columns.html">db.columns</a>,
97 <a href="db.describe.html">db.describe</a>,
98 <a href="db.drivers.html">db.drivers</a>,
99 <a href="db.droptable.html">db.droptable</a>,
100 <a href="db.login.html">db.login</a>,
101 <a href="db.select.html">db.select</a>,
102 <a href="db.tables.html">db.tables</a>,
103 </em>
104 <p>
105 <em>
106 <a href="sql.html">GRASS SQL interface</a>
107 </em>
108
109 <h2>AUTHOR</h2>
110
111 CERL