tcltest: do a better job of cleanup up after tests
[jimtcl.git] / README.sqlite
blob9fb83483bd351f737dd35cba6d4062949a73e07c
1 Jim Sqlite extension documentation.
2 Copyright 2005 Salvatore Sanfilippo <antirez@invece.org>
5 Overview
6 ~~~~~~~~
8 The Sqlite extension makes possible to work with sqlite (http://www.sqlite.org)
9 databases from Jim. SQLite is a small C library that implements a
10 self-contained, embeddable, zero-configuration SQL database engine. This
11 means it is perfect for embedded systems, and for stand-alone applications
12 that need the power of SQL without to use an external server like Mysql.
14 Basic usage
15 ~~~~~~~~~~~
17 The Sqlite extension exports an Object Based interface for databases. In order
18 to open a database use:
20   set f [sqlite3.open dbname]
22 The [sqlite3.open] command returns a db handle, that is a command name that
23 can be used to perform operations on the database. A real example:
25     . set db [sqlite3.open test.db]
26     sqlite.handle0
27     . $db query "SELECT * from tbl1"
28     {one hello! two 10} {one goodbye two 20}
30 In the second line the handle is used as a command name, followed
31 by the 'method' or 'subcommand' ("query" in the example), and the arguments.
33 The query method
34 ~~~~~~~~~~~~~~~~
36 The query method has the following signature:
38     $db query SqlQuery ?args?
40 The sql query may contain occurrences of "%s" that are substituted
41 in the actual query with the following arguments, quoted in order
42 to make sure that the query is correct even if these arguments contain
43 "'" characters. So for example it is possible to write:
45     . $db query "SELECT * from tbl1 WHERE one='%s'" hello!
46     {one hello! two 10}
48 Instead of hello! it is possible to use a string with embedded "'":
50     . $db query "SELECT * from tbl1 WHERE one='%s'" a'b
51     (no matches - the empty list is returned)
53 This does not work instead using the Tcl variable expansion in the string:
55     . $db query "SELECT * from tbl1 WHERE one='$foo'"
56     Runtime error, file "?", line 1:
57         near "b": syntax error
59 In order to obtain an actual '%' character in the query, there is just
60 to use two, like in "foo %% bar". This is the same as the [format] argument.
62 Specification of query results
63 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
65 In one of the above examples, the following query was used:
67     . $db query "SELECT * from tbl1"
68     {one hello! two 10} {one goodbye two 20}
70 As you can see the result of a query is a list of lists. Every
71 element of the list represents a row, as a list of key/value pairs,
72 so actually every row is a Jim dictionary.
74 The following example and generated output show how to take advantage
75 of this representation:
77     . set res [$db query "SELECT * from tbl1"]
78     {one hello! two 10} {one goodbye two 20}
79     . foreach row $res {puts "One: $row(one), Two: $row(two)"}
80     One: hello!, Two: 10
81     One: goodbye, Two: 20
83 To access every row sequentially is very simple, and field of a row
84 can be accessed using the $row(field) syntax.
86 The close method
87 ~~~~~~~~~~~~~~~~
89 In order to close the db, use the 'close' method that will have as side effect
90 to close the db and to remove the command associated with the db.
91 Just use:
93     $db close
95 Handling NULL values
96 ~~~~~~~~~~~~~~~~~~~~
98 In the SQL language there is a special value NULL that is not the empty
99 string, so how to represent it in a typeless language like Tcl?
100 For default this extension will use the empty string, but it is possible
101 to specify a different string for the NULL value.
103 In the above example there were two rows in the 'tbl1' table. Now
104 we can add using the "sqlite" command line client another one with
105 a NULL value:
107     sqlite> INSERT INTO tbl1 VALUES(NULL,30);
108     sqlite> .exit
110 That's what the sqlite extension will return for default:
112     . $db query "SELECT * from tbl1"
113     {one hello! two 10} {one goodbye two 20} {one {} two 30}
115 As you can see in the last row, the NULL is represented as {}, that's
116 the empty string. Using the -null option of the 'query' command we
117 can change this default, and tell the sqlite extension to represent
118 the NULL value as a different string:
120     . $db query -null <<NULL>> "SELECT * from tbl1"
121     {one hello! two 10} {one goodbye two 20} {one <<NULL>> two 30}
123 This way if the empty string has some semantical value for your
124 dataset you can change it.
126 Finding the ID of the last inserted row
127 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
129 This is as simple as:
131     . $db lastid
132     10
134 Number of rows changed by the most recent query
135 ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
137 This is also very simple, there is just to use the 'changes' method
138 without arguments.
140     . $db changes
141     5
143 Note that if you drop an entire table the number of changes will
144 be reported as zero, because of details of the sqlite implementation.
146 That's all,
147 Enjoy!
148 Salvatore Sanfilippo
150 p.s. this extension is just the work of some hour thanks to the cool
151 clean C API that sqlite exports. Thanks to the author of sqlite for this
152 great work.
154 In memory databases
155 ~~~~~~~~~~~~~~~~~~~
157 SQLite is able to create in-memory databases instead to use files.
158 This is of course faster and does not need the ability to write
159 to the filesystem. Of course this databases are only useful for
160 temp data.
162 In-memory DBs are used just like regular databases, just the name used to
163 open the database is :memory:. That's an example that does not use the
164 filesystem at all to create and work with the db.
166   package require sqlite3
167   set db [sqlite3.open :memory:]
168   $db query {CREATE TABLE plays (id, author, title)}
169   $db query {INSERT INTO plays (id, author, title) VALUES (1, 'Goethe', 'Faust');}
170   $db query {INSERT INTO plays (id, author, title) VALUES (2, 'Shakespeare', 'Hamlet');}
171   $db query {INSERT INTO plays (id, author, title) VALUES (3, 'Sophocles', 'Oedipus Rex');}
172   set res [$db query "SELECT * FROM plays"]
173   $db close
174   foreach r $res {puts $r(author)}
176 Of course once the Jim process is destroyed the database will no longer
177 exists.