db.execute.html 3.4 KB

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