Same fix as r45172 for classes/iconimage:
[AROS-Contrib.git] / sqlite3 / www / tclsqlite.tcl
blob8bb6d7978f0e8b95add381121ab032b44d430986
2 # Run this Tcl script to generate the tclsqlite.html file.
4 set rcsid {$Id: tclsqlite.tcl,v 1.13 2005/04/03 23:54:45 danielk1977 Exp $}
5 source common.tcl
6 header {The Tcl interface to the SQLite library}
7 proc METHOD {name text} {
8 puts "<a name=\"$name\">\n<h3>The \"$name\" method</h3>\n"
9 puts $text
11 puts {
12 <h2>The Tcl interface to the SQLite library</h2>
14 <p>The SQLite library is designed to be very easy to use from
15 a Tcl or Tcl/Tk script. This document gives an overview of the Tcl
16 programming interface.</p>
18 <h3>The API</h3>
20 <p>The interface to the SQLite library consists of single
21 tcl command named <b>sqlite</b> (version 2.8) or <b>sqlite3</b>
22 (version 3.0). Because there is only this
23 one command, the interface is not placed in a separate
24 namespace.</p>
26 <p>The <b>sqlite3</b> command is used as follows:</p>
28 <blockquote>
29 <b>sqlite3</b>&nbsp;&nbsp;<i>dbcmd&nbsp;&nbsp;database-name</i>
30 </blockquote>
32 <p>
33 The <b>sqlite3</b> command opens the database named in the second
34 argument. If the database does not already exist, it is
35 automatically created.
36 The <b>sqlite3</b> command also creates a new Tcl
37 command to control the database. The name of the new Tcl command
38 is given by the first argument. This approach is similar to the
39 way widgets are created in Tk.
40 </p>
42 <p>
43 The name of the database is just the name of a disk file in which
44 the database is stored.
45 </p>
47 <p>
48 Once an SQLite database is open, it can be controlled using
49 methods of the <i>dbcmd</i>. There are currently 19 methods
50 defined:</p>
52 <p>
53 <ul>
55 foreach m [lsort {
56 authorizer
57 busy
58 changes
59 close
60 collate
61 collation_needed
62 commit_hook
63 complete
64 copy
65 errorcode
66 eval
67 function
68 last_insert_rowid
69 nullvalue
70 onecolumn
71 progress
72 timeout
73 total_changes
74 trace
75 }] {
76 puts "<li><a href=\"#$m\">$m</a></li>"
78 puts {
79 </ul>
80 </p>
82 <p>The use of each of these methods will be explained in the sequel, though
83 not in the order shown above.</p>
87 ##############################################################################
88 METHOD close {
90 <p>
91 As its name suggests, the "close" method to an SQLite database just
92 closes the database. This has the side-effect of deleting the
93 <i>dbcmd</i> Tcl command. Here is an example of opening and then
94 immediately closing a database:
95 </p>
97 <blockquote>
98 <b>sqlite3 db1 ./testdb<br>
99 db1 close</b>
100 </blockquote>
103 If you delete the <i>dbcmd</i> directly, that has the same effect
104 as invoking the "close" method. So the following code is equivalent
105 to the previous:</p>
107 <blockquote>
108 <b>sqlite3 db1 ./testdb<br>
109 rename db1 {}</b>
110 </blockquote>
113 ##############################################################################
114 METHOD eval {
116 The most useful <i>dbcmd</i> method is "eval". The eval method is used
117 to execute SQL on the database. The syntax of the eval method looks
118 like this:</p>
120 <blockquote>
121 <i>dbcmd</i>&nbsp;&nbsp;<b>eval</b>&nbsp;&nbsp;<i>sql</i>
122 &nbsp;&nbsp;&nbsp;&nbsp;?<i>array-name&nbsp;</i>?&nbsp;?<i>script</i>?
123 </blockquote>
126 The job of the eval method is to execute the SQL statement or statements
127 given in the second argument. For example, to create a new table in
128 a database, you can do this:</p>
130 <blockquote>
131 <b>sqlite3 db1 ./testdb<br>
132 db1 eval {CREATE TABLE t1(a int, b text)}</b>
133 </blockquote>
135 <p>The above code creates a new table named <b>t1</b> with columns
136 <b>a</b> and <b>b</b>. What could be simpler?</p>
138 <p>Query results are returned as a list of column values. If a
139 query requests 2 columns and there are 3 rows matching the query,
140 then the returned list will contain 6 elements. For example:</p>
142 <blockquote>
143 <b>db1 eval {INSERT INTO t1 VALUES(1,'hello')}<br>
144 db1 eval {INSERT INTO t1 VALUES(2,'goodbye')}<br>
145 db1 eval {INSERT INTO t1 VALUES(3,'howdy!')}<br>
146 set x [db1 eval {SELECT * FROM t1 ORDER BY a}]</b>
147 </blockquote>
149 <p>The variable <b>$x</b> is set by the above code to</p>
151 <blockquote>
152 <b>1 hello 2 goodbye 3 howdy!</b>
153 </blockquote>
155 <p>You can also process the results of a query one row at a time
156 by specifying the name of an array variable and a script following
157 the SQL code. For each row of the query result, the values of all
158 columns will be inserted into the array variable and the script will
159 be executed. For instance:</p>
161 <blockquote>
162 <b>db1 eval {SELECT * FROM t1 ORDER BY a} values {<br>
163 &nbsp;&nbsp;&nbsp;&nbsp;parray values<br>
164 &nbsp;&nbsp;&nbsp;&nbsp;puts ""<br>
165 }</b>
166 </blockquote>
168 <p>This last code will give the following output:</p>
170 <blockquote><b>
171 values(*) = a b<br>
172 values(a) = 1<br>
173 values(b) = hello<p>
175 values(*) = a b<br>
176 values(a) = 2<br>
177 values(b) = goodbye<p>
179 values(*) = a b<br>
180 values(a) = 3<br>
181 values(b) = howdy!</b>
182 </blockquote>
185 For each column in a row of the result, the name of that column
186 is used as an index in to array. The value of the column is stored
187 in the corresponding array entry. The special array index * is
188 used to store a list of column names in the order that they appear.
189 </p>
192 If the array variable name is omitted or is the empty string, then the value of
193 each column is stored in a variable with the same name as the column
194 itself. For example:
195 </p>
197 <blockquote>
198 <b>db1 eval {SELECT * FROM t1 ORDER BY a} {<br>
199 &nbsp;&nbsp;&nbsp;&nbsp;puts "a=$a b=$b"<br>
200 }</b>
201 </blockquote>
204 From this we get the following output
205 </p>
207 <blockquote><b>
208 a=1 b=hello<br>
209 a=2 b=goodbye<br>
210 a=3 b=howdy!</b>
211 </blockquote>
214 Tcl variable names can appear in the SQL statement of the second argument
215 in any position where it is legal to put a string or number literal. The
216 value of the variable is substituted for the variable name. If the
217 variable does not exist a NULL values is used. For example:
218 </p>
220 <blockquote><b>
221 db1 eval {INSERT INTO t1 VALUES(5,$bigblob)}
222 </b></blockquote>
225 Note that it is not necessary to quote the $bigblob value. That happens
226 automatically. If $bigblob is a large string or binary object, this
227 technique is not only easier to write, it is also much more efficient
228 since it avoids making a copy of the content of $bigblob.
229 </p>
233 ##############################################################################
234 METHOD complete {
237 The "complete" method takes a string of supposed SQL as its only argument.
238 It returns TRUE if the string is a complete statement of SQL and FALSE if
239 there is more to be entered.</p>
241 <p>The "complete" method is useful when building interactive applications
242 in order to know when the user has finished entering a line of SQL code.
243 This is really just an interface to the <b>sqlite3_complete()</b> C
244 function. Refer to the <a href="c_interface.html">C/C++ interface</a>
245 specification for additional information.</p>
248 ##############################################################################
249 METHOD copy {
252 The "copy" method copies data from a file into a table.
253 It returns the number of rows processed successfully from the file.
254 The syntax of the copy method looks like this:</p>
256 <blockquote>
257 <i>dbcmd</i>&nbsp;&nbsp;<b>copy</b>&nbsp;&nbsp;<i>conflict-algorithm</i>
258 &nbsp;&nbsp;<i>table-name&nbsp;</i>&nbsp;&nbsp;<i>file-name&nbsp;</i>
259 &nbsp;&nbsp;&nbsp;&nbsp;?<i>column-separator&nbsp;</i>?
260 &nbsp;&nbsp;?<i>null-indicator</i>?
261 </blockquote>
263 <p>Conflict-alogrithm must be one of the SQLite conflict algorithms for
264 the INSERT statement: <i>rollback</i>, <i>abort</i>,
265 <i>fail</i>,<i>ignore</i>, or <i>replace</i>. See the SQLite Language
266 section for <a href="lang.html#conflict">ON CONFLICT</a> for more information.
267 The conflict-algorithm must be specified in lower case.
268 </p>
270 <p>Table-name must already exists as a table. File-name must exist, and
271 each row must contain the same number of columns as defined in the table.
272 If a line in the file contains more or less than the number of columns defined,
273 the copy method rollbacks any inserts, and returns an error.</p>
275 <p>Column-separator is an optional column separator string. The default is
276 the ASCII tab character \t. </p>
278 <p>Null-indicator is an optional string that indicates a column value is null.
279 The default is an empty string. Note that column-separator and
280 null-indicator are optional positional arguments; if null-indicator
281 is specified, a column-separator argument must be specifed and
282 precede the null-indicator argument.</p>
284 <p>The copy method implements similar functionality to the <b>.import</b>
285 SQLite shell command.
286 The SQLite 2.x <a href="lang.html#copy"><b>COPY</b></a> statement
287 (using the PostgreSQL COPY file format)
288 can be implemented with this method as:</p>
290 <blockquote>
291 dbcmd&nbsp;&nbsp;copy&nbsp;&nbsp;$conflictalgo
292 &nbsp;&nbsp;$tablename&nbsp;&nbsp;&nbsp;$filename&nbsp;
293 &nbsp;&nbsp;&nbsp;&nbsp;\t&nbsp;
294 &nbsp;&nbsp;\\N
295 </blockquote>
299 ##############################################################################
300 METHOD timeout {
302 <p>The "timeout" method is used to control how long the SQLite library
303 will wait for locks to clear before giving up on a database transaction.
304 The default timeout is 0 millisecond. (In other words, the default behavior
305 is not to wait at all.)</p>
307 <p>The SQLite database allows multiple simultaneous
308 readers or a single writer but not both. If any process is writing to
309 the database no other process is allows to read or write. If any process
310 is reading the database other processes are allowed to read but not write.
311 The entire database shared a single lock.</p>
313 <p>When SQLite tries to open a database and finds that it is locked, it
314 can optionally delay for a short while and try to open the file again.
315 This process repeats until the query times out and SQLite returns a
316 failure. The timeout is adjustable. It is set to 0 by default so that
317 if the database is locked, the SQL statement fails immediately. But you
318 can use the "timeout" method to change the timeout value to a positive
319 number. For example:</p>
321 <blockquote><b>db1 timeout 2000</b></blockquote>
323 <p>The argument to the timeout method is the maximum number of milliseconds
324 to wait for the lock to clear. So in the example above, the maximum delay
325 would be 2 seconds.</p>
328 ##############################################################################
329 METHOD busy {
331 <p>The "busy" method, like "timeout", only comes into play when the
332 database is locked. But the "busy" method gives the programmer much more
333 control over what action to take. The "busy" method specifies a callback
334 Tcl procedure that is invoked whenever SQLite tries to open a locked
335 database. This callback can do whatever is desired. Presumably, the
336 callback will do some other useful work for a short while (such as service
337 GUI events) then return
338 so that the lock can be tried again. The callback procedure should
339 return "0" if it wants SQLite to try again to open the database and
340 should return "1" if it wants SQLite to abandon the current operation.
343 ##############################################################################
344 METHOD last_insert_rowid {
346 <p>The "last_insert_rowid" method returns an integer which is the ROWID
347 of the most recently inserted database row.</p>
350 ##############################################################################
351 METHOD function {
353 <p>The "function" method registers new SQL functions with the SQLite engine.
354 The arguments are the name of the new SQL function and a TCL command that
355 implements that function. Arguments to the function are appended to the
356 TCL command before it is invoked.</p>
359 The following example creates a new SQL function named "hex" that converts
360 its numeric argument in to a hexadecimal encoded string:
361 </p>
363 <blockquote><b>
364 db function hex {format 0x%X}
365 </b></blockquote>
369 ##############################################################################
370 METHOD nullvalue {
373 The "nullvalue" method changes the representation for NULL returned
374 as result of the "eval" method.</p>
376 <blockquote><b>
377 db1 nullvalue NULL
378 </b></blockquote>
380 <p>The "nullvalue" method is useful to differ between NULL and empty
381 column values as Tcl lacks a NULL representation. The default
382 representation for NULL values is an empty string.</p>
387 ##############################################################################
388 METHOD onecolumn {
390 <p>The "onecolumn" method works like "eval" in that it evaluates the
391 SQL query statement given as its argument. The difference is that
392 "onecolumn" returns a single element which is the first column of the
393 first row of the query result.</p>
395 <p>This is a convenience method. It saves the user from having to
396 do a "<tt>[lindex&nbsp;...&nbsp;0]</tt>" on the results of an "eval"
397 in order to extract a single column result.</p>
400 ##############################################################################
401 METHOD changes {
403 <p>The "changes" method returns an integer which is the number of rows
404 in the database that were inserted, deleted, and/or modified by the most
405 recent "eval" method.</p>
408 ##############################################################################
409 METHOD total_changes {
411 <p>The "total_changes" method returns an integer which is the number of rows
412 in the database that were inserted, deleted, and/or modified since the
413 current database connection was first opened.</p>
416 ##############################################################################
417 METHOD authorizer {
419 <p>The "authorizer" method provides access to the sqlite3_set_authorizer
420 C/C++ interface. The argument to authorizer is the name of a procedure that
421 is called when SQL statements are being compiled in order to authorize
422 certain operations. The callback procedure takes 5 arguments which describe
423 the operation being coded. If the callback returns the text string
424 "SQLITE_OK", then the operation is allowed. If it returns "SQLITE_IGNORE",
425 then the operation is silently disabled. If the return is "SQLITE_DENY"
426 then the compilation fails with an error.
427 </p>
429 <p>If the argument is an empty string then the authorizer is disabled.
430 If the argument is omitted, then the current authorizer is returned.</p>
433 ##############################################################################
434 METHOD progress {
436 <p>This method registers a callback that is invoked periodically during
437 query processing. There are two arguments: the number of SQLite virtual
438 machine opcodes between invocations, and the TCL command to invoke.
439 Setting the progress callback to an empty string disables it.</p>
441 <p>The progress callback can be used to display the status of a lengthy
442 query or to process GUI events during a lengthy query.</p>
446 ##############################################################################
447 METHOD collate {
449 <p>This method registers new text collating sequences. There are
450 two arguments: the name of the collating sequence and the name of a
451 TCL procedure that implements a comparison function for the collating
452 sequence.
453 </p>
455 <p>For example, the following code implements a collating sequence called
456 "NOCASE" that sorts in text order without regard to case:
457 </p>
459 <blockquote><b>
460 proc nocase_compare {a b} {<br>
461 &nbsp;&nbsp;&nbsp;&nbsp;return [string compare [string tolower $a] [string tolower $b]]<br>
462 }<br>
463 db collate NOCASE nocase_compare<br>
464 </b></blockquote>
467 ##############################################################################
468 METHOD collation_needed {
470 <p>This method registers a callback routine that is invoked when the SQLite
471 engine needs a particular collating sequence but does not have that
472 collating sequence registered. The callback can register the collating
473 sequence. The callback is invoked with a single parameter which is the
474 name of the needed collating sequence.</p>
477 ##############################################################################
478 METHOD commit_hook {
480 <p>This method registers a callback routine that is invoked just before
481 SQLite tries to commit changes to a database. If the callback throws
482 an exception or returns a non-zero result, then the transaction rolls back
483 rather than commit.</p>
486 ##############################################################################
487 METHOD errorcode {
489 <p>This method returns the numeric error code that resulted from the most
490 recent SQLite operation.</p>
493 ##############################################################################
494 METHOD trace {
496 <p>The "trace" method registers a callback that is invoked as each SQL
497 statement is compiled. The text of the SQL is appended as a single string
498 to the command before it is invoked. This can be used (for example) to
499 keep a log of all SQL operations that an application performs.
500 </p>
504 footer $rcsid