aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/sqlite/unix/sqlite-3.5.1/www/sqlite.tcl
diff options
context:
space:
mode:
authordan miller2007-10-20 02:49:29 +0000
committerdan miller2007-10-20 02:49:29 +0000
commite36d23a85ebff914d74bb541558c2b6082b78edb (patch)
tree54b58fdf162e78af64055282a6035c8d2443389d /libraries/sqlite/unix/sqlite-3.5.1/www/sqlite.tcl
parent* Fixed an issue whereby avatar chat distances were being calculated against ... (diff)
downloadopensim-SC-e36d23a85ebff914d74bb541558c2b6082b78edb.zip
opensim-SC-e36d23a85ebff914d74bb541558c2b6082b78edb.tar.gz
opensim-SC-e36d23a85ebff914d74bb541558c2b6082b78edb.tar.bz2
opensim-SC-e36d23a85ebff914d74bb541558c2b6082b78edb.tar.xz
sqlite source (unix build) added to libraries
Diffstat (limited to 'libraries/sqlite/unix/sqlite-3.5.1/www/sqlite.tcl')
-rw-r--r--libraries/sqlite/unix/sqlite-3.5.1/www/sqlite.tcl582
1 files changed, 582 insertions, 0 deletions
diff --git a/libraries/sqlite/unix/sqlite-3.5.1/www/sqlite.tcl b/libraries/sqlite/unix/sqlite-3.5.1/www/sqlite.tcl
new file mode 100644
index 0000000..eed1cd7
--- /dev/null
+++ b/libraries/sqlite/unix/sqlite-3.5.1/www/sqlite.tcl
@@ -0,0 +1,582 @@
1#
2# Run this Tcl script to generate the sqlite.html file.
3#
4set rcsid {$Id: sqlite.tcl,v 1.25 2007/01/08 14:31:36 drh Exp $}
5source common.tcl
6header {sqlite3: A command-line access program for SQLite databases}
7puts {
8<h2>sqlite3: A command-line access program for SQLite databases</h2>
9
10<p>The SQLite library includes a simple command-line utility named
11<b>sqlite3</b> that allows the user to manually enter and execute SQL
12commands against an SQLite database. This document provides a brief
13introduction on how to use <b>sqlite3</b>.
14
15<h3>Getting Started</h3>
16
17<p>To start the <b>sqlite3</b> program, just type "sqlite3" followed by
18the name the file that holds the SQLite database. If the file does
19not exist, a new one is created automatically.
20The <b>sqlite3</b> program will
21then prompt you to enter SQL. Type in SQL statements (terminated by a
22semicolon), press "Enter" and the SQL will be executed.</p>
23
24<p>For example, to create a new SQLite database named "ex1"
25with a single table named "tbl1", you might do this:</p>
26}
27
28proc Code {body} {
29 puts {<blockquote><tt>}
30 regsub -all {&} [string trim $body] {\&amp;} body
31 regsub -all {>} $body {\&gt;} body
32 regsub -all {<} $body {\&lt;} body
33 regsub -all {\(\(\(} $body {<b>} body
34 regsub -all {\)\)\)} $body {</b>} body
35 regsub -all { } $body {\&nbsp;} body
36 regsub -all \n $body <br>\n body
37 puts $body
38 puts {</tt></blockquote>}
39}
40
41Code {
42$ (((sqlite3 ex1)))
43SQLite version 3.3.10
44Enter ".help" for instructions
45sqlite> (((create table tbl1(one varchar(10), two smallint);)))
46sqlite> (((insert into tbl1 values('hello!',10);)))
47sqlite> (((insert into tbl1 values('goodbye', 20);)))
48sqlite> (((select * from tbl1;)))
49hello!|10
50goodbye|20
51sqlite>
52}
53
54puts {
55<p>You can terminate the sqlite3 program by typing your systems
56End-Of-File character (usually a Control-D) or the interrupt
57character (usually a Control-C).</p>
58
59<p>Make sure you type a semicolon at the end of each SQL command!
60The sqlite3 program looks for a semicolon to know when your SQL command is
61complete. If you omit the semicolon, sqlite3 will give you a
62continuation prompt and wait for you to enter more text to be
63added to the current SQL command. This feature allows you to
64enter SQL commands that span multiple lines. For example:</p>
65}
66
67Code {
68sqlite> (((CREATE TABLE tbl2 ()))
69 ...> ((( f1 varchar(30) primary key,)))
70 ...> ((( f2 text,)))
71 ...> ((( f3 real)))
72 ...> ((();)))
73sqlite>
74}
75
76puts {
77
78<h3>Aside: Querying the SQLITE_MASTER table</h3>
79
80<p>The database schema in an SQLite database is stored in
81a special table named "sqlite_master".
82You can execute "SELECT" statements against the
83special sqlite_master table just like any other table
84in an SQLite database. For example:</p>
85}
86
87Code {
88$ (((sqlite3 ex1)))
89SQlite vresion 3.3.10
90Enter ".help" for instructions
91sqlite> (((select * from sqlite_master;)))
92 type = table
93 name = tbl1
94tbl_name = tbl1
95rootpage = 3
96 sql = create table tbl1(one varchar(10), two smallint)
97sqlite>
98}
99
100puts {
101<p>
102But you cannot execute DROP TABLE, UPDATE, INSERT or DELETE against
103the sqlite_master table. The sqlite_master
104table is updated automatically as you create or drop tables and
105indices from the database. You can not make manual changes
106to the sqlite_master table.
107</p>
108
109<p>
110The schema for TEMPORARY tables is not stored in the "sqlite_master" table
111since TEMPORARY tables are not visible to applications other than the
112application that created the table. The schema for TEMPORARY tables
113is stored in another special table named "sqlite_temp_master". The
114"sqlite_temp_master" table is temporary itself.
115</p>
116
117<h3>Special commands to sqlite3</h3>
118
119<p>
120Most of the time, sqlite3 just reads lines of input and passes them
121on to the SQLite library for execution.
122But if an input line begins with a dot ("."), then
123that line is intercepted and interpreted by the sqlite3 program itself.
124These "dot commands" are typically used to change the output format
125of queries, or to execute certain prepackaged query statements.
126</p>
127
128<p>
129For a listing of the available dot commands, you can enter ".help"
130at any time. For example:
131</p>}
132
133Code {
134sqlite> (((.help)))
135.bail ON|OFF Stop after hitting an error. Default OFF
136.databases List names and files of attached databases
137.dump ?TABLE? ... Dump the database in an SQL text format
138.echo ON|OFF Turn command echo on or off
139.exit Exit this program
140.explain ON|OFF Turn output mode suitable for EXPLAIN on or off.
141.header(s) ON|OFF Turn display of headers on or off
142.help Show this message
143.import FILE TABLE Import data from FILE into TABLE
144.indices TABLE Show names of all indices on TABLE
145.load FILE ?ENTRY? Load an extension library
146.mode MODE ?TABLE? Set output mode where MODE is one of:
147 csv Comma-separated values
148 column Left-aligned columns. (See .width)
149 html HTML <table> code
150 insert SQL insert statements for TABLE
151 line One value per line
152 list Values delimited by .separator string
153 tabs Tab-separated values
154 tcl TCL list elements
155.nullvalue STRING Print STRING in place of NULL values
156.output FILENAME Send output to FILENAME
157.output stdout Send output to the screen
158.prompt MAIN CONTINUE Replace the standard prompts
159.quit Exit this program
160.read FILENAME Execute SQL in FILENAME
161.schema ?TABLE? Show the CREATE statements
162.separator STRING Change separator used by output mode and .import
163.show Show the current values for various settings
164.tables ?PATTERN? List names of tables matching a LIKE pattern
165.timeout MS Try opening locked tables for MS milliseconds
166.width NUM NUM ... Set column widths for "column" mode
167sqlite>
168}
169
170puts {
171<h3>Changing Output Formats</h3>
172
173<p>The sqlite3 program is able to show the results of a query
174in eight different formats: "csv", "column", "html", "insert",
175"line", "tabs", and "tcl".
176You can use the ".mode" dot command to switch between these output
177formats.</p>
178
179<p>The default output mode is "list". In
180list mode, each record of a query result is written on one line of
181output and each column within that record is separated by a specific
182separator string. The default separator is a pipe symbol ("|").
183List mode is especially useful when you are going to send the output
184of a query to another program (such as AWK) for additional processing.</p>}
185
186Code {
187sqlite> (((.mode list)))
188sqlite> (((select * from tbl1;)))
189hello|10
190goodbye|20
191sqlite>
192}
193
194puts {
195<p>You can use the ".separator" dot command to change the separator
196for list mode. For example, to change the separator to a comma and
197a space, you could do this:</p>}
198
199Code {
200sqlite> (((.separator ", ")))
201sqlite> (((select * from tbl1;)))
202hello, 10
203goodbye, 20
204sqlite>
205}
206
207puts {
208<p>In "line" mode, each column in a row of the database
209is shown on a line by itself. Each line consists of the column
210name, an equal sign and the column data. Successive records are
211separated by a blank line. Here is an example of line mode
212output:</p>}
213
214Code {
215sqlite> (((.mode line)))
216sqlite> (((select * from tbl1;)))
217one = hello
218two = 10
219
220one = goodbye
221two = 20
222sqlite>
223}
224
225puts {
226<p>In column mode, each record is shown on a separate line with the
227data aligned in columns. For example:</p>}
228
229Code {
230sqlite> (((.mode column)))
231sqlite> (((select * from tbl1;)))
232one two
233---------- ----------
234hello 10
235goodbye 20
236sqlite>
237}
238
239puts {
240<p>By default, each column is at least 10 characters wide.
241Data that is too wide to fit in a column is truncated. You can
242adjust the column widths using the ".width" command. Like this:</p>}
243
244Code {
245sqlite> (((.width 12 6)))
246sqlite> (((select * from tbl1;)))
247one two
248------------ ------
249hello 10
250goodbye 20
251sqlite>
252}
253
254puts {
255<p>The ".width" command in the example above sets the width of the first
256column to 12 and the width of the second column to 6. All other column
257widths were unaltered. You can gives as many arguments to ".width" as
258necessary to specify the widths of as many columns as are in your
259query results.</p>
260
261<p>If you specify a column a width of 0, then the column
262width is automatically adjusted to be the maximum of three
263numbers: 10, the width of the header, and the width of the
264first row of data. This makes the column width self-adjusting.
265The default width setting for every column is this
266auto-adjusting 0 value.</p>
267
268<p>The column labels that appear on the first two lines of output
269can be turned on and off using the ".header" dot command. In the
270examples above, the column labels are on. To turn them off you
271could do this:</p>}
272
273Code {
274sqlite> (((.header off)))
275sqlite> (((select * from tbl1;)))
276hello 10
277goodbye 20
278sqlite>
279}
280
281puts {
282<p>Another useful output mode is "insert". In insert mode, the output
283is formatted to look like SQL INSERT statements. You can use insert
284mode to generate text that can later be used to input data into a
285different database.</p>
286
287<p>When specifying insert mode, you have to give an extra argument
288which is the name of the table to be inserted into. For example:</p>
289}
290
291Code {
292sqlite> (((.mode insert new_table)))
293sqlite> (((select * from tbl1;)))
294INSERT INTO 'new_table' VALUES('hello',10);
295INSERT INTO 'new_table' VALUES('goodbye',20);
296sqlite>
297}
298
299puts {
300<p>The last output mode is "html". In this mode, sqlite3 writes
301the results of the query as an XHTML table. The beginning
302&lt;TABLE&gt; and the ending &lt;/TABLE&gt; are not written, but
303all of the intervening &lt;TR&gt;s, &lt;TH&gt;s, and &lt;TD&gt;s
304are. The html output mode is envisioned as being useful for
305CGI.</p>
306}
307
308puts {
309<h3>Writing results to a file</h3>
310
311<p>By default, sqlite3 sends query results to standard output. You
312can change this using the ".output" command. Just put the name of
313an output file as an argument to the .output command and all subsequent
314query results will be written to that file. Use ".output stdout" to
315begin writing to standard output again. For example:</p>}
316
317Code {
318sqlite> (((.mode list)))
319sqlite> (((.separator |)))
320sqlite> (((.output test_file_1.txt)))
321sqlite> (((select * from tbl1;)))
322sqlite> (((.exit)))
323$ (((cat test_file_1.txt)))
324hello|10
325goodbye|20
326$
327}
328
329puts {
330<h3>Querying the database schema</h3>
331
332<p>The sqlite3 program provides several convenience commands that
333are useful for looking at the schema of the database. There is
334nothing that these commands do that cannot be done by some other
335means. These commands are provided purely as a shortcut.</p>
336
337<p>For example, to see a list of the tables in the database, you
338can enter ".tables".</p>
339}
340
341Code {
342sqlite> (((.tables)))
343tbl1
344tbl2
345sqlite>
346}
347
348puts {
349<p>The ".tables" command is similar to setting list mode then
350executing the following query:</p>
351
352<blockquote><pre>
353SELECT name FROM sqlite_master
354WHERE type IN ('table','view') AND name NOT LIKE 'sqlite_%'
355UNION ALL
356SELECT name FROM sqlite_temp_master
357WHERE type IN ('table','view')
358ORDER BY 1
359</pre></blockquote>
360
361<p>In fact, if you look at the source code to the sqlite3 program
362(found in the source tree in the file src/shell.c) you'll find
363exactly the above query.</p>
364
365<p>The ".indices" command works in a similar way to list all of
366the indices for a particular table. The ".indices" command takes
367a single argument which is the name of the table for which the
368indices are desired. Last, but not least, is the ".schema" command.
369With no arguments, the ".schema" command shows the original CREATE TABLE
370and CREATE INDEX statements that were used to build the current database.
371If you give the name of a table to ".schema", it shows the original
372CREATE statement used to make that table and all if its indices.
373We have:</p>}
374
375Code {
376sqlite> (((.schema)))
377create table tbl1(one varchar(10), two smallint)
378CREATE TABLE tbl2 (
379 f1 varchar(30) primary key,
380 f2 text,
381 f3 real
382)
383sqlite> (((.schema tbl2)))
384CREATE TABLE tbl2 (
385 f1 varchar(30) primary key,
386 f2 text,
387 f3 real
388)
389sqlite>
390}
391
392puts {
393<p>The ".schema" command accomplishes the same thing as setting
394list mode, then entering the following query:</p>
395
396<blockquote><pre>
397SELECT sql FROM
398 (SELECT * FROM sqlite_master UNION ALL
399 SELECT * FROM sqlite_temp_master)
400WHERE type!='meta'
401ORDER BY tbl_name, type DESC, name
402</pre></blockquote>
403
404<p>Or, if you give an argument to ".schema" because you only
405want the schema for a single table, the query looks like this:</p>
406
407<blockquote><pre>
408SELECT sql FROM
409 (SELECT * FROM sqlite_master UNION ALL
410 SELECT * FROM sqlite_temp_master)
411WHERE type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'
412ORDER BY substr(type,2,1), name
413</pre></blockquote>
414
415<p>
416You can supply an argument to the .schema command. If you do, the
417query looks like this:
418</p>
419
420<blockquote><pre>
421SELECT sql FROM
422 (SELECT * FROM sqlite_master UNION ALL
423 SELECT * FROM sqlite_temp_master)
424WHERE tbl_name LIKE '%s'
425 AND type!='meta' AND sql NOT NULL AND name NOT LIKE 'sqlite_%'
426ORDER BY substr(type,2,1), name
427</pre></blockquote>
428
429<p>The "%s" in the query is replace by your argument. This allows you
430to view the schema for some subset of the database.</p>
431}
432
433Code {
434sqlite> (((.schema %abc%)))
435}
436
437puts {
438<p>
439Along these same lines,
440the ".table" command also accepts a pattern as its first argument.
441If you give an argument to the .table command, a "%" is both
442appended and prepended and a LIKE clause is added to the query.
443This allows you to list only those tables that match a particular
444pattern.</p>
445
446<p>The ".databases" command shows a list of all databases open in
447the current connection. There will always be at least 2. The first
448one is "main", the original database opened. The second is "temp",
449the database used for temporary tables. There may be additional
450databases listed for databases attached using the ATTACH statement.
451The first output column is the name the database is attached with,
452and the second column is the filename of the external file.</p>}
453
454Code {
455sqlite> (((.databases)))
456}
457
458puts {
459<h3>Converting An Entire Database To An ASCII Text File</h3>
460
461<p>Use the ".dump" command to convert the entire contents of a
462database into a single ASCII text file. This file can be converted
463back into a database by piping it back into <b>sqlite3</b>.</p>
464
465<p>A good way to make an archival copy of a database is this:</p>
466}
467
468Code {
469$ (((echo '.dump' | sqlite3 ex1 | gzip -c >ex1.dump.gz)))
470}
471
472puts {
473<p>This generates a file named <b>ex1.dump.gz</b> that contains everything
474you need to reconstruct the database at a later time, or on another
475machine. To reconstruct the database, just type:</p>
476}
477
478Code {
479$ (((zcat ex1.dump.gz | sqlite3 ex2)))
480}
481
482puts {
483<p>The text format is pure SQL so you
484can also use the .dump command to export an SQLite database
485into other popular SQL database engines. Like this:</p>
486}
487
488Code {
489$ (((createdb ex2)))
490$ (((sqlite3 ex1 .dump | psql ex2)))
491}
492
493puts {
494<h3>Other Dot Commands</h3>
495
496<p>The ".explain" dot command can be used to set the output mode
497to "column" and to set the column widths to values that are reasonable
498for looking at the output of an EXPLAIN command. The EXPLAIN command
499is an SQLite-specific SQL extension that is useful for debugging. If any
500regular SQL is prefaced by EXPLAIN, then the SQL command is parsed and
501analyzed but is not executed. Instead, the sequence of virtual machine
502instructions that would have been used to execute the SQL command are
503returned like a query result. For example:</p>}
504
505Code {
506sqlite> (((.explain)))
507sqlite> (((explain delete from tbl1 where two<20;)))
508addr opcode p1 p2 p3
509---- ------------ ----- ----- -------------------------------------
5100 ListOpen 0 0
5111 Open 0 1 tbl1
5122 Next 0 9
5133 Field 0 1
5144 Integer 20 0
5155 Ge 0 2
5166 Key 0 0
5177 ListWrite 0 0
5188 Goto 0 2
5199 Noop 0 0
52010 ListRewind 0 0
52111 ListRead 0 14
52212 Delete 0 0
52313 Goto 0 11
52414 ListClose 0 0
525}
526
527puts {
528
529<p>The ".timeout" command sets the amount of time that the <b>sqlite3</b>
530program will wait for locks to clear on files it is trying to access
531before returning an error. The default value of the timeout is zero so
532that an error is returned immediately if any needed database table or
533index is locked.</p>
534
535<p>And finally, we mention the ".exit" command which causes the
536sqlite3 program to exit.</p>
537
538<h3>Using sqlite3 in a shell script</h3>
539
540<p>
541One way to use sqlite3 in a shell script is to use "echo" or
542"cat" to generate a sequence of commands in a file, then invoke sqlite3
543while redirecting input from the generated command file. This
544works fine and is appropriate in many circumstances. But as
545an added convenience, sqlite3 allows a single SQL command to be
546entered on the command line as a second argument after the
547database name. When the sqlite3 program is launched with two
548arguments, the second argument is passed to the SQLite library
549for processing, the query results are printed on standard output
550in list mode, and the program exits. This mechanism is designed
551to make sqlite3 easy to use in conjunction with programs like
552"awk". For example:</p>}
553
554Code {
555$ (((sqlite3 ex1 'select * from tbl1' |)))
556> ((( awk '{printf "<tr><td>%s<td>%s\n",$1,$2 }')))
557<tr><td>hello<td>10
558<tr><td>goodbye<td>20
559$
560}
561
562puts {
563<h3>Ending shell commands</h3>
564
565<p>
566SQLite commands are normally terminated by a semicolon. In the shell
567you can also use the word "GO" (case-insensitive) or a slash character
568"/" on a line by itself to end a command. These are used by SQL Server
569and Oracle, respectively. These won't work in <b>sqlite3_exec()</b>,
570because the shell translates these into a semicolon before passing them
571to that function.</p>
572}
573
574puts {
575<h3>Compiling the sqlite3 program from sources</h3>
576
577<p>
578The sqlite3 program is built automatically when you compile the
579SQLite library. Just get a copy of the source tree, run
580"configure" and then "make".</p>
581}
582footer $rcsid