📄 tclsqlite.html
字号:
<p>This sets the maximum number of statements that can be cached.The upper limit is 100. The default is 10. If you set the cache sizeto 0, no caching is done.</p><p>The second form of the command is this:</p><blockquote><i>dbcmd</i> <b>cache flush</b></blockquote><p>The cache-flush method <a href="c3ref/finalize.html">finalizes</a>all prepared statements currentlyin the cache.</p><a name="complete"></a><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><p>The "complete" method is useful when building interactive applicationsin order to know when the user has finished entering a line of SQL code.This is really just an interface to the <a href="c3ref/complete.html"><b>sqlite3_complete()</b></a> Cfunction.<a name="copy"></a><h3>The "copy" method</h3><p>The "copy" method copies data from a file into a table.It returns the number of rows processed successfully from the file.The syntax of the copy method looks like this:</p><blockquote><i>dbcmd</i> <b>copy</b> <i>conflict-algorithm</i> <i>table-name </i> <i>file-name </i> ?<i>column-separator </i>? ?<i>null-indicator</i>?</blockquote><p>Conflict-alogrithm must be one of the SQLite conflict algorithms forthe INSERT statement: <i>rollback</i>, <i>abort</i>,<i>fail</i>,<i>ignore</i>, or <i>replace</i>. See the SQLite Languagesection for <a href="lang.html#conflict">ON CONFLICT</a> for more information.The conflict-algorithm must be specified in lower case.</p><p>Table-name must already exists as a table. File-name must exist, andeach row must contain the same number of columns as defined in the table.If a line in the file contains more or less than the number of columns defined,the copy method rollbacks any inserts, and returns an error.</p><p>Column-separator is an optional column separator string. The default isthe ASCII tab character \t. </p><p>Null-indicator is an optional string that indicates a column value is null.The default is an empty string. Note that column-separator andnull-indicator are optional positional arguments; if null-indicatoris specified, a column-separator argument must be specifed andprecede the null-indicator argument.</p><p>The copy method implements similar functionality to the <b>.import</b>SQLite shell command. The SQLite 2.x <a href="lang.html#copy"><b>COPY</b></a> statement (using the PostgreSQL COPY file format)can be implemented with this method as:</p><blockquote>dbcmd copy $conflictalgo $tablename $filename \t \\N</blockquote><a name="timeout"></a><h3>The "timeout" method</h3><p>The "timeout" method is used to control how long the SQLite librarywill wait for locks to clear before giving up on a database transaction.The default timeout is 0 millisecond. (In other words, the default behavioris not to wait at all.)</p><p>The SQLite database allows multiple simultaneousreaders or a single writer but not both. If any process is writing tothe database no other process is allows to read or write. If any processis reading the database other processes are allowed to read but not write.The entire database shared a single lock.</p><p>When SQLite tries to open a database and finds that it is locked, itcan optionally delay for a short while and try to open the file again.This process repeats until the query times out and SQLite returns afailure. The timeout is adjustable. It is set to 0 by default so thatif the database is locked, the SQL statement fails immediately. But youcan use the "timeout" method to change the timeout value to a positivenumber. For example:</p><blockquote><b>db1 timeout 2000</b></blockquote><p>The argument to the timeout method is the maximum number of millisecondsto wait for the lock to clear. So in the example above, the maximum delaywould be 2 seconds.</p><a name="busy"></a><h3>The "busy" method</h3><p>The "busy" method, like "timeout", only comes into play when thedatabase is locked. But the "busy" method gives the programmer much morecontrol over what action to take. The "busy" method specifies a callbackTcl procedure that is invoked whenever SQLite tries to open a lockeddatabase. This callback can do whatever is desired. Presumably, thecallback will do some other useful work for a short while (such as serviceGUI events) then returnso that the lock can be tried again. The callback procedure shouldreturn "0" if it wants SQLite to try again to open the database andshould return "1" if it wants SQLite to abandon the current operation.<a name="exists"></a><h3>The "exists" method</h3><p>The "exists" method is similar to "onecolumn" and "eval" in thatit executes SQL statements. The difference is that the "exists" methodalways returns a boolean value which is TRUE if a query in the SQLstatement it executes returns one or more rows and FALSE if the SQLreturns an empty set.</p><p>The "exists" method is often used to test for the existance ofrows in a table. For example:</p><blockquote><b>if {[db exists {SELECT 1 FROM table1 WHERE user=$user}]} {<br> # Processing if $user exists<br>} else {<br> # Processing if $user does not exist<br>}</b></blockquote><a name="last_insert_rowid"></a><h3>The "last_insert_rowid" method</h3><p>The "last_insert_rowid" method returns an integer which is the ROWIDof the most recently inserted database row.</p><a name="function"></a><h3>The "function" method</h3><p>The "function" method registers new SQL functions with the SQLite engine.The arguments are the name of the new SQL function and a TCL command thatimplements that function. Arguments to the function are appended to theTCL command before it is invoked.</p><p>The following example creates a new SQL function named "hex" that convertsits numeric argument in to a hexadecimal encoded string:</p><blockquote><b>db function hex {format 0x%X}</b></blockquote><a name="nullvalue"></a><h3>The "nullvalue" method</h3><p>The "nullvalue" method changes the representation for NULL returnedas result of the "eval" method.</p><blockquote><b>db1 nullvalue NULL</b></blockquote><p>The "nullvalue" method is useful to differ between NULL and emptycolumn values as Tcl lacks a NULL representation. The defaultrepresentation for NULL values is an empty string.</p><a name="onecolumn"></a><h3>The "onecolumn" method</h3><p>The "onecolumn" method works like "<a href="#eval">eval</a>" in that it evaluates theSQL query statement given as its argument. The difference is that"onecolumn" returns a single element which is the first column of thefirst row of the query result.</p><p>This is a convenience method. It saves the user from having todo a "<tt>[lindex ... 0]</tt>" on the results of an "eval"in order to extract a single column result.</p><a name="changes"></a><h3>The "changes" method</h3><p>The "changes" method returns an integer which is the number of rowsin the database that were inserted, deleted, and/or modified by the mostrecent "eval" method.</p><a name="total_changes"></a><h3>The "total_changes" method</h3><p>The "total_changes" method returns an integer which is the number of rowsin the database that were inserted, deleted, and/or modified since thecurrent database connection was first opened.</p><a name="authorizer"></a><h3>The "authorizer" method</h3><p>The "authorizer" method provides access to the <a href="c3ref/set_authorizer.html">sqlite3_set_authorizer</a>C/C++ interface. The argument to authorizer is the name of a procedure thatis called when SQL statements are being compiled in order to authorizecertain operations. The callback procedure takes 5 arguments which describethe operation being coded. If the callback returns the text string"SQLITE_OK", then the operation is allowed. If it returns "SQLITE_IGNORE",then the operation is silently disabled. If the return is "SQLITE_DENY"then the compilation fails with an error.</p><p>If the argument is an empty string then the authorizer is disabled.If the argument is omitted, then the current authorizer is returned.</p><a name="progress"></a><h3>The "progress" method</h3><p>This method registers a callback that is invoked periodically duringquery processing. There are two arguments: the number of SQLite virtualmachine opcodes between invocations, and the TCL command to invoke.Setting the progress callback to an empty string disables it.</p><p>The progress callback can be used to display the status of a lengthyquery or to process GUI events during a lengthy query.</p><a name="collate"></a><h3>The "collate" method</h3><p>This method registers new text collating sequences. There aretwo arguments: the name of the collating sequence and the name of aTCL procedure that implements a comparison function for the collatingsequence.</p><p>For example, the following code implements a collating sequence called"NOCASE" that sorts in text order without regard to case:</p><blockquote><b>proc nocase_compare {a b} {<br> return [string compare [string tolower $a] [string tolower $b]]<br>}<br>db collate NOCASE nocase_compare<br></b></blockquote><a name="collation_needed"></a><h3>The "collation_needed" method</h3><p>This method registers a callback routine that is invoked when the SQLiteengine needs a particular collating sequence but does not have thatcollating sequence registered. The callback can register the collatingsequence. The callback is invoked with a single parameter which is thename of the needed collating sequence.</p><a name="commit_hook"></a><h3>The "commit_hook" method</h3><p>This method registers a callback routine that is invoked just beforeSQLite tries to commit changes to a database. If the callback throwsan exception or returns a non-zero result, then the transaction rolls backrather than commit.</p><a name="rollback_hook"></a><h3>The "rollback_hook" method</h3><p>This method registers a callback routine that is invoked just beforeSQLite tries to do a rollback. The script argument is run without change.</p><a name="update_hook"></a><h3>The "update_hook" method</h3><p>This method registers a callback routine that is invoked just beforeeach row is modified by an UPDATE, INSERT, or DELETE statement. Fourarguments are appended to the callback before it is invoked:</p><ul><li>The keyword "INSERT", "UPDATE", or "DELETE", as appropriate</li><li>The name of the database which is being changed</li><li>The table that is being changed</li><li>The rowid of the row in the table being changed</li></ul><a name="incrblob"></a><h3>The "incrblob" method</h3><p>This method opens a TCL channel that can be used to read or writeinto a preexisting BLOB in the database. The syntax is like this:</p><blockquote><i>dbcmd</i> <b>incrblob</b> <b>?-readonly??</b> <i>?DB? TABLE COLUMN ROWID</i></blockquote><p>The command returns a new TCL channel for reading or writing to the BLOB.The channel is opened using the underlying <a href="c3ref/blob_open.html">sqlite3_blob_open()</a> C-langaugeinterface. Close the channel using the <b>close</b> command of TCL.</p><a name="errorcode"></a><h3>The "errorcode" method</h3><p>This method returns the numeric error code that resulted from the mostrecent SQLite operation.</p><a name="trace"></a><h3>The "trace" method</h3><p>The "trace" method registers a callback that is invoked as each SQLstatement is compiled. The text of the SQL is appended as a single stringto the command before it is invoked. This can be used (for example) tokeep a log of all SQL operations that an application performs.</p><hr><small<i>This page last modified 2007/12/20 02:19:54 UTC</i></small></div></body></html>
⌨️ 快捷键说明
复制代码
Ctrl + C
搜索代码
Ctrl + F
全屏模式
F11
切换主题
Ctrl + Shift + D
显示快捷键
?
增大字号
Ctrl + =
减小字号
Ctrl + -