faq.tcl 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386
  1. #
  2. # Run this script to generated a faq.html output file
  3. #
  4. set rcsid {$Id: faq.tcl,v 1.10 2002/04/25 00:21:50 drh Exp $}
  5. puts {<html>
  6. <head>
  7. <title>SQLite Frequently Asked Questions</title>
  8. </head>
  9. <body bgcolor="white">
  10. <h1 align="center">Frequently Asked Questions</h1>
  11. }
  12. puts "<p align=center>
  13. (This page was last modified on [lrange $rcsid 3 4] UTC)
  14. </p>"
  15. set cnt 1
  16. proc faq {question answer} {
  17. set ::faq($::cnt) [list [string trim $question] [string trim $answer]]
  18. incr ::cnt
  19. }
  20. #############
  21. # Enter questions and answers here.
  22. faq {
  23. How do I create an AUTOINCREMENT field.
  24. } {
  25. <p>Short answer: A column declared INTEGER PRIMARY KEY will
  26. autoincrement.</p>
  27. <p>Here is the long answer:
  28. Beginning with version SQLite 2.3.4, If you declare a column of
  29. a table to be INTEGER PRIMARY KEY, then whenever you insert a NULL
  30. into that column of the table, the NULL is automatically converted
  31. into an integer which is one greater than the largest value of that
  32. column over all other rows in the table, or 1 if the table is empty.
  33. For example, suppose you have a table like this:
  34. <blockquote><pre>
  35. CREATE TABLE t1(
  36. a INTEGER PRIMARY KEY,
  37. b INTEGER
  38. );
  39. </pre></blockquote>
  40. <p>With this table, the statement</p>
  41. <blockquote><pre>
  42. INSERT INTO t1 VALUES(NULL,123);
  43. </pre></blockquote>
  44. <p>is logically equivalent to saying:</p>
  45. <blockquote><pre>
  46. INSERT INTO t1 VALUES((SELECT max(a) FROM t1)+1,123);
  47. </pre></blockquote>
  48. <p>For SQLite version 2.2.0 through 2.3.3, if you insert a NULL into
  49. an INTEGER PRIMARY KEY column, the NULL will be changed to a unique
  50. integer, but it will a semi-random integer. Unique keys generated this
  51. way will not be sequential. For SQLite version 2.3.4 and beyond, the
  52. unique keys will be sequential until the largest key reaches a value
  53. of 2147483647. That is the largest 32-bit signed integer and cannot
  54. be incremented, so subsequent insert attempts will revert to the
  55. semi-random key generation algorithm of SQLite version 2.3.3 and
  56. earlier.</p>
  57. <p>Beginning with version 2.2.3, there is a new API function named
  58. <b>sqlite_last_insert_rowid()</b> which will return the integer key
  59. for the most recent insert operation. See the API documentation for
  60. details.</p>
  61. }
  62. faq {
  63. What datatypes does SQLite support?
  64. } {
  65. <p>SQLite is typeless. All data is stored as null-terminated strings.
  66. The datatype information that follows the column name in CREATE TABLE
  67. statements is ignored (mostly). You can put any type of data you want
  68. into any column, without regard to the declared datatype of that column.
  69. </p>
  70. <p>An exception to this rule is a column of type INTEGER PRIMARY KEY.
  71. Such columns must hold an integer. An attempt to put a non-integer
  72. value into an INTEGER PRIMARY KEY column will generate an error.</p>
  73. }
  74. faq {
  75. SQLite lets me insert a string into a database column of type integer!
  76. } {
  77. <p>This is a feature, not a bug. SQLite is typeless. Any data can be
  78. inserted into any column. You can put arbitrary length strings into
  79. integer columns, floating point numbers in boolean columns, or dates
  80. in character columns. The datatype you assign to a column in the
  81. CREATE TABLE command is (mostly) ignored. Every column is able to hold
  82. an arbitrary length string. (There is one exception: Columns of
  83. type INTEGER PRIMARY KEY may only hold an integer. An error will result
  84. if you try to put anything other than an integer into an
  85. INTEGER PRIMARY KEY column.)</p>
  86. <p>Because SQLite ignores data types, you can omit the data type definition
  87. from columns in CREATE TABLE statements. For example, instead of saying
  88. <blockquote><pre>
  89. CREATE TABLE t1(
  90. f1 int,
  91. f2 varchar(10),
  92. f3 boolean
  93. );
  94. </pre></blockquote>
  95. You can save yourself a lot of typing and formatting by omitting the
  96. data type declarations, like this:
  97. <blockquote><pre>
  98. CREATE TABLE t1(f1,f2,f3);
  99. </pre></blockquote>
  100. </p>
  101. }
  102. faq {
  103. Why does SQLite think that the expression '0'=='00' is TRUE?
  104. } {
  105. <p>This is a consequence of SQLite being typeless. All data is stored
  106. internally as a null-terminated string. There is no concept of
  107. separate data types for strings and numbers.</p>
  108. <p>When doing a comparison, SQLite looks at the string on both sides of
  109. the comparison operator. If both strings look like pure numeric
  110. values (with no extra punctuation or spacing) then the strings are
  111. converted to floating point numbers using <b>atof()</b> and the results
  112. are compared. The results of <b>atof("0")</b> and <b>atof("00")</b>
  113. are both 0.0, so those two strings are considered to be equal.</p>
  114. <p>If only one string in a comparison is a pure numeric, then that string
  115. is assumed to be less than the other. Of neither string is a pure numeric,
  116. then <b>strcmp()</b> is used for the comparison.</p>
  117. }
  118. faq {
  119. Why doesn't SQLite allow me to use '0' and '0.0' as the primary
  120. key on two different rows of the same table?
  121. } {
  122. <p>Every row much have a unique primary key.
  123. But SQLite thinks that <b>'0'</b> and <b>'0.0'</b> are the
  124. same value because they compare equal to one another numerically.
  125. (See the previous question.) Hence the values are not unique.</p>
  126. <p>You can work around this issue in two ways:</p>
  127. <ol>
  128. <li><p>Remove the <b>primary key</b> clause from the CREATE TABLE.</p></li>
  129. <li><p>Prepend a space to the beginning of every value you use for
  130. the primary key. The initial
  131. space will mean that the entries are not pure numerics and hence
  132. will be compared as strings using <b>strcmp()</b>.</p></li>
  133. </ol>
  134. }
  135. faq {
  136. My linux box is not able to read an SQLite database that was created
  137. on my SparcStation.
  138. } {
  139. <p>The x86 processor on your linux box is little-endian (meaning that
  140. the least significant byte of integers comes first) but the Sparc is
  141. big-endian (the most significant bytes comes first). SQLite databases
  142. created on a little-endian architecture cannot be used on a big-endian
  143. machine and vice versa.</p>
  144. <p>If you need to move the database from one machine to another, you'll
  145. have to do an ASCII dump of the database on the source machine and then
  146. reconstruct the database at the destination machine. The following is
  147. a typical command for transferring an SQLite databases between two
  148. machines:
  149. <blockquote><pre>
  150. echo .dump | sqlite from.db | ssh sparc sqlite to.db
  151. </pre></blockquote>
  152. The command above assumes the name of the destination machine is
  153. <b>sparc</b> and that you have SSH running on both the source and
  154. destination. An alternative approach is to save the output of the first
  155. <b>sqlite</b> command in a temporary file, move the temporary file
  156. to the destination machine, then run the second <b>sqlite</b> command
  157. while redirecting input from the temporary file.</p>
  158. }
  159. faq {
  160. Can multiple applications or multiple instances of the same
  161. application access a single database file at the same time?
  162. } {
  163. <p>Multiple processes can have the same database open at the same
  164. time. On unix systems, multiple processes can be doing a SELECT
  165. at the same time. But only one process can be making changes to
  166. the database at once. On windows, only a single process can be
  167. reading from the database at one time since Win95/98/ME does not
  168. support reader/writer locks.</p>
  169. <p>The locking mechanism used to control simultaneous access might
  170. not work correctly if the database file is kept on an NFS filesystem.
  171. You should avoid putting SQLite database files on NFS if multiple
  172. processes might try to access the file at the same time.</p>
  173. <p>Locking in SQLite is very course-grained. SQLite locks the
  174. entire database. Big database servers (PostgreSQL, MySQL, Oracle, etc.)
  175. generally have finer grained locking, such as locking on a single
  176. table or a single row within a table. If you have a massively
  177. parallel database application, you should consider using a big database
  178. server instead of SQLite.</p>
  179. <p>When SQLite tries to access a file that is locked by another
  180. process, the default behavior is to return SQLITE_BUSY. You can
  181. adjust this behavior from C code using the <b>sqlite_busy_handler()</b> or
  182. <b>sqlite_busy_timeout()</b> API functions. See the API documentation
  183. for details.</p>
  184. <p>If two or more processes have the same database open and one
  185. process creates a new table or index, the other processes might
  186. not be able to see the new table right away. You might have to
  187. get the other processes to close and reopen their connection to
  188. the database before they will be able to see the new table.</p>
  189. }
  190. faq {
  191. Is SQLite threadsafe?
  192. } {
  193. <p>Yes. Sometimes. In order to be thread-safe, SQLite must be compiled
  194. with the THREADSAFE preprocessor macro set to 1. In the default
  195. distribution, the windows binaries are compiled to be threadsafe but
  196. the linux binaries are not. If you want to change this, you'll have to
  197. recompile.</p>
  198. <p>"Threadsafe" in the previous paragraph means that two or more threads
  199. can run SQLite at the same time on different "<b>sqlite</b>" structures
  200. returned from separate calls to <b>sqlite_open()</b>. It is never safe
  201. to use the same <b>sqlite</b> structure pointer simultaneously in two
  202. or more threads.</p>
  203. <p>Note that if two or more threads have the same database open and one
  204. thread creates a new table or index, the other threads might
  205. not be able to see the new table right away. You might have to
  206. get the other threads to close and reopen their connection to
  207. the database before they will be able to see the new table.</p>
  208. }
  209. faq {
  210. How do I list all tables/indices contained in an SQLite database
  211. } {
  212. <p>If you are running the <b>sqlite</b> command-line access program
  213. you can type "<b>.tables</b>" to get a list of all tables. Or you
  214. can type "<b>.schema</b>" to see the complete database schema including
  215. all tables and indices. Either of these commands can be followed by
  216. a LIKE pattern that will restrict the tables that are displayed.</p>
  217. <p>From within a C/C++ program (or a script using Tcl/Ruby/Perl/Python
  218. bindings) you can get access to table and index names by doing a SELECT
  219. on a special table named "<b>SQLITE_MASTER</b>". Every SQLite database
  220. has an SQLITE_MASTER table that defines the schema for the database.
  221. The SQLITE_MASTER table looks like this:</p>
  222. <blockquote><pre>
  223. CREATE TABLE sqlite_master (
  224. type TEXT,
  225. name TEXT,
  226. tbl_name TEXT,
  227. rootpage INTEGER,
  228. sql TEXT
  229. );
  230. </pre></blockquote>
  231. <p>For tables, the <b>type</b> field will always be <b>'table'</b> and the
  232. <b>name</b> field will be the name of the table. So to get a list of
  233. all tables in the database, use the following SELECT command:</p>
  234. <blockquote><pre>
  235. SELECT name FROM sqlite_master
  236. WHERE type='table'
  237. ORDER BY name;
  238. </pre></blockquote>
  239. <p>For indices, <b>type</b> is equal to <b>'index'</b>, <b>name</b> is the
  240. name of the index and <b>tbl_name</b> is the name of the table to which
  241. the index belongs. For both tables and indices, the <b>sql</b> field is
  242. the text of the original CREATE TABLE or CREATE INDEX statement that
  243. created the table or index. For automatically created indices (used
  244. to implement the PRIMARY KEY or UNIQUE constraints) the <b>sql</b> field
  245. is NULL.</p>
  246. <p>The SQLITE_MASTER table is read-only. You cannot change this table
  247. using UPDATE, INSERT, or DELETE. The table is automatically updated by
  248. CREATE TABLE, CREATE INDEX, DROP TABLE, and DROP INDEX commands.</p>
  249. <p>Temporary tables do not appear in the SQLITE_MASTER table. At this time
  250. there is no way to get a listing of temporary tables and indices.</p>
  251. }
  252. faq {
  253. Are there any known size limits to SQLite databases?
  254. } {
  255. <p>Internally, SQLite can handle databases up to 2^40 bytes (1 terabyte)
  256. in size. But the backend interface to POSIX and Win32 limits files to
  257. 2^31 (2 gigabytes).</p>
  258. <p>SQLite arbitrarily limits the amount of data in one row to 1 megabyte.
  259. There is a single #define in the source code that can be changed to raise
  260. this limit as high as 16 megabytes if desired.</p>
  261. <p>There is a theoretical limit of about 2^32 (4 billion) rows
  262. in a single table, but there
  263. is no way to test this limit without exceeding the maximum file size, so
  264. it is not really an issue. There is also a theoretical limit of about 2^32
  265. tables and indices, but again it is not really possible to reach this
  266. limit due to the file size constraint.</p>
  267. <p>The name and "CREATE TABLE" statement for a table must fit entirely
  268. within a 1-megabyte row of the SQLITE_MASTER table. Other than this,
  269. there are no constraints on the length of the name of a table, or on the
  270. number of columns, etc. Indices are similarly unconstrained.</p>
  271. }
  272. faq {
  273. What is the maximum size of a VARCHAR in SQLite?
  274. } {
  275. <p>Remember, SQLite is typeless. A VARCHAR column can hold as much
  276. data as any other column. The total amount of data in a single row
  277. of the database is limited to 1 megabyte. You can increase this limit
  278. to 16 megabytes, if you need to, by adjusting a single #define in the
  279. source tree and recompiling.</p>
  280. <p>For maximum speed and space efficiency, you should try to keep the
  281. amount of data in a single row below about 230 bytes.</p>
  282. }
  283. faq {
  284. Does SQLite support a BLOB type?
  285. } {
  286. <p>You can declare a table column to be of type "BLOB" but it will still
  287. only store null-terminated strings. This is because the only way to
  288. insert information into an SQLite database is using an INSERT SQL statement,
  289. and you can not include binary data in the middle of the ASCII text string
  290. of an INSERT statement.</p>
  291. <p>SQLite is 8-bit clean with regard to the data is stores as long as
  292. the data does not contain any NUL characters. If you want to store binary
  293. data, consider encoding your data in such a way that it contains no NUL
  294. characters and inserting it that way. You might use URL-style encoding:
  295. encode NUL as "%00" and "%" as "%25". Or you might consider encoding your
  296. binary data using base-64.</p>
  297. }
  298. faq {
  299. How do I add or delete columns from an existing table in SQLite.
  300. } {
  301. <p>SQLite does not support the "ALTER TABLE" SQL command. If you
  302. what to change the structure of a table, you have to recreate the
  303. table. You can save existing data to a temporary table, drop the
  304. old table, create the new table, then copy the data back in from
  305. the temporary table.</p>
  306. <p>For example, suppose you have a table named "t1" with columns
  307. names "a", "b", and "c" and that you want to delete column "c" from
  308. this table. The following steps illustrate how this could be done:
  309. </p>
  310. <blockquote><pre>
  311. BEGIN TRANSACTION;
  312. CREATE TEMPORARY TABLE t1_backup(a,b);
  313. INSERT INTO t1_backup SELECT a,b FROM t1;
  314. DROP TABLE t1;
  315. CREATE TABLE t1(a,b);
  316. INSERT INTO t1 SELECT a,b FROM t1_backup;
  317. DROP TABLE t1_backup;
  318. COMMIT;
  319. </pre></blockquote>
  320. }
  321. # End of questions and answers.
  322. #############
  323. puts {<DL COMPACT>}
  324. for {set i 1} {$i<$cnt} {incr i} {
  325. puts " <DT><A HREF=\"#q$i\">($i)</A></DT>"
  326. puts " <DD>[lindex $faq($i) 0]</DD>"
  327. }
  328. puts {</DL>}
  329. for {set i 1} {$i<$cnt} {incr i} {
  330. puts "<A NAME=\"q$i\"><HR />"
  331. puts "<P><B>($i) [lindex $faq($i) 0]</B></P>\n"
  332. puts "<BLOCKQUOTE>[lindex $faq($i) 1]</BLOCKQUOTE></LI>\n"
  333. }
  334. puts {
  335. </OL>
  336. <p><hr /></p>
  337. <p><a href="index.html"><img src="/goback.jpg" border=0 />
  338. Back to the SQLite Home Page</a>
  339. </p>
  340. </body></html>}