aboutsummaryrefslogtreecommitdiffstatshomepage
path: root/libraries/sqlite/unix/sqlite-3.5.1/www/faq.tcl
diff options
context:
space:
mode:
authordan miller2007-10-21 08:36:32 +0000
committerdan miller2007-10-21 08:36:32 +0000
commit2f8d7092bc2c9609fa98d6888106b96f38b22828 (patch)
treeda6c37579258cc965b52a75aee6135fe44237698 /libraries/sqlite/unix/sqlite-3.5.1/www/faq.tcl
parent* Committing new PolicyManager based on an ACL system. (diff)
downloadopensim-SC-2f8d7092bc2c9609fa98d6888106b96f38b22828.zip
opensim-SC-2f8d7092bc2c9609fa98d6888106b96f38b22828.tar.gz
opensim-SC-2f8d7092bc2c9609fa98d6888106b96f38b22828.tar.bz2
opensim-SC-2f8d7092bc2c9609fa98d6888106b96f38b22828.tar.xz
libraries moved to opensim-libs, a new repository
Diffstat (limited to 'libraries/sqlite/unix/sqlite-3.5.1/www/faq.tcl')
-rw-r--r--libraries/sqlite/unix/sqlite-3.5.1/www/faq.tcl463
1 files changed, 0 insertions, 463 deletions
diff --git a/libraries/sqlite/unix/sqlite-3.5.1/www/faq.tcl b/libraries/sqlite/unix/sqlite-3.5.1/www/faq.tcl
deleted file mode 100644
index dd2c7cc..0000000
--- a/libraries/sqlite/unix/sqlite-3.5.1/www/faq.tcl
+++ /dev/null
@@ -1,463 +0,0 @@
1#
2# Run this script to generated a faq.html output file
3#
4set rcsid {$Id: faq.tcl,v 1.40 2007/09/04 01:58:27 drh Exp $}
5source common.tcl
6header {SQLite Frequently Asked Questions</title>}
7
8set cnt 1
9proc faq {question answer} {
10 set ::faq($::cnt) [list [string trim $question] [string trim $answer]]
11 incr ::cnt
12}
13
14#############
15# Enter questions and answers here.
16
17faq {
18 How do I create an AUTOINCREMENT field.
19} {
20 <p>Short answer: A column declared INTEGER PRIMARY KEY will
21 autoincrement.</p>
22
23 <p>Here is the long answer:
24 If you declare a column of a table to be INTEGER PRIMARY KEY, then
25 whenever you insert a NULL
26 into that column of the table, the NULL is automatically converted
27 into an integer which is one greater than the largest value of that
28 column over all other rows in the table, or 1 if the table is empty.
29 (If the largest possible integer key, 9223372036854775807, then an
30 unused key value is chosen at random.)
31 For example, suppose you have a table like this:
32<blockquote><pre>
33CREATE TABLE t1(
34 a INTEGER PRIMARY KEY,
35 b INTEGER
36);
37</pre></blockquote>
38 <p>With this table, the statement</p>
39<blockquote><pre>
40INSERT INTO t1 VALUES(NULL,123);
41</pre></blockquote>
42 <p>is logically equivalent to saying:</p>
43<blockquote><pre>
44INSERT INTO t1 VALUES((SELECT max(a) FROM t1)+1,123);
45</pre></blockquote>
46
47 <p>There is a new API function named
48 <a href="capi3ref.html#sqlite3_last_insert_rowid">
49 sqlite3_last_insert_rowid()</a> which will return the integer key
50 for the most recent insert operation.</p>
51
52 <p>Note that the integer key is one greater than the largest
53 key that was in the table just prior to the insert. The new key
54 will be unique over all keys currently in the table, but it might
55 overlap with keys that have been previously deleted from the
56 table. To create keys that are unique over the lifetime of the
57 table, add the AUTOINCREMENT keyword to the INTEGER PRIMARY KEY
58 declaration. Then the key chosen will be one more than than the
59 largest key that has ever existed in that table. If the largest
60 possible key has previously existed in that table, then the INSERT
61 will fail with an SQLITE_FULL error code.</p>
62}
63
64faq {
65 What datatypes does SQLite support?
66} {
67 <p>See <a href="datatype3.html">http://www.sqlite.org/datatype3.html</a>.</p>
68}
69
70faq {
71 SQLite lets me insert a string into a database column of type integer!
72} {
73 <p>This is a feature, not a bug. SQLite does not enforce data type
74 constraints. Any data can be
75 inserted into any column. You can put arbitrary length strings into
76 integer columns, floating point numbers in boolean columns, or dates
77 in character columns. The datatype you assign to a column in the
78 CREATE TABLE command does not restrict what data can be put into
79 that column. Every column is able to hold
80 an arbitrary length string. (There is one exception: Columns of
81 type INTEGER PRIMARY KEY may only hold a 64-bit signed integer.
82 An error will result
83 if you try to put anything other than an integer into an
84 INTEGER PRIMARY KEY column.)</p>
85
86 <p>But SQLite does use the declared type of a column as a hint
87 that you prefer values in that format. So, for example, if a
88 column is of type INTEGER and you try to insert a string into
89 that column, SQLite will attempt to convert the string into an
90 integer. If it can, it inserts the integer instead. If not,
91 it inserts the string. This feature is sometimes
92 call <a href="datatype3.html#affinity">type or column affinity</a>.
93 </p>
94}
95
96faq {
97 Why doesn't SQLite allow me to use '0' and '0.0' as the primary
98 key on two different rows of the same table?
99} {
100 <p>Your primary key must have a numeric type. Change the datatype of
101 your primary key to TEXT and it should work.</p>
102
103 <p>Every row must have a unique primary key. For a column with a
104 numeric type, SQLite thinks that <b>'0'</b> and <b>'0.0'</b> are the
105 same value because they compare equal to one another numerically.
106 (See the previous question.) Hence the values are not unique.</p>
107}
108
109
110faq {
111 Can multiple applications or multiple instances of the same
112 application access a single database file at the same time?
113} {
114 <p>Multiple processes can have the same database open at the same
115 time. Multiple processes can be doing a SELECT
116 at the same time. But only one process can be making changes to
117 the database at any moment in time, however.</p>
118
119 <p>SQLite uses reader/writer locks to control access to the database.
120 (Under Win95/98/ME which lacks support for reader/writer locks, a
121 probabilistic simulation is used instead.)
122 But use caution: this locking mechanism might
123 not work correctly if the database file is kept on an NFS filesystem.
124 This is because fcntl() file locking is broken on many NFS implementations.
125 You should avoid putting SQLite database files on NFS if multiple
126 processes might try to access the file at the same time. On Windows,
127 Microsoft's documentation says that locking may not work under FAT
128 filesystems if you are not running the Share.exe daemon. People who
129 have a lot of experience with Windows tell me that file locking of
130 network files is very buggy and is not dependable. If what they
131 say is true, sharing an SQLite database between two or more Windows
132 machines might cause unexpected problems.</p>
133
134 <p>We are aware of no other <i>embedded</i> SQL database engine that
135 supports as much concurrancy as SQLite. SQLite allows multiple processes
136 to have the database file open at once, and for multiple processes to
137 read the database at once. When any process wants to write, it must
138 lock the entire database file for the duration of its update. But that
139 normally only takes a few milliseconds. Other processes just wait on
140 the writer to finish then continue about their business. Other embedded
141 SQL database engines typically only allow a single process to connect to
142 the database at once.</p>
143
144 <p>However, client/server database engines (such as PostgreSQL, MySQL,
145 or Oracle) usually support a higher level of concurrency and allow
146 multiple processes to be writing to the same database at the same time.
147 This is possible in a client/server database because there is always a
148 single well-controlled server process available to coordinate access.
149 If your application has a need for a lot of concurrency, then you should
150 consider using a client/server database. But experience suggests that
151 most applications need much less concurrency than their designers imagine.
152 </p>
153
154 <p>When SQLite tries to access a file that is locked by another
155 process, the default behavior is to return SQLITE_BUSY. You can
156 adjust this behavior from C code using the
157 <a href="capi3ref.html#sqlite3_busy_handler">sqlite3_busy_handler()</a> or
158 <a href="capi3ref.html#sqlite3_busy_timeout">sqlite3_busy_timeout()</a>
159 API functions.</p>
160}
161
162faq {
163 Is SQLite threadsafe?
164} {
165 <p>Yes. Sometimes. In order to be thread-safe, SQLite must be compiled
166 with the SQLITE_THREADSAFE preprocessor macro set to 1. Both the windows
167 and linux precompiled binaries in the distribution are compiled this way.
168 If you are unsure if the SQLite library you are linking against is compiled
169 to be threadsafe you can call the
170 <a href="capi3ref.html#sqlite3_threadsafe">sqlite3_threadsafe()</a>
171 interface to find out.
172 </p>
173
174 <p>Prior to version 3.3.1,
175 an <b>sqlite3</b> structure could only be used in the same thread
176 that called <a href="capi3ref.html#sqlite3_open">sqlite3_open</a>
177 to create it.
178 You could not open a
179 database in one thread then pass the handle off to another thread for
180 it to use. This was due to limitations (bugs?) in many common threading
181 implementations such as on RedHat9. Specifically, an fcntl() lock
182 created by one thread cannot be removed or modified by a different
183 thread on the troublesome systems. And since SQLite uses fcntl()
184 locks heavily for concurrency control, serious problems arose if you
185 start moving database connections across threads.</p>
186
187 <p>The restriction on moving database connections across threads
188 was relaxed somewhat in version 3.3.1. With that and subsequent
189 versions, it is safe to move a connection handle across threads
190 as long as the connection is not holding any fcntl() locks. You
191 can safely assume that no locks are being held if no
192 transaction is pending and all statements have been finalized.</p>
193
194 <p>Under UNIX, you should not carry an open SQLite database across
195 a fork() system call into the child process. Problems will result
196 if you do.</p>
197}
198
199faq {
200 How do I list all tables/indices contained in an SQLite database
201} {
202 <p>If you are running the <b>sqlite3</b> command-line access program
203 you can type "<b>.tables</b>" to get a list of all tables. Or you
204 can type "<b>.schema</b>" to see the complete database schema including
205 all tables and indices. Either of these commands can be followed by
206 a LIKE pattern that will restrict the tables that are displayed.</p>
207
208 <p>From within a C/C++ program (or a script using Tcl/Ruby/Perl/Python
209 bindings) you can get access to table and index names by doing a SELECT
210 on a special table named "<b>SQLITE_MASTER</b>". Every SQLite database
211 has an SQLITE_MASTER table that defines the schema for the database.
212 The SQLITE_MASTER table looks like this:</p>
213<blockquote><pre>
214CREATE TABLE sqlite_master (
215 type TEXT,
216 name TEXT,
217 tbl_name TEXT,
218 rootpage INTEGER,
219 sql TEXT
220);
221</pre></blockquote>
222 <p>For tables, the <b>type</b> field will always be <b>'table'</b> and the
223 <b>name</b> field will be the name of the table. So to get a list of
224 all tables in the database, use the following SELECT command:</p>
225<blockquote><pre>
226SELECT name FROM sqlite_master
227WHERE type='table'
228ORDER BY name;
229</pre></blockquote>
230 <p>For indices, <b>type</b> is equal to <b>'index'</b>, <b>name</b> is the
231 name of the index and <b>tbl_name</b> is the name of the table to which
232 the index belongs. For both tables and indices, the <b>sql</b> field is
233 the text of the original CREATE TABLE or CREATE INDEX statement that
234 created the table or index. For automatically created indices (used
235 to implement the PRIMARY KEY or UNIQUE constraints) the <b>sql</b> field
236 is NULL.</p>
237
238 <p>The SQLITE_MASTER table is read-only. You cannot change this table
239 using UPDATE, INSERT, or DELETE. The table is automatically updated by
240 CREATE TABLE, CREATE INDEX, DROP TABLE, and DROP INDEX commands.</p>
241
242 <p>Temporary tables do not appear in the SQLITE_MASTER table. Temporary
243 tables and their indices and triggers occur in another special table
244 named SQLITE_TEMP_MASTER. SQLITE_TEMP_MASTER works just like SQLITE_MASTER
245 except that it is only visible to the application that created the
246 temporary tables. To get a list of all tables, both permanent and
247 temporary, one can use a command similar to the following:
248<blockquote><pre>
249SELECT name FROM
250 (SELECT * FROM sqlite_master UNION ALL
251 SELECT * FROM sqlite_temp_master)
252WHERE type='table'
253ORDER BY name
254</pre></blockquote>
255}
256
257faq {
258 Are there any known size limits to SQLite databases?
259} {
260 <p>See <a href="limits.html">limits.html</a> for a full discussion of
261 the limits of SQLite.</p>
262}
263
264faq {
265 What is the maximum size of a VARCHAR in SQLite?
266} {
267 <p>SQLite does not enforce the length of a VARCHAR. You can declare
268 a VARCHAR(10) and SQLite will be happy to let you put 500 characters
269 in it. And it will keep all 500 characters intact - it never truncates.
270 </p>
271}
272
273faq {
274 Does SQLite support a BLOB type?
275} {
276 <p>SQLite versions 3.0 and later allow you to store BLOB data in any
277 column, even columns that are declared to hold some other type.</p>
278}
279
280faq {
281 How do I add or delete columns from an existing table in SQLite.
282} {
283 <p>SQLite has limited
284 <a href="lang_altertable.html">ALTER TABLE</a> support that you can
285 use to add a column to the end of a table or to change the name of
286 a table.
287 If you what make more complex changes the structure of a table,
288 you will have to recreate the
289 table. You can save existing data to a temporary table, drop the
290 old table, create the new table, then copy the data back in from
291 the temporary table.</p>
292
293 <p>For example, suppose you have a table named "t1" with columns
294 names "a", "b", and "c" and that you want to delete column "c" from
295 this table. The following steps illustrate how this could be done:
296 </p>
297
298 <blockquote><pre>
299BEGIN TRANSACTION;
300CREATE TEMPORARY TABLE t1_backup(a,b);
301INSERT INTO t1_backup SELECT a,b FROM t1;
302DROP TABLE t1;
303CREATE TABLE t1(a,b);
304INSERT INTO t1 SELECT a,b FROM t1_backup;
305DROP TABLE t1_backup;
306COMMIT;
307</pre></blockquote>
308}
309
310faq {
311 I deleted a lot of data but the database file did not get any
312 smaller. Is this a bug?
313} {
314 <p>No. When you delete information from an SQLite database, the
315 unused disk space is added to an internal "free-list" and is reused
316 the next time you insert data. The disk space is not lost. But
317 neither is it returned to the operating system.</p>
318
319 <p>If you delete a lot of data and want to shrink the database file,
320 run the <a href="lang_vacuum.html">VACUUM</a> command.
321 VACUUM will reconstruct
322 the database from scratch. This will leave the database with an empty
323 free-list and a file that is minimal in size. Note, however, that the
324 VACUUM can take some time to run (around a half second per megabyte
325 on the Linux box where SQLite is developed) and it can use up to twice
326 as much temporary disk space as the original file while it is running.
327 </p>
328
329 <p>As of SQLite version 3.1, an alternative to using the VACUUM command
330 is auto-vacuum mode, enabled using the
331 <a href="pragma.html#pragma_auto_vacuum">auto_vacuum pragma</a>.</p>
332}
333
334faq {
335 Can I use SQLite in my commercial product without paying royalties?
336} {
337 <p>Yes. SQLite is in the
338 <a href="copyright.html">public domain</a>. No claim of ownership is made
339 to any part of the code. You can do anything you want with it.</p>
340}
341
342faq {
343 How do I use a string literal that contains an embedded single-quote (')
344 character?
345} {
346 <p>The SQL standard specifies that single-quotes in strings are escaped
347 by putting two single quotes in a row. SQL works like the Pascal programming
348 language in the regard. SQLite follows this standard. Example:
349 </p>
350
351 <blockquote><pre>
352 INSERT INTO xyz VALUES('5 O''clock');
353 </pre></blockquote>
354}
355
356faq {What is an SQLITE_SCHEMA error, and why am I getting one?} {
357 <p>An SQLITE_SCHEMA error is returned when a
358 prepared SQL statement is no longer valid and cannot be executed.
359 When this occurs, the statement must be recompiled from SQL using
360 the
361 <a href="capi3ref.html#sqlite3_prepare">sqlite3_prepare()</a> API.
362 In SQLite version 3, an SQLITE_SCHEMA error can
363 only occur when using the
364 <a href="capi3ref.html#sqlite3_prepare">sqlite3_prepare()</a>/<a
365 href="capi3ref.html#sqlite3_step">sqlite3_step()</a>/<a
366 href="capi3ref.html#sqlite3_finalize">sqlite3_finalize()</a>
367 API to execute SQL, not when using the
368 <a href="capi3ref.html#sqlite3_exec">sqlite3_exec()</a>. This was not
369 the case in version 2.</p>
370
371 <p>The most common reason for a prepared statement to become invalid
372 is that the schema of the database was modified after the SQL was
373 prepared (possibly by another process). The other reasons this can
374 happen are:</p>
375 <ul>
376 <li>A database was <a href="lang_detach.html">DETACH</a>ed.
377 <li>The database was <a href="lang_vacuum.html">VACUUM</a>ed
378 <li>A user-function definition was deleted or changed.
379 <li>A collation sequence definition was deleted or changed.
380 <li>The authorization function was changed.
381 </ul>
382
383 <p>In all cases, the solution is to recompile the statement from SQL
384 and attempt to execute it again. Because a prepared statement can be
385 invalidated by another process changing the database schema, all code
386 that uses the
387 <a href="capi3ref.html#sqlite3_prepare">sqlite3_prepare()</a>/<a
388 href="capi3ref.html#sqlite3_step">sqlite3_step()</a>/<a
389 href="capi3ref.html#sqlite3_finalize">sqlite3_finalize()</a>
390 API should be prepared to handle SQLITE_SCHEMA errors. An example
391 of one approach to this follows:</p>
392
393 <blockquote><pre>
394
395 int rc;
396 sqlite3_stmt *pStmt;
397 char zSql[] = "SELECT .....";
398
399 do {
400 /* Compile the statement from SQL. Assume success. */
401 sqlite3_prepare(pDb, zSql, -1, &pStmt, 0);
402
403 while( SQLITE_ROW==sqlite3_step(pStmt) ){
404 /* Do something with the row of available data */
405 }
406
407 /* Finalize the statement. If an SQLITE_SCHEMA error has
408 ** occured, then the above call to sqlite3_step() will have
409 ** returned SQLITE_ERROR. sqlite3_finalize() will return
410 ** SQLITE_SCHEMA. In this case the loop will execute again.
411 */
412 rc = sqlite3_finalize(pStmt);
413 } while( rc==SQLITE_SCHEMA );
414
415 </pre></blockquote>
416}
417
418faq {Why does ROUND(9.95,1) return 9.9 instead of 10.0?
419 Shouldn't 9.95 round up?} {
420 <p>SQLite uses binary arithmetic and in binary, there is no
421 way to write 9.95 in a finite number of bits. The closest to
422 you can get to 9.95 in a 64-bit IEEE float (which is what
423 SQLite uses) is 9.949999999999999289457264239899814128875732421875.
424 So when you type "9.95", SQLite really understands the number to be
425 the much longer value shown above. And that value rounds down.</p>
426
427 <p>This kind of problem comes up all the time when dealing with
428 floating point binary numbers. The general rule to remember is
429 that most fractional numbers that have a finite representation in decimal
430 (a.k.a "base-10")
431 do not have a finite representation in binary (a.k.a "base-2").
432 And so they are
433 approximated using the closest binary number available. That
434 approximation is usually very close, but it will be slightly off
435 and in some cases can cause your results to be a little different
436 from what you might expect.</p>
437}
438
439# End of questions and answers.
440#############
441
442puts {<h2>Frequently Asked Questions</h2>}
443
444# puts {<DL COMPACT>}
445# for {set i 1} {$i<$cnt} {incr i} {
446# puts " <DT><A HREF=\"#q$i\">($i)</A></DT>"
447# puts " <DD>[lindex $faq($i) 0]</DD>"
448# }
449# puts {</DL>}
450puts {<OL>}
451for {set i 1} {$i<$cnt} {incr i} {
452 puts "<li><a href=\"#q$i\">[lindex $faq($i) 0]</a></li>"
453}
454puts {</OL>}
455
456for {set i 1} {$i<$cnt} {incr i} {
457 puts "<A NAME=\"q$i\"><HR />"
458 puts "<P><B>($i) [lindex $faq($i) 0]</B></P>\n"
459 puts "<BLOCKQUOTE>[lindex $faq($i) 1]</BLOCKQUOTE></LI>\n"
460}
461
462puts {</OL>}
463footer $rcsid