⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 the tcl interface to the sqlite library4.htm

📁 在嵌入式操作系统uclinux下应用的数据库sqlite
💻 HTM
📖 第 1 页 / 共 2 页
字号:
<html><head><title>The Tcl interface to the SQLite library</title></head><body bgcolor="white" link="#50695f" vlink="#508896"><table width="100%" border="0"><tr><td valign="top"><img src="sqlite.gif"></td><td width="100%"></td><td valign="bottom"><ul><li><a href="http://www.sqlite.org/cvstrac/tktnew">bugs</a></li><li><a href="changes.html">changes</a></li><li><a href="contrib">contrib</a></li><li><a href="download.html#cvs">cvs&nbsp;repository</a></li><li><a href="docs.html">documentation</a></li></ul></td><td width="10"></td><td valign="bottom"><ul><li><a href="download.html">download</a></li><li><a href="faq.html">faq</a></li><li><a href="index.html">home</a></li><li><a href="support.html">mailing&nbsp;list</a></li><li><a href="index.html">news</a></li></ul></td><td width="10"></td><td valign="bottom"><ul><li><a href="quickstart.html">quick&nbsp;start</a></li><li><a href="support.html">support</a></li><li><a href="lang.html">syntax</a></li><li><a href="http://www.sqlite.org/cvstrac/timeline">timeline</a></li><li><a href="http://www.sqlite.org/cvstrac/wiki">wiki</a></li></ul></td></tr></table><table width="100%"><tr><td bgcolor="#80a796"></td></tr></table><h2>The Tcl interface to the SQLite library</h2><p>The SQLite library is designed to be very easy to use froma Tcl or Tcl/Tk script.  This document gives an overview of the Tclprogramming interface.</p><h3>The API</h3><p>The interface to the SQLite library consists of singletcl command named <b>sqlite</b> (version 2.8) or <b>sqlite3</b>(version 3.0).  Because there is only thisone command, the interface is not placed in a separatenamespace.</p><p>The <b>sqlite3</b> command is used as follows:</p><blockquote><b>sqlite3</b>&nbsp;&nbsp;<i>dbcmd&nbsp;&nbsp;database-name</i></blockquote><p>The <b>sqlite3</b> command opens the database named in the secondargument.  If the database does not already exist, it isautomatically created.The <b>sqlite3</b> command also creates a new Tclcommand to control the database.  The name of the new Tcl commandis given by the first argument.  This approach is similar to theway widgets are created in Tk.</p><p>The name of the database is just the name of a disk file in whichthe database is stored.</p><p>Once an SQLite database is open, it can be controlled using methods of the <i>dbcmd</i>.  There are currently 18 methodsdefined:</p><p><ul><li><a href="#authorizer">authorizer</a></li><li><a href="#busy">busy</a></li><li><a href="#changes">changes</a></li><li><a href="#close">close</a></li><li><a href="#collate">collate</a></li><li><a href="#collation_needed">collation_needed</a></li><li><a href="#commit_hook">commit_hook</a></li><li><a href="#complete">complete</a></li><li><a href="#copy">copy</a></li><li><a href="#errorcode">errorcode</a></li><li><a href="#eval">eval</a></li><li><a href="#function">function</a></li><li><a href="#last_insert_rowid">last_insert_rowid</a></li><li><a href="#onecolumn">onecolumn</a></li><li><a href="#progress">progress</a></li><li><a href="#timeout">timeout</a></li><li><a href="#total_changes">total_changes</a></li><li><a href="#trace">trace</a></li></ul></p><p>The use of each of these methods will be explained in the sequel, thoughnot in the order shown above.</p><a name="close"><h3>The "close" method</h3><p>As its name suggests, the "close" method to an SQLite database justcloses the database.  This has the side-effect of deleting the<i>dbcmd</i> Tcl command.  Here is an example of opening and thenimmediately closing a database:</p><blockquote><b>sqlite3 db1 ./testdb<br>db1 close</b></blockquote><p>If you delete the <i>dbcmd</i> directly, that has the same effectas invoking the "close" method.  So the following code is equivalentto the previous:</p><blockquote><b>sqlite3 db1 ./testdb<br>rename db1 {}</b></blockquote><a name="eval"><h3>The "eval" method</h3><p>The most useful <i>dbcmd</i> method is "eval".  The eval method is usedto execute SQL on the database.  The syntax of the eval method lookslike this:</p><blockquote><i>dbcmd</i>&nbsp;&nbsp;<b>eval</b>&nbsp;&nbsp;<i>sql</i>&nbsp;&nbsp;&nbsp;&nbsp;?<i>array-name&nbsp;</i>?&nbsp;?<i>script</i>?</blockquote><p>The job of the eval method is to execute the SQL statement or statementsgiven in the second argument.  For example, to create a new table ina database, you can do this:</p><blockquote><b>sqlite3 db1 ./testdb<br>db1 eval {CREATE TABLE t1(a int, b text)}</b></blockquote><p>The above code creates a new table named <b>t1</b> with columns<b>a</b> and <b>b</b>.  What could be simpler?</p><p>Query results are returned as a list of column values.  If aquery requests 2 columns and there are 3 rows matching the query,then the returned list will contain 6 elements.  For example:</p><blockquote><b>db1 eval {INSERT INTO t1 VALUES(1,'hello')}<br>db1 eval {INSERT INTO t1 VALUES(2,'goodbye')}<br>db1 eval {INSERT INTO t1 VALUES(3,'howdy!')}<br>set x [db1 eval {SELECT * FROM t1 ORDER BY a}]</b></blockquote><p>The variable <b>$x</b> is set by the above code to</p><blockquote><b>1 hello 2 goodbye 3 howdy!</b></blockquote><p>You can also process the results of a query one row at a timeby specifying the name of an array variable and a script followingthe SQL code.  For each row of the query result, the values of allcolumns will be inserted into the array variable and the script willbe executed.  For instance:</p><blockquote><b>db1 eval {SELECT * FROM t1 ORDER BY a} values {<br>&nbsp;&nbsp;&nbsp;&nbsp;parray values<br>&nbsp;&nbsp;&nbsp;&nbsp;puts ""<br>}</b></blockquote><p>This last code will give the following output:</p><blockquote><b>values(*) = a b<br>values(a) = 1<br>values(b) = hello<p>values(*) = a b<br>values(a) = 2<br>values(b) = goodbye<p>values(*) = a b<br>values(a) = 3<br>values(b) = howdy!</b></blockquote><p>For each column in a row of the result, the name of that columnis used as an index in to array.  The value of the column is storedin the corresponding array entry.  The special array index * isused to store a list of column names in the order that they appear.</p><p>If the array variable name is omitted or is the empty string, then the value ofeach column is stored in a variable with the same name as the columnitself.  For example:</p><blockquote><b>db1 eval {SELECT * FROM t1 ORDER BY a} {<br>&nbsp;&nbsp;&nbsp;&nbsp;puts "a=$a b=$b"<br>}</b></blockquote><p>From this we get the following output</p><blockquote><b>a=1 b=hello<br>a=2 b=goodbye<br>a=3 b=howdy!</b></blockquote><p>Tcl variable names can appear in the SQL statement of the second argumentin any position where it is legal to put a string or number literal.  Thevalue of the variable is substituted for the variable name.  If thevariable does not exist a NULL values is used.  For example:</p><blockquote><b>db1 eval {INSERT INTO t1 VALUES(5,$bigblob)}</b></blockquote><p>Note that it is not necessary to quote the $bigblob value.  That happensautomatically.  If $bigblob is a large string or binary object, thistechnique is not only easier to write, it is also much more efficientsince it avoids making a copy of the content of $bigblob.</p><a name="complete"><h3>The "complete" method</h3><p>The "complete" method takes a string of supposed SQL as its only argument.It returns TRUE if the string is a complete statement of SQL and FALSE ifthere is more to be entered.</p>

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -