tclsqlite.tcl 7.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266
  1. #
  2. # Run this Tcl script to generate the tclsqlite.html file.
  3. #
  4. set rcsid {$Id: tclsqlite.tcl,v 1.7 2002/04/12 10:09:00 drh Exp $}
  5. puts {<html>
  6. <head>
  7. <title>The Tcl interface to the SQLite library</title>
  8. </head>
  9. <body bgcolor=white>
  10. <h1 align=center>
  11. The Tcl interface to the SQLite library
  12. </h1>}
  13. puts "<p align=center>
  14. (This page was last modified on [lrange $rcsid 3 4] UTC)
  15. </p>"
  16. puts {
  17. <p>The SQLite library is designed to be very easy to use from
  18. a Tcl or Tcl/Tk script. This document gives an overview of the Tcl
  19. programming interface.</p>
  20. <h2>The API</h2>
  21. <p>The interface to the SQLite library consists of single
  22. tcl command named <b>sqlite</b>. Because there is only this
  23. one interface command, the interface is not placed in a separate
  24. namespace.</p>
  25. <p>The <b>sqlite</b> command is used as follows:</p>
  26. <blockquote>
  27. <b>sqlite</b>&nbsp;&nbsp;<i>dbcmd&nbsp;&nbsp;database-name</i>
  28. </blockquote>
  29. <p>
  30. The <b>sqlite</b> command opens the database named in the second
  31. argument. If the database does not already exist, it is
  32. automatically created.
  33. The <b>sqlite</b> command also creates a new Tcl
  34. command to control the database. The name of the new Tcl command
  35. is given by the first argument. This approach is similar to the
  36. way widgets are created in Tk.
  37. </p>
  38. <p>
  39. The name of the database is just the name of a disk file in which
  40. the database is stored.
  41. </p>
  42. <p>
  43. Once an SQLite database is open, it can be controlled using
  44. methods of the <i>dbcmd</i>. There are currently 7 methods
  45. defined:</p>
  46. <p>
  47. <ul>
  48. <li> busy
  49. <li> changes
  50. <li> close
  51. <li> complete
  52. <li> eval
  53. <li> last_insert_rowid
  54. <li> timeout
  55. </ul>
  56. </p>
  57. <p>We will explain all of these methods, though not in that order.
  58. We will be begin with the "close" method.</p>
  59. <h2>The "close" method</h2>
  60. <p>
  61. As its name suggests, the "close" method to an SQLite database just
  62. closes the database. This has the side-effect of deleting the
  63. <i>dbcmd</i> Tcl command. Here is an example of opening and then
  64. immediately closing a database:
  65. </p>
  66. <blockquote>
  67. <b>sqlite db1 ./testdb<br>
  68. db1 close</b>
  69. </blockquote>
  70. <p>
  71. If you delete the <i>dbcmd</i> directly, that has the same effect
  72. as invoking the "close" method. So the following code is equivalent
  73. to the previous:</p>
  74. <blockquote>
  75. <b>sqlite db1 ./testdb<br>
  76. rename db1 {}</b>
  77. </blockquote>
  78. <h2>The "eval" method</h2>
  79. <p>
  80. The most useful <i>dbcmd</i> method is "eval". The eval method is used
  81. to execute SQL on the database. The syntax of the eval method looks
  82. like this:</p>
  83. <blockquote>
  84. <i>dbcmd</i>&nbsp;&nbsp;<b>eval</b>&nbsp;&nbsp;<i>sql</i>
  85. &nbsp;&nbsp;?<i>array-name&nbsp;&nbsp;script</i>?
  86. </blockquote>
  87. <p>
  88. The job of the eval method is to execute the SQL statement or statements
  89. given in the second argument. For example, to create a new table in
  90. a database, you can do this:</p>
  91. <blockquote>
  92. <b>sqlite db1 ./testdb<br>
  93. db1 eval {CREATE TABLE t1(a int, b text)}</b>
  94. </blockquote>
  95. <p>The above code creates a new table named <b>t1</b> with columns
  96. <b>a</b> and <b>b</b>. What could be simpler?</p>
  97. <p>Query results are returned as a list of column values. If a
  98. query requests 2 columns and there are 3 rows matching the query,
  99. then the returned list will contain 6 elements. For example:</p>
  100. <blockquote>
  101. <b>db1 eval {INSERT INTO t1 VALUES(1,'hello')}<br>
  102. db1 eval {INSERT INTO t1 VALUES(2,'goodbye')}<br>
  103. db1 eval {INSERT INTO t1 VALUES(3,'howdy!')}<br>
  104. set x [db1 eval {SELECT * FROM t1 ORDER BY a}]</b>
  105. </blockquote>
  106. <p>The variable <b>$x</b> is set by the above code to</p>
  107. <blockquote>
  108. <b>1 hello 2 goodbye 3 howdy!</b>
  109. </blockquote>
  110. <p>You can also process the results of a query one row at a time
  111. by specifying the name of an array variable and a script following
  112. the SQL code. For each row of the query result, the value of each
  113. column will be inserted into the array variable and the script will
  114. be executed. For instance:</p>
  115. <blockquote>
  116. <b>db1 eval {SELECT * FROM t1 ORDER BY a} values {<br>
  117. &nbsp;&nbsp;&nbsp;&nbsp;parray values<br>
  118. &nbsp;&nbsp;&nbsp;&nbsp;puts ""<br>
  119. }</b>
  120. </blockquote>
  121. <p>This last code will give the following output:</p>
  122. <blockquote><b>
  123. values(*) = a b<br>
  124. values(a) = 1<br>
  125. values(b) = hello<p>
  126. values(*) = a b<br>
  127. values(a) = 2<br>
  128. values(b) = goodbye<p>
  129. values(*) = a b<br>
  130. values(a) = 3<br>
  131. values(b) = howdy!</b>
  132. </blockquote>
  133. <p>
  134. For each column in a row of the result, the name of that column
  135. is used as an index in to array. The value of the column is stored
  136. in the corresponding array entry. The special array index * is
  137. used to store a list of column names in the order that they appear.
  138. </p>
  139. <p>
  140. If the array variable name is the empty string, then the value of
  141. each column is stored in a variable with the same name as the column
  142. itself. For example:
  143. </p>
  144. <blockquote>
  145. <b>db1 eval {SELECT * FROM t1 ORDER BY a} {} {<br>
  146. &nbsp;&nbsp;&nbsp;&nbsp;puts "a=$a b=$b"<br>
  147. }</b>
  148. </blockquote>
  149. <p>
  150. From this we get the following output
  151. </p>
  152. <blockquote><b>
  153. a=1 b=hello<br>
  154. a=2 b=goodbye<br>
  155. a=3 b=howdy!</b>
  156. </blockquote>
  157. <h2>The "complete" method</h2>
  158. <p>
  159. The "complete" method takes a string of supposed SQL as its only argument.
  160. It returns TRUE if the string is a complete statement of SQL and FALSE if
  161. there is more to be entered.</p>
  162. <p>The "complete" method is useful when building interactive applications
  163. in order to know when the user has finished entering a line of SQL code.
  164. This is really just an interface to the <b>sqlite_complete()</b> C
  165. function. Refer to the <a href="c_interface.html">C/C++ interface</a>
  166. specification for additional information.</p>
  167. <h2>The "timeout" method</h2>
  168. <p>The "timeout" method is used to control how long the SQLite library
  169. will wait for locks to clear before giving up on a database transaction.
  170. The default timeout is 0 millisecond. (In other words, the default behavior
  171. is not to wait at all.)</p>
  172. <p>The SQlite database allows multiple simultaneous
  173. readers or a single writer but not both. If any process is writing to
  174. the database no other process is allows to read or write. If any process
  175. is reading the database other processes are allowed to read but not write.
  176. The entire database shared a single lock.</p>
  177. <p>When SQLite tries to open a database and finds that it is locked, it
  178. can optionally delay for a short while and try to open the file again.
  179. This process repeats until the query times out and SQLite returns a
  180. failure. The timeout is adjustable. It is set to 0 by default so that
  181. if the database is locked, the SQL statement fails immediately. But you
  182. can use the "timeout" method to change the timeout value to a positive
  183. number. For example:</p>
  184. <blockquote><b>db1 timeout 2000</b></blockquote>
  185. <p>The argument to the timeout method is the maximum number of milliseconds
  186. to wait for the lock to clear. So in the example above, the maximum delay
  187. would be 2 seconds.</p>
  188. <h2>The "busy" method</h2>
  189. <p>The "busy" method, like "timeout", only comes into play when the
  190. database is locked. But the "busy" method gives the programmer much more
  191. control over what action to take. The "busy" method specifies a callback
  192. Tcl procedure that is invoked whenever SQLite tries to open a locked
  193. database. This callback can do whatever is desired. Presumably, the
  194. callback will do some other useful work for a short while then return
  195. so that the lock can be tried again. The callback procedure should
  196. return "0" if it wants SQLite to try again to open the database and
  197. should return "1" if it wants SQLite to abandon the current operation.
  198. <h2>The "last_insert_rowid" method</h2>
  199. <p>The "last_insert_rowid" method returns an integer which is the ROWID
  200. of the most recently inserted database row.</p>
  201. <h2>The "changes" method</h2>
  202. <p>The "changes" method returns an integer which is the number of rows
  203. in the database that were inserted, deleted, and/or modified by the most
  204. recent "eval" method.</p>
  205. }
  206. puts {
  207. <p><hr /></p>
  208. <p><a href="index.html"><img src="/goback.jpg" border=0 />
  209. Back to the SQLite Home Page</a>
  210. </p>
  211. </body></html>}