sql.html 5.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01 Transitional//EN">
  2. <html>
  3. <head>
  4. <title>SQL support in GRASS GIS</title>
  5. <meta http-equiv="Content-Type" content="text/html; charset=iso-8859-1">
  6. <link rel="stylesheet" href="grassdocs.css" type="text/css">
  7. </head>
  8. <!-- This file is lib/db/sqlp/description.html -->
  9. <body bgcolor="white">
  10. <img src="grass_logo.png" alt="GRASS logo"><hr align=center size=6 noshade>
  11. <H2>SQL support in GRASS GIS</H2>
  12. GRASS can use various RDBMS and embedded databases.
  13. SQL queries are directly passed to the underlying
  14. database system.
  15. The set of supported SQL commands depends on the RDMBS
  16. and driver selected.
  17. <H2>Drivers</H2>
  18. The list of available drivers can vary in various binary
  19. distributions of GRASS.
  20. <p>
  21. <table border=1 >
  22. <tr><td><a href="grass-dbf.html">dbf</a></td><td>DBF files. Data are stored in DBF files.</td>
  23. <td><a href="http://shapelib.maptools.org/dbf_api.html">http://shapelib.maptools.org/dbf_api.html</a></td></tr>
  24. <tr><td><a href="grass-sqlite.html">sqlite</a></td><td>SQLite embedded database.</td>
  25. <td><a href="http://sqlite.org/">http://sqlite.org/</a></td></tr>
  26. <tr><td><a href="grass-pg.html">pg</a></td><td>PostgreSQL RDBMS.</td>
  27. <td><a href="http://postgresql.org/">http://postgresql.org/</a></td></tr>
  28. <tr><td><a href="grass-mysql.html">mysql</a></td><td>MySQL RDBMS.</td>
  29. <td><a href="http://mysql.org/">http://mysql.org/</a></td></tr>
  30. <tr><td><a href="grass-mesql.html">mesql</a></td><td>MySQL embedded database.</td>
  31. <td><a href="http://mysql.org/">http://mysql.org/</a></td></tr>
  32. <tr><td><a href="grass-odbc.html">odbc</a></td><td>UnixODBC. (PostgreSQL, Oracle, etc.)</td>
  33. <td><a href="http://www.unixodbc.org/">http://www.unixodbc.org/</a></td></tr>
  34. </table>
  35. <H2>NOTES</H2>
  36. <ul>
  37. <li> SQL does not support '.' (dots) in table names.
  38. <li> Supported table name characters are only:<br>
  39. [A-Za-z][A-Za-z0-9_]*
  40. <li> A table name must start with a character, not a number.
  41. <li> Text-string matching requires the text part to be 'single quoted'.
  42. When run from the command line multiple queries should be contained
  43. in "double quotes". e.g.<br>
  44. <div class="code"><pre>
  45. d.vect map where="individual='juvenile' and area='beach'"
  46. </pre></div>
  47. <li> An error message such as "dbmi: Protocol error" either indicates an invalid column name
  48. or an unsupported column type (then the GRASS SQL parser needs to be extended).
  49. <li> DBF column names are limited to 10 characters (DBF API definition)
  50. </ul>
  51. <h2>EXAMPLES</h2>
  52. Display all vector points except for LAMAR valley and <i>extensive trapping</i> (brackets are superfluous in this example):
  53. <div class="code"><pre>
  54. d.vect trapping_sites_points fcol=black icon=basic/diamond col=white size=13 \
  55. where="valley <> 'LAMAR' OR (valley = 'LAMAR' AND description = 'extensive trapping')"
  56. </pre></div>
  57. <p>
  58. Select all attributes from table where str1 column values are not 'No Name':
  59. <div class="code"><pre>
  60. echo "SELECT * FROM archsites WHERE str1 &lt;&gt; 'No Name'" | db.select
  61. </pre></div>
  62. <p>
  63. <p>Example of subquery expressions from a list (does not work for DBF driver):
  64. <div class="code"><pre>
  65. v.db.select mysites where="id IN ('P04', 'P05')"
  66. </pre></div>
  67. <p>Example of pattern matching:
  68. <div class="code"><pre>
  69. # match exactly number of characters (here: 2), does not work for DBF driver:
  70. v.db.select mysites where="id LIKE 'P__'"
  71. #define wildcard:
  72. v.db.select mysites where="id LIKE 'P%'"
  73. </pre></div>
  74. <p>Example of null handling:
  75. <div class="code"><pre>
  76. v.db.addcol map=roads col="nulltest int"
  77. v.db.update map=roads col=nulltest value=1 where="cat &gt; 2"
  78. d.vect roads where="nulltest is null"
  79. v.db.update map=roads col=nulltest value=2 where="cat &lt;= 2"
  80. </pre></div>
  81. <p>Examples of complex expressions in updates (using v.db.* modules):
  82. <div class="code"><pre>
  83. v.db.addcol map=roads col="exprtest double precision"
  84. v.db.update map=roads col=exprtest value=cat/nulltest
  85. v.db.update map=roads col=exprtest value=cat/nulltest+cat where=cat=1
  86. </pre></div>
  87. <p>Examples of complex expressions in updates (using db.* modules):
  88. <div class="code"><pre>
  89. echo "UPDATE roads SET exprtest=null"
  90. echo "UPDATE roads SET exprtest=cat/2" | db.execute
  91. echo "UPDATE roads SET exprtest=cat/2+cat/3" | db.execute
  92. echo "UPDATE roads SET exprtest=NULL WHERE cat&gt;2" | db.execute
  93. echo "UPDATE roads SET exprtest=cat/3*(cat+1) WHERE exprtest IS NULL" | db.execute"
  94. </pre></div>
  95. <p>
  96. Instead of creating and updating new columns with an expression,
  97. you can use the expression directly in a command:
  98. <div class="code"><pre>
  99. d.vect roads where="(cat/3*(cat+1))&gt;8"
  100. d.vect roads where="cat&gt;exprtest"
  101. </pre></div>
  102. <h2>SEE ALSO</h2>
  103. <a href="databaseintro.html">Database management in GRASS GIS</a>,<BR>
  104. <a href="database.html">Help pages for database modules</a>,<BR>
  105. <a href=http://www.redhat.com/docs/manuals/database/RHDB-2.1-Manual/sql/sqlreference.html>SQL
  106. Guide and Reference</a> (Red Hat)
  107. <p><i>Last changed: $Date$</i></p>
  108. <HR>
  109. <BR><a href=index.html>Help Index</a>
  110. <P>&copy; 2008 <a href="http://grass.osgeo.org">GRASS Development Team</a></P>
  111. </body>
  112. </html>