?? sqlite.tcl
字號:
puts {<p>The last output mode is "html". In this mode, sqlite writesthe results of the query as an XHTML table. The beginning<TABLE> and the ending </TABLE> are not written, butall of the intervening <TR>s, <TH>s, and <TD>sare. The html output mode is envisioned as being useful forCGI.</p>}puts {<h2>Writing results to a file</h2><p>By default, sqlite sends query results to standard output. Youcan change this using the ".output" command. Just put the name ofan output file as an argument to the .output command and all subsequentquery results will be written to that file. Use ".output stdout" tobegin writing to standard output again. For example:</p>}Code {sqlite> (((.mode list)))sqlite> (((.separator |)))sqlite> (((.output test_file_1.txt)))sqlite> (((select * from tbl1;)))sqlite> (((.exit)))$ (((cat test_file_1.txt)))hello|10goodbye|20$}puts {<h2>Querying the database schema</h2><p>The sqlite program provides several convenience commands thatare useful for looking at the schema of the database. There isnothing that these commands do that cannot be done by some othermeans. These commands are provided purely as a shortcut.</p><p>For example, to see a list of the tables in the database, youcan enter ".tables".</p>}Code {sqlite> (((.tables)))tbl1tbl2sqlite>}puts {<p>The ".tables" command is the same as setting list mode thenexecuting the following query:</p><blockquote><pre>SELECT name FROM sqlite_master WHERE type='table' UNION ALL SELECT name FROM sqlite_temp_master WHERE type='table'ORDER BY name;</pre></blockquote><p>In fact, if you look at the source code to the sqlite program(found in the source tree in the file src/shell.c) you'll findexactly the above query.</p><p>The ".indices" command works in a similar way to list all ofthe indices for a particular table. The ".indices" command takesa single argument which is the name of the table for which theindices are desired. Last, but not least, is the ".schema" command.With no arguments, the ".schema" command shows the original CREATE TABLEand CREATE INDEX statements that were used to build the current database.If you give the name of a table to ".schema", it shows the originalCREATE statement used to make that table and all if its indices.We have:</p>}Code {sqlite> (((.schema)))create table tbl1(one varchar(10), two smallint)CREATE TABLE tbl2 ( f1 varchar(30) primary key, f2 text, f3 real)sqlite> (((.schema tbl2)))CREATE TABLE tbl2 ( f1 varchar(30) primary key, f2 text, f3 real)sqlite>}puts {<p>The ".schema" command accomplishes the same thing as settinglist mode, then entering the following query:</p><blockquote><pre>SELECT sql FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master)WHERE type!='meta'ORDER BY tbl_name, type DESC, name</pre></blockquote><p>Or, if you give an argument to ".schema" because you onlywant the schema for a single table, the query looks like this:</p><blockquote><pre>SELECT sql FROM (SELECT * FROM sqlite_master UNION ALL SELECT * FROM sqlite_temp_master)WHERE tbl_name LIKE '%s' AND type!='meta'ORDER BY type DESC, name</pre></blockquote><p>The <b>%s</b> in the query above is replaced by the argumentto ".schema", of course. Notice that the argument to the ".schema"command appears to the right of an SQL LIKE operator. So you canuse wildcards in the name of the table. For example, to get theschema for all tables whose names contain the character string"abc" you could enter:</p>}Code {sqlite> (((.schema %abc%)))}puts {<p>Along these same lines,the ".table" command also accepts a pattern as its first argument.If you give an argument to the .table command, a "%" is bothappended and prepended and a LIKE clause is added to the query.This allows you to list only those tables that match a particularpattern.</p><p>The ".databases" command shows a list of all databases open inthe current connection. There will always be at least 2. The firstone is "main", the original database opened. The second is "temp",the database used for temporary tables. There may be additional databases listed for databases attached using the ATTACH statement.The first output column is the name the database is attached with, and the second column is the filename of the external file.</p>}Code {sqlite> (((.databases)))}puts {<h2>Converting An Entire Database To An ASCII Text File</h2><p>Use the ".dump" command to convert the entire contents of adatabase into a single ASCII text file. This file can be convertedback into a database by piping it back into <b>sqlite</b>.</p><p>A good way to make an archival copy of a database is this:</p>}Code {$ (((echo '.dump' | sqlite ex1 | gzip -c >ex1.dump.gz)))}puts {<p>This generates a file named <b>ex1.dump.gz</b> that contains everythingyou need to reconstruct the database at a later time, or on anothermachine. To reconstruct the database, just type:</p>}Code {$ (((zcat ex1.dump.gz | sqlite ex2)))}puts {<p>The text format used is the same as used by<a href="http://www.postgresql.org/">PostgreSQL</a>, so youcan also use the .dump command to export an SQLite databaseinto a PostgreSQL database. Like this:</p>}Code {$ (((createdb ex2)))$ (((echo '.dump' | sqlite ex1 | psql ex2)))}puts {<p>You can almost (but not quite) go the other way and exporta PostgreSQL database into SQLite using the <b>pg_dump</b> utility.Unfortunately, when <b>pg_dump</b> writes the database schema information,it uses some SQL syntax that SQLite does not understand.So you cannot pipe the output of <b>pg_dump</b> directly into <b>sqlite</b>.But if you can recreate theschema separately, you can use <b>pg_dump</b> with the <b>-a</b>option to list just the dataof a PostgreSQL database and import that directly into SQLite.</p>}Code {$ (((sqlite ex3 <schema.sql)))$ (((pg_dump -a ex2 | sqlite ex3)))}puts {<h2>Other Dot Commands</h2><p>The ".explain" dot command can be used to set the output modeto "column" and to set the column widths to values that are reasonablefor looking at the output of an EXPLAIN command. The EXPLAIN commandis an SQLite-specific SQL extension that is useful for debugging. If anyregular SQL is prefaced by EXPLAIN, then the SQL command is parsed andanalyzed but is not executed. Instead, the sequence of virtual machineinstructions that would have been used to execute the SQL command arereturned like a query result. For example:</p>}Code {sqlite> (((.explain)))sqlite> (((explain delete from tbl1 where two<20;)))addr opcode p1 p2 p3 ---- ------------ ----- ----- ------------------------------------- 0 ListOpen 0 0 1 Open 0 1 tbl1 2 Next 0 9 3 Field 0 1 4 Integer 20 0 5 Ge 0 2 6 Key 0 0 7 ListWrite 0 0 8 Goto 0 2 9 Noop 0 0 10 ListRewind 0 0 11 ListRead 0 14 12 Delete 0 0 13 Goto 0 11 14 ListClose 0 0 }puts {<p>The ".timeout" command sets the amount of time that the <b>sqlite</b>program will wait for locks to clear on files it is trying to accessbefore returning an error. The default value of the timeout is zero sothat an error is returned immediately if any needed database table orindex is locked.</p><p>And finally, we mention the ".exit" command which causes thesqlite program to exit.</p><h2>Using sqlite in a shell script</h2><p>One way to use sqlite in a shell script is to use "echo" or"cat" to generate a sequence of commands in a file, then invoke sqlite while redirecting input from the generated command file. Thisworks fine and is appropriate in many circumstances. But asan added convenience, sqlite allows a single SQL command to beentered on the command line as a second argument after thedatabase name. When the sqlite program is launched with twoarguments, the second argument is passed to the SQLite libraryfor processing, the query results are printed on standard outputin list mode, and the program exits. This mechanism is designedto make sqlite easy to use in conjunction with programs like"awk". For example:</p>}Code {$ (((sqlite ex1 'select * from tbl1' |)))> ((( awk '{printf "<tr><td>%s<td>%s\n",$1,$2 }')))<tr><td>hello<td>10<tr><td>goodbye<td>20$}puts {<h2>Ending shell commands</h2><p>SQLite commands are normally terminated by a semicolon. In the shell you can also use the word "GO" (case-insensitive) or a backslash character "\" on a line by itself to end a command. These are used by SQL Server and Oracle, respectively. These won't work in <b>sqlite_exec()</b>, because the shell translates these into a semicolon before passing them to that function.</p>}puts {<h2>Compiling the sqlite program from sources</h2><p>The sqlite program is built automatically when you compile thesqlite library. Just get a copy of the source tree, run"configure" and then "make".</p>}puts {<p><hr /></p><p><a href="index.html"><img src="/goback.jpg" border=0 />Back to the SQLite Home Page</a></p></body></html>}
?? 快捷鍵說明
復制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -