db.execute.html 3.4 KB

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