Debian Codegraph @

www/tclsqlite.html

raw
1 <!DOCTYPE html>
2 <html><head>
3 <meta name="viewport" content="width=device-width, initial-scale=1.0">
4 <meta http-equiv="content-type" content="text/html; charset=UTF-8">
5 <link href="sqlite.css" rel="stylesheet">
6 <title>The Tcl interface to the SQLite library</title>
7 <!-- path= -->
8 </head>
9 <body>
10 <div class=nosearch>
11 <a href="index.html">
12 <img class="logo" src="images/sqlite370_banner.gif" alt="SQLite" border="0">
13 </a>
14 <div><!-- IE hack to prevent disappearing logo --></div>
15 <div class="tagline desktoponly">
16 Small. Fast. Reliable.<br>Choose any three.
17 </div>
18 <div class="menu mainmenu">
19 <ul>
20 <li><a href="index.html">Home</a>
21 <li class='mobileonly'><a href="javascript:void(0)" onclick='toggle_div("submenu")'>Menu</a>
22 <li class='wideonly'><a href='about.html'>About</a>
23 <li class='desktoponly'><a href="docs.html">Documentation</a>
24 <li class='desktoponly'><a href="download.html">Download</a>
25 <li class='wideonly'><a href='copyright.html'>License</a>
26 <li class='desktoponly'><a href="support.html">Support</a>
27 <li class='desktoponly'><a href="prosupport.html">Purchase</a>
28 <li class='search' id='search_menubutton'>
29 <a href="javascript:void(0)" onclick='toggle_search()'>Search</a>
30 </ul>
31 </div>
32 <div class="menu submenu" id="submenu">
33 <ul>
34 <li><a href='about.html'>About</a>
35 <li><a href='docs.html'>Documentation</a>
36 <li><a href='download.html'>Download</a>
37 <li><a href='support.html'>Support</a>
38 <li><a href='prosupport.html'>Purchase</a>
39 </ul>
40 </div>
41 <div class="searchmenu" id="searchmenu">
42 <form method="GET" action="search">
43 <select name="s" id="searchtype">
44 <option value="d">Search Documentation</option>
45 <option value="c">Search Changelog</option>
46 </select>
47 <input type="text" name="q" id="searchbox" value="">
48 <input type="submit" value="Go">
49 </form>
50 </div>
51 </div>
52 <script>
53 function toggle_div(nm) {
54 var w = document.getElementById(nm);
55 if( w.style.display=="block" ){
56 w.style.display = "none";
57 }else{
58 w.style.display = "block";
59 }
60 }
61 function toggle_search() {
62 var w = document.getElementById("searchmenu");
63 if( w.style.display=="block" ){
64 w.style.display = "none";
65 } else {
66 w.style.display = "block";
67 setTimeout(function(){
68 document.getElementById("searchbox").focus()
69 }, 30);
70 }
71 }
72 function div_off(nm){document.getElementById(nm).style.display="none";}
73 window.onbeforeunload = function(e){div_off("submenu");}
74 /* Disable the Search feature if we are not operating from CGI, since */
75 /* Search is accomplished using CGI and will not work without it. */
76 if( !location.origin || !location.origin.match || !location.origin.match(/http/) ){
77 document.getElementById("search_menubutton").style.display = "none";
78 }
79 /* Used by the Hide/Show button beside syntax diagrams, to toggle the */
80 function hideorshow(btn,obj){
81 var x = document.getElementById(obj);
82 var b = document.getElementById(btn);
83 if( x.style.display!='none' ){
84 x.style.display = 'none';
85 b.innerHTML='show';
86 }else{
87 x.style.display = '';
88 b.innerHTML='hide';
89 }
90 return false;
91 }
92 var antiRobot = 0;
93 function antiRobotGo(){
94 if( antiRobot!=3 ) return;
95 antiRobot = 7;
96 var j = document.getElementById("mtimelink");
97 if(j && j.hasAttribute("data-href")) j.href=j.getAttribute("data-href");
98 }
99 function antiRobotDefense(){
100 document.body.onmousedown=function(){
101 antiRobot |= 2;
102 antiRobotGo();
103 document.body.onmousedown=null;
104 }
105 document.body.onmousemove=function(){
106 antiRobot |= 2;
107 antiRobotGo();
108 document.body.onmousemove=null;
109 }
110 setTimeout(function(){
111 antiRobot |= 1;
112 antiRobotGo();
113 }, 100)
114 antiRobotGo();
115 }
116 antiRobotDefense();
117 </script>
118
119
120
121 <h1 align=center>The Tcl interface to the SQLite library</h1>
122
123 <p>The SQLite library is designed to be very easy to use from
124 a <a href="http://www.tcl-lang.org">Tcl or Tcl/Tk</a> script. SQLite
125 began as a <a href="http://www.tcl-lang.org/doc/tea/">Tcl extension</a>
126 and the primary <a href="testing.html">test suite</a> for SQLite is written in TCL. SQLite
127 can be used with any programming language, but its connections to
128 TCL run deep.</p>
129
130 <p>This document gives an overview of the Tcl
131 programming interface for SQLite.</p>
132
133 <h3>The API</h3>
134
135 <p>The interface to the SQLite library consists of single
136 tcl command named <b>sqlite3</b>
137 Because there is only this
138 one command, the interface is not placed in a separate
139 namespace.</p>
140
141 <p>The <b>sqlite3</b> command is mostly used as follows
142 to open or create a database:</p>
143
144 <blockquote>
145 <b>sqlite3</b>&nbsp;&nbsp;<i>dbcmd&nbsp;&nbsp;?database-name?&nbsp;&nbsp;?options?</i>
146 </blockquote>
147
148 <p>To get information only, the <b>sqlite3</b> command may be given exactly
149 one argument, either "-version", "-sourceid" or "-has-codec", which will
150 return the specified datum with no other effect.
151 </p>
152
153 <p>With other arguments, the <b>sqlite3</b> command opens the database
154 named in the second non-option argument, or named "" if there is no such.
155 If the open succeeds, a new Tcl command named by the first argument is created
156 and "" is returned.
157 (This approach is similar to the way widgets are created in Tk.)
158 If the open fails, an error is raised without creating a Tcl command
159 and an error message string is returned.
160 </p>
161 <p>If the database does not already exist, the default behavior
162 is for it to be created automatically (though this can be changed by
163 using the "<b>-create <i>false</i></b>" option).
164 </p>
165
166 <p>
167 The name of the database is usually just the name of a disk file in which
168 the database is stored. If the name of the database is
169 the special name "<a href="inmemorydb.html">:memory:</a>", then a new database is created
170 in memory. If the name of the database is an empty string, then
171 the database is created in an empty file that is automatically deleted
172 when the database connection closes. <a href="uri.html">URI filenames</a> can be used if
173 the "<b>-uri yes</b>" option is supplied on the <b>sqlite3</b> command.
174 </p>
175
176 <p>
177 Options understood by the <b>sqlite3</b> command include:
178 </p>
179
180 <blockquote>
181 <dl>
182
183 <dt><b>-create</b> <i>BOOLEAN</i></dt>
184 <dd><p>
185 If true, then a new database is created if one does not already exist.
186 If false, then an attempt to open a database file that does not previously
187 exist raises an error. The default behavior is "true".
188 </p></dd>
189
190 <dt><b>-nomutex</b> <i>BOOLEAN</i></dt>
191 <dd><p>
192 If true, then all mutexes for the database connection are disabled.
193 This provides a small performance boost in single-threaded applications.
194 </p></dd>
195
196 <dt><b>-readonly</b> <i>BOOLEAN</i></dt>
197 <dd><p>
198 If true, then open the database file read-only. If false, then the
199 database is opened for both reading and writing if filesystem permissions
200 allow, or for reading only if filesystem write permission is denied
201 by the operating system. The default setting is "false". Note that
202 if the previous process to have the database did not exit cleanly
203 and left behind a <a href="fileformat2.html#hotjrnl">hot journal</a>, then the write permission is required
204 to recover the database after opening, and the database cannot be
205 opened read-only.
206 </p></dd>
207
208 <dt><b>-uri</b> <i>BOOLEAN</i></dt>
209 <dd><p>
210 If true, then interpret the filename argument as a <a href="uri.html">URI filename</a>. If
211 false, then the argument is a literal filename. The default value is
212 "false".
213 </p></dd>
214
215 <dt><b>-vfs</b> <i>VFSNAME</i></dt>
216 <dd><p>
217 Use an alternative <a href="vfs.html">VFS</a> named by the argument.
218 </p></dd>
219
220 <dt><b>-fullmutex</b> <i>BOOLEAN</i></dt>
221 <dd><p>
222 If true, multiple threads can safely attempt to use the database.
223 If false, such attempts are unsafe. The default value depends upon
224 how the extension is built.
225 </p></dd>
226
227 <dt><b>-nofollow</b> <i>BOOLEAN</i></dt>
228 <dd><p>
229 If true, and the database name refers to a symbolic link,
230 it will not be followed to open the true database file.
231 If false, symbolic links will be followed.
232 The default is "false".
233 </p></dd>
234
235 </dl>
236 </blockquote>
237
238
239
240 <p>
241 Once an SQLite database is open, it can be controlled using
242 methods of the <i>dbcmd</i>.
243 There are currently 40 methods
244 defined.</p>
245
246
247 <table border=0 cellpadding=10 width='100%'>
248 <tr><td valign='top'><ul>
249 <li><a href="#authorizer">authorizer</a></li>
250 <li><a href="#backup">backup</a></li>
251 <li><a href="#bind_fallback">bind_fallback</a></li>
252 <li><a href="#busy">busy</a></li>
253 <li><a href="#cache">cache</a></li>
254 <li><a href="#changes">changes</a></li>
255 <li><a href="#close">close</a></li>
256 <li><a href="#collate">collate</a></li>
257 <li><a href="#collation_needed">collation_needed</a></li>
258 <li><a href="#commit_hook">commit_hook</a></li>
259 <li><a href="#complete">complete</a></li>
260 <li><a href="#config">config</a></li>
261 <li><a href="#copy">copy</a></li>
262 <li><a href="#deserialize">deserialize</a></li>
263 </ul></td>
264 <td valign='top'><ul>
265 <li><a href="#enable_load_extension">enable_load_extension</a></li>
266 <li><a href="#errorcode">errorcode</a></li>
267 <li><a href="#eval">eval</a></li>
268 <li><a href="#exists">exists</a></li>
269 <li><a href="#function">function</a></li>
270 <li><a href="#incrblob">incrblob</a></li>
271 <li><a href="#interrupt">interrupt</a></li>
272 <li><a href="#last_insert_rowid">last_insert_rowid</a></li>
273 <li><a href="#nullvalue">nullvalue</a></li>
274 <li><a href="#onecolumn">onecolumn</a></li>
275 <li><a href="#preupdate">preupdate</a></li>
276 <li><a href="#profile">profile</a></li>
277 <li><a href="#progress">progress</a></li>
278 <li><a href="#restore">restore</a></li>
279 </ul></td>
280 <td valign='top'><ul>
281 <li><a href="#rollback_hook">rollback_hook</a></li>
282 <li><a href="#serialize">serialize</a></li>
283 <li><a href="#status">status</a></li>
284 <li><a href="#timeout">timeout</a></li>
285 <li><a href="#total_changes">total_changes</a></li>
286 <li><a href="#trace">trace</a></li>
287 <li><a href="#trace_v2">trace_v2</a></li>
288 <li><a href="#transaction">transaction</a></li>
289 <li><a href="#unlock_notify">unlock_notify</a></li>
290 <li><a href="#update_hook">update_hook</a></li>
291 <li><a href="#version">version</a></li>
292 <li><a href="#wal_hook">wal_hook</a></li>
293
294 </ul></td></tr>
295 </table>
296
297
298 <p>The use of each of these methods will be explained in the sequel, though
299 not in the order shown above.</p>
300
301 <a name="eval"></a>
302 <h3>The "eval" method</h3>
303
304 <p>
305 The most useful <i>dbcmd</i> method is "eval". The eval method is used
306 to execute SQL on the database. The syntax of the eval method looks
307 like this:</p>
308
309 <blockquote>
310 <i>dbcmd</i>&nbsp;&nbsp;<b>eval</b>&nbsp;&nbsp;?<b>-withoutnulls</b>?&nbsp;&nbsp;<i>sql</i>
311 &nbsp;&nbsp;?<i>array-name</i>?&nbsp;&nbsp;?<i>script</i>?
312 </blockquote>
313
314 <p>
315 The job of the eval method is to execute the SQL statement or statements
316 given in the second argument. For example, to create a new table in
317 a database, you can do this:</p>
318
319 <blockquote>
320 <b>sqlite3 db1 ./testdb<br>
321 db1 eval {CREATE TABLE t1(a int, b text)}</b>
322 </blockquote>
323
324 <p>The above code creates a new table named <b>t1</b> with columns
325 <b>a</b> and <b>b</b>. What could be simpler?</p>
326
327 <p>Query results are returned as a list of column values. If a
328 query requests 2 columns and there are 3 rows matching the query,
329 then the returned list will contain 6 elements. For example:</p>
330
331 <blockquote>
332 <b>db1 eval {INSERT INTO t1 VALUES(1,'hello')}<br>
333 db1 eval {INSERT INTO t1 VALUES(2,'goodbye')}<br>
334 db1 eval {INSERT INTO t1 VALUES(3,'howdy!')}<br>
335 set x &#91;db1 eval {SELECT * FROM t1 ORDER BY a}&#93;</b>
336 </blockquote>
337
338 <p>The variable <b>$x</b> is set by the above code to</p>
339
340 <blockquote>
341 <b>1 hello 2 goodbye 3 howdy!</b>
342 </blockquote>
343
344 <p>You can also process the results of a query one row at a time
345 by specifying the name of an array variable and a script following
346 the SQL code. For each row of the query result, the values of all
347 columns will be inserted into the array variable and the script will
348 be executed. For instance:</p>
349
350 <blockquote>
351 <b>db1 eval {SELECT * FROM t1 ORDER BY a} values {<br>
352 &nbsp;&nbsp;&nbsp;&nbsp;parray values<br>
353 &nbsp;&nbsp;&nbsp;&nbsp;puts ""<br>
354 }</b>
355 </blockquote>
356
357 <p>This last code will give the following output:</p>
358
359 <blockquote><b>
360 values(*) = a b<br>
361 values(a) = 1<br>
362 values(b) = hello<p>
363
364 values(*) = a b<br>
365 values(a) = 2<br>
366 values(b) = goodbye<p>
367
368 values(*) = a b<br>
369 values(a) = 3<br>
370 values(b) = howdy!</b>
371 </blockquote>
372
373 <p>
374 For each column in a row of the result, the name of that column
375 is used as an index in to array and the value of the column is stored
376 in the corresponding array entry. (Caution: If two or more columns
377 in the result set of a query have the same name, then the last column
378 with that name will overwrite prior values and earlier columns with the
379 same name will be inaccessible.) The special array index * is
380 used to store a list of column names in the order that they appear.
381 </p>
382
383 <p>
384 Normally, NULL SQL results are stored in the array using the
385 <a href="#nullvalue">nullvalue</a> setting. However, if
386 the <b>-withoutnulls</b> option is used, then NULL SQL values
387 cause the corresponding array element to be unset instead.
388 </p>
389
390 <p>
391 If the array variable name is omitted or is the empty string, then the value of
392 each column is stored in a variable with the same name as the column
393 itself. For example:
394 </p>
395
396 <blockquote>
397 <b>db1 eval {SELECT * FROM t1 ORDER BY a} {<br>
398 &nbsp;&nbsp;&nbsp;&nbsp;puts "a=$a b=$b"<br>
399 }</b>
400 </blockquote>
401
402 <p>
403 From this we get the following output
404 </p>
405
406 <blockquote><b>
407 a=1 b=hello<br>
408 a=2 b=goodbye<br>
409 a=3 b=howdy!</b>
410 </blockquote>
411
412 <a name="varsubst"></a>
413
414 <p>
415 Tcl variable names can appear in the SQL statement of the second argument
416 in any position where it is legal to put a string or number literal. The
417 value of the variable is substituted for the variable name. If the
418 variable does not exist a NULL values is used. For example:
419 </p>
420
421 <blockquote><b>
422 db1 eval {INSERT INTO t1 VALUES(5,$bigstring)}
423 </b></blockquote>
424
425 <p>
426 Note that it is not necessary to quote the $bigstring value. That happens
427 automatically. If $bigstring is a large string or binary object, this
428 technique is not only easier to write, it is also much more efficient
429 since it avoids making a copy of the content of $bigstring.
430 </p>
431
432 <p>
433 If the $bigstring variable has both a string and a "bytearray" representation,
434 then TCL inserts the value as a string. If it has only a "bytearray"
435 representation, then the value is inserted as a BLOB. To force a
436 value to be inserted as a BLOB even if it also has a text representation,
437 use a "@" character to in place of the "$". Like this:
438 </p>
439
440 <blockquote><b>
441 db1 eval {INSERT INTO t1 VALUES(5,@bigstring)}
442 </b></blockquote>
443
444 <p>
445 If the variable does not have a bytearray representation, then "@" works
446 just like "$". Note that ":" works like "$" in all cases so the following
447 is another way to express the same statement:
448 </p>
449
450 <blockquote><b>
451 db1 eval {INSERT INTO t1 VALUES(5,:bigstring)}
452 </b></blockquote>
453
454 <p>The use of ":" instead of "$" before the name of a variable can
455 sometimes be useful if the SQL text is enclosed in double-quotes "..."
456 instead of curly-braces {...}.
457 When the SQL is contained within double-quotes "..." then TCL will do
458 the substitution of $-variables, which can lead to SQL injection if
459 extreme care is not used. But TCL will never substitute a :-variable
460 regardless of whether double-quotes "..." or curly-braces {...} are
461 used to enclose the SQL, so the use of :-variables adds an extra
462 measure of defense against SQL
463 injection.
464 </p>
465
466 <a name="close"></a>
467 <h3>The "close" method</h3>
468
469
470 <p>
471 As its name suggests, the "close" method to an SQLite database just
472 closes the database. This has the side-effect of deleting the
473 <i>dbcmd</i> Tcl command. Here is an example of opening and then
474 immediately closing a database:
475 </p>
476
477 <blockquote>
478 <b>sqlite3 db1 ./testdb<br>
479 db1 close</b>
480 </blockquote>
481
482 <p>
483 If you delete the <i>dbcmd</i> directly, that has the same effect
484 as invoking the "close" method. So the following code is equivalent
485 to the previous:</p>
486
487 <blockquote>
488 <b>sqlite3 db1 ./testdb<br>
489 rename db1 {}</b>
490 </blockquote>
491 <a name="transaction"></a>
492 <h3>The "transaction" method</h3>
493
494
495 <p>
496 The "transaction" method is used to execute a TCL script inside an SQLite
497 database transaction. The transaction is committed when the script completes,
498 or it rolls back if the script fails. If the transaction occurs within
499 another transaction (even one that is started manually using BEGIN) it
500 is a no-op.
501 </p>
502
503 <p>
504 The transaction command can be used to group together several SQLite
505 commands in a safe way. You can always start transactions manually using
506 BEGIN, of
507 course. But if an error occurs so that the COMMIT or ROLLBACK are never
508 run, then the database will remain locked indefinitely. Also, BEGIN
509 does not nest, so you have to make sure no other transactions are active
510 before starting a new one. The "transaction" method takes care of
511 all of these details automatically.
512 </p>
513
514 <p>
515 The syntax looks like this:
516 </p>
517
518 <blockquote>
519 <i>dbcmd</i>&nbsp;&nbsp;<b>transaction</b>&nbsp;&nbsp;<i>?transaction-type?</i>
520 &nbsp;&nbsp;<i>script</i>
521 </blockquote>
522
523
524 <p>
525 The <i>transaction-type</i> can be one of <b>deferred</b>,
526 <b>exclusive</b> or <b>immediate</b>. The default is deferred.
527 </p>
528 <a name="cache"></a>
529 <h3>The "cache" method</h3>
530
531
532 <p>
533 The "eval" method described <a href="#eval">above</a> keeps a cache of
534 <a href="c3ref/prepare.html">prepared statements</a>
535 for recently evaluated SQL commands.
536 The "cache" method is used to control this cache.
537 The first form of this command is:</p>
538
539 <blockquote>
540 <i>dbcmd</i>&nbsp;&nbsp;<b>cache size</b>&nbsp;&nbsp;<i>N</i>
541 </blockquote>
542
543 <p>This sets the maximum number of statements that can be cached.
544 The upper limit is 100. The default is 10. If you set the cache size
545 to 0, no caching is done.</p>
546
547 <p>The second form of the command is this:</p>
548
549
550 <blockquote>
551 <i>dbcmd</i>&nbsp;&nbsp;<b>cache flush</b>
552 </blockquote>
553
554 <p>The cache-flush method
555 <a href="c3ref/finalize.html">finalizes</a>
556 all prepared statements currently
557 in the cache.</p>
558
559 <a name="complete"></a>
560 <h3>The "complete" method</h3>
561
562
563 <p>
564 The "complete" method takes a string of supposed SQL as its only argument.
565 It returns TRUE if the string is a complete statement of SQL and FALSE if
566 there is more to be entered.</p>
567
568 <p>The "complete" method is useful when building interactive applications
569 in order to know when the user has finished entering a line of SQL code.
570 This is really just an interface to the
571 <a href="c3ref/complete.html"><b>sqlite3_complete()</b></a> C
572 function.
573 <a name="config"></a>
574 <h3>The "config" method</h3>
575
576
577 <p>
578 The "config" method queries or changes certain configuration settings for
579 the database connection using the <a href="c3ref/db_config.html">sqlite3_db_config()</a> interface.
580 Run this method with no arguments to get a TCL list of available
581 configuration settings and their current values:
582
583 <blockquote>
584 <i>dbcmd</i>&nbsp;&nbsp;<b>config</b>
585 </blockquote>
586
587 <p>The above will return something like this:
588
589 <blockquote>
590 defensive 0 dqs_ddl 1 dqs_dml 1 enable_fkey 0 enable_qpsg 0 enable_trigger 1 enable_view 1 fts3_tokenizer 1 legacy_alter_table 0 legacy_file_format 0 load_extension 0 no_ckpt_on_close 0 reset_database 0 trigger_eqp 0 trusted_schema 1 writable_schema 0
591 </blockquote>
592
593 <p>Add the name of an individual configuration setting to query the current
594 value of that setting. Optionally add a boolean value to change a setting.
595
596 <p>
597 The following four configuration changes are recommended for maximum
598 application security. Turning off the trust_schema setting prevents
599 virtual tables and dodgy SQL functions from being used inside of triggers,
600 views, CHECK constraints, generated columns, and expression indexes.
601 Turning off the dqs_dml and dqs_ddl settings prevents the use of
602 double-quoted strings. Turning on defensive prevents direct writes
603 to shadow tables.
604
605 <blockquote><pre>
606 db config trusted_schema 0
607 db config defensive 1
608 db config dqs_dml 0
609 db config dqs_ddl 0
610 </pre></blockquote>
611 <a name="copy"></a>
612 <h3>The "copy" method</h3>
613
614
615 <p>
616 The "copy" method copies data from a file into a table.
617 It returns the number of rows processed successfully from the file.
618 The syntax of the copy method looks like this:</p>
619
620 <blockquote>
621 <i>dbcmd</i>&nbsp;&nbsp;<b>copy</b>&nbsp;&nbsp;<i>conflict-algorithm</i>
622 &nbsp;&nbsp;<i>table-name&nbsp;</i>&nbsp;&nbsp;<i>file-name&nbsp;</i>
623 &nbsp;&nbsp;&nbsp;&nbsp;?<i>column-separator</i>?
624 &nbsp;&nbsp;?<i>null-indicator</i>?
625 </blockquote>
626
627 <p>Conflict-algorithm must be one of the SQLite conflict algorithms for
628 the INSERT statement: <i>rollback</i>, <i>abort</i>,
629 <i>fail</i>,<i>ignore</i>, or <i>replace</i>. See the SQLite Language
630 section for <a href="lang_conflict.html">ON CONFLICT</a> for
631 more information. The conflict-algorithm must be specified in lower case.
632 </p>
633
634 <p>Table-name must already exists as a table. File-name must exist, and
635 each row must contain the same number of columns as defined in the table.
636 If a line in the file contains more or less than the number of columns defined,
637 the copy method rollbacks any inserts, and returns an error.</p>
638
639 <p>Column-separator is an optional column separator string. The default is
640 the ASCII tab character \t. </p>
641
642 <p>Null-indicator is an optional string that indicates a column value is null.
643 The default is an empty string. Note that column-separator and
644 null-indicator are optional positional arguments; if null-indicator
645 is specified, a column-separator argument must be specified and
646 precede the null-indicator argument.</p>
647
648 <p>The copy method implements similar functionality to the <b>.import</b>
649 SQLite shell command.
650 <a name="timeout"></a>
651 <h3>The "timeout" method</h3>
652
653
654 <p>The "timeout" method is used to control how long the SQLite library
655 will wait for locks to clear before giving up on a database transaction.
656 The default timeout is 0 millisecond. (In other words, the default behavior
657 is not to wait at all.)</p>
658
659 <p>The SQLite database allows multiple simultaneous
660 readers or a single writer but not both. If any process is writing to
661 the database no other process is allows to read or write. If any process
662 is reading the database other processes are allowed to read but not write.
663 The entire database shared a single lock.</p>
664
665 <p>When SQLite tries to open a database and finds that it is locked, it
666 can optionally delay for a short while and try to open the file again.
667 This process repeats until the query times out and SQLite returns a
668 failure. The timeout is adjustable. It is set to 0 by default so that
669 if the database is locked, the SQL statement fails immediately. But you
670 can use the "timeout" method to change the timeout value to a positive
671 number. For example:</p>
672
673 <blockquote><b>db1 timeout 2000</b></blockquote>
674
675 <p>The argument to the timeout method is the maximum number of milliseconds
676 to wait for the lock to clear. So in the example above, the maximum delay
677 would be 2 seconds.</p>
678 <a name="busy"></a>
679 <h3>The "busy" method</h3>
680
681
682 <p>The "busy" method, like "timeout", only comes into play when the
683 database is locked. But the "busy" method gives the programmer much more
684 control over what action to take. The "busy" method specifies a callback
685 Tcl procedure that is invoked whenever SQLite tries to open a locked
686 database. A single integer argument is appended to the callback before
687 it is invoke. The argument is the number of prior calls to the busy
688 callback for the current locking event. It is intended that
689 the callback will do some other useful work for a short while
690 (such as service GUI events) then return
691 so that the lock can be tried again. The callback procedure should
692 return "0" if it wants SQLite to try again to open the database and
693 should return "1" if it wants SQLite to abandon the current operation.
694 <p>
695 If the busy method is invoked without an argument, the name of the callback
696 procedure last set by the busy method is returned. If no callback procedure
697 has been set, an empty string is returned.
698 <a name="enable_load_extension"></a>
699 <h3>The "enable_load_extension" method</h3>
700
701
702 <p>The extension loading mechanism of SQLite (accessed using the
703 <a href="lang_corefunc.html#load_extension">load_extension()</a> SQL function) is turned off by default. This is
704 a security precaution. If an application wants to make use of the
705 <a href="lang_corefunc.html#load_extension">load_extension()</a> function it must first turn the capability on using
706 this method.</p>
707
708 <p>This method takes a single boolean argument which will turn the
709 extension loading functionality on or off.</p>
710
711 <p>For best security, do not use this method unless truly needed,
712 and run <a href="pragma.html#pragma_trusted_schema">PRAGMA trusted_schema=OFF</a> or the
713 "<a href="#config">db config trusted_schema 0</a>" method <u>before</u>
714 invoking this method.
715
716 <p>This method maps to the <a href="c3ref/enable_load_extension.html">sqlite3_enable_load_extension()</a> C/C++
717 interface.</p>
718 <a name="exists"></a>
719 <h3>The "exists" method</h3>
720
721
722 <p>The "exists" method is similar to "onecolumn" and "eval" in that
723 it executes SQL statements. The difference is that the "exists" method
724 always returns a boolean value which is TRUE if a query in the SQL
725 statement it executes returns one or more rows and FALSE if the SQL
726 returns an empty set.</p>
727
728 <p>The "exists" method is often used to test for the existence of
729 rows in a table. For example:</p>
730
731 <blockquote><b>
732 if {&#91;db exists {SELECT 1 FROM table1 WHERE user=$user}&#93;} {<br>
733 &nbsp;&nbsp;&nbsp;# Processing if $user exists<br>
734 } else {<br>
735 &nbsp;&nbsp;&nbsp;# Processing if $user does not exist<br>
736 }
737 </b></blockquote>
738 <a name="last_insert_rowid"></a>
739 <h3>The "last_insert_rowid" method</h3>
740
741
742 <p>The "last_insert_rowid" method returns an integer which is the ROWID
743 of the most recently inserted database row.</p>
744 <a name="function"></a>
745 <h3>The "function" method</h3>
746
747
748 <p>The "function" method registers new SQL functions with the SQLite engine.
749 The arguments are the name of the new SQL function and a TCL command that
750 implements that function. Arguments to the function are appended to the
751 TCL command before it is invoked.</p>
752
753 <p>
754 For security reasons, it is recommended that applications first set
755 <a href="pragma.html#pragma_trusted_schema">PRAGMA trusted_schema=OFF</a> or run the
756 "<a href="#config">db config trusted_schema 0</a>"
757 method before using this method.</p>
758
759 <p>
760 The syntax looks like this:
761 </p>
762
763 <blockquote>
764 <i>dbcmd</i>&nbsp;&nbsp;<b>function</b>
765 &nbsp;&nbsp;<i>sql-name</i>
766 &nbsp;&nbsp;<i>?options?</i>
767 &nbsp;&nbsp;<i>script</i>
768 </blockquote>
769
770 <p>
771 The following example creates a new SQL function named "hex" that converts
772 its numeric argument in to a hexadecimal encoded string:
773 </p>
774
775 <blockquote><b>
776 db function hex {format 0x%X}
777 </b></blockquote>
778
779 <p>The "function" method accepts the following options:
780 <blockquote>
781 <dl>
782
783 <dt><b>-argcount</b> <i>INTEGER</i></dt>
784 <dd><p>
785 Specify the number of arguments that the SQL function accepts. The default
786 value of -1 means any number of arguments.
787
788 <dt><b>-deterministic</b>
789 <dd><p>
790 This option indicates that the function will always return the same
791 answer given the same argument values. The SQLite query optimizer
792 uses this information to cache answers from function calls with
793 constant inputs and reuse the result rather than invoke the function
794 repeatedly.
795 </dd>
796
797 <dt><b>-directonly</b>
798 <dd><p>
799 This option restricts the function to only be usable by direct
800 top-level SQL statement. The function will not be accessible to
801 triggers, views, CHECK constraints, generated columns, or index
802 expressions. This option is recommended for all application-defined
803 SQL functions, and is <u>highly recommended</u> for any SQL
804 function that has side effects or that reveals internal state of
805 the application.
806
807 <p style="background-color:#ffff90;"><b>Security Warning:</b>
808 Without this switch, an attacker might be able to change the
809 schema of a database file to include the new function inside a trigger
810 or view or CHECK constraint and thereby trick the application into
811 running the function with parameters of the attacker's choosing.
812 Hence, if the new function has side effects or reveals internal
813 state about the application and the -directonly option is not
814 used, that is a potential security vulnerability.</p>
815 </dd>
816
817 <dt><b>-innocuous</b>
818 <dd><p>
819 This option indicates that the function has no side effects and
820 does not leak any information that cannot be computed directly
821 from its input parameters. When this option is specified, the
822 function may be used in triggers, views, CHECK constraints,
823 generated columns, and/or index expressions even if
824 <a href="pragma.html#pragma_trusted_schema">PRAGMA trusted_schema=OFF</a>. The use of this option is discouraged
825 unless it is truly needed.
826 </dd>
827
828 <dt><b>-returntype integer|real|text|blob|any</b>
829 <dd><p>
830 This option is used to configure the type of the result returned by the
831 function. If this option is set to "any" (the default), SQLite attempts to
832 determine the type of each value returned by the function implementation based
833 on the Tcl value's internal type. Or, if it is set to "text" or "blob", the
834 returned value is always a text or blob value, respectively. If this option is
835 set to "integer", SQLite attempts to coerce the value returned by the function
836 to an integer. If this is not possible without data loss, it attempts to coerce
837 it to a real value, and finally falls back to text. If this option is set to
838 "real", an attempt is made to return a real value, falling back to text if this
839 is not possible.
840 </dd>
841 </dl>
842
843
844
845 </blockquote>
846 <a name="nullvalue"></a>
847 <h3>The "nullvalue" method</h3>
848
849
850 <p>
851 The "nullvalue" method changes the representation for NULL returned
852 as result of the "eval" method.</p>
853
854 <blockquote><b>
855 db1 nullvalue NULL
856 </b></blockquote>
857
858 <p>The "nullvalue" method is useful to differ between NULL and empty
859 column values as Tcl lacks a NULL representation. The default
860 representation for NULL values is an empty string.</p>
861 <a name="onecolumn"></a>
862 <h3>The "onecolumn" method</h3>
863
864
865 <p>The "onecolumn" method works like
866 "<a href="#eval">eval</a>" in that it evaluates the
867 SQL query statement given as its argument. The difference is that
868 "onecolumn" returns a single element which is the first column of the
869 first row of the query result.</p>
870
871 <p>This is a convenience method. It saves the user from having to
872 do a "<tt>&#91;lindex&nbsp;...&nbsp;0&#93;</tt>" on the results of an "eval"
873 in order to extract a single column result.</p>
874 <a name="changes"></a>
875 <h3>The "changes" method</h3>
876
877
878 <p>The "changes" method returns an integer which is the number of rows
879 in the database that were inserted, deleted, and/or modified by the most
880 recent "eval" method.</p>
881 <a name="total_changes"></a>
882 <h3>The "total_changes" method</h3>
883
884
885 <p>The "total_changes" method returns an integer which is the number of rows
886 in the database that were inserted, deleted, and/or modified since the
887 current database connection was first opened.</p>
888 <a name="authorizer"></a>
889 <h3>The "authorizer" method</h3>
890
891
892 <p>The "authorizer" method provides access to the
893 <a href="c3ref/set_authorizer.html">sqlite3_set_authorizer</a>
894 C/C++ interface. The argument to authorizer is the name of a procedure that
895 is called when SQL statements are being compiled in order to authorize
896 certain operations. The callback procedure takes 5 arguments which describe
897 the operation being coded. If the callback returns the text string
898 "SQLITE_OK", then the operation is allowed. If it returns "SQLITE_IGNORE",
899 then the operation is silently disabled. If the return is "SQLITE_DENY"
900 then the compilation fails with an error.
901 </p>
902
903 <p>If the argument is an empty string then the authorizer is disabled.
904 If the argument is omitted, then the current authorizer is returned.</p>
905 <a name="bind_fallback"></a>
906 <h3>The "bind_fallback" method</h3>
907
908
909 <p>The "bind_fallback" method gives the application control over how to
910 handle parameter binding when no TCL variable matches the parameter name.
911
912 <p>When the <a href="#eval">eval method</a> sees a named SQL
913 parameter such as "$abc" or ":def" or "@ghi" in an SQL statement, it tries
914 to look up a TCL variable with the same name, and it binds the value
915 of that TCL variable to the SQL parameter. If no such TCL variable exists,
916 the default behavior is to bind an SQL NULL value to the parameter. However,
917 if a bind_fallback proc is specified, then that proc is invoked with the
918 name of the SQL parameter and the return value from the proc is bound to
919 the SQL parameter. Or if the proc returns an error, then the SQL statement
920 aborts with that error. If the proc returns with some code other than
921 TCL_OK or TCL_ERROR, then the SQL parameter is bound to NULL, as it would
922 be by default.
923
924 <p>The "bind_fallback" method has a single optional argument. If the argument
925 is an empty string, then the bind_fallback is cancelled and the default behavior
926 is restored. If the argument is a non-empty string, then the argument is a
927 TCL command (usually the name of a proc) to invoke whenever an SQL parameter
928 is seen that does not match any TCL variable. If the "bind_fallback" method
929 is given no arguments, then the current bind_fallback command is returned.
930
931 <p>As an example, the following setup causes TCL to throw an error if
932 an SQL statement contains an parameter that does not match any global
933 TCL variable:
934
935 <blockquote><pre><b>
936 proc bind_error {nm} {
937 error "no such variable: $nm"
938 }
939 db bind_fallback bind_error
940 </b></pre></blockquote>
941 <a name="progress"></a>
942 <h3>The "progress" method</h3>
943
944
945 <p>This method registers a callback that is invoked periodically during
946 query processing. There are two arguments: the number of SQLite virtual
947 machine opcodes between invocations, and the TCL command to invoke.
948 Setting the progress callback to an empty string disables it.</p>
949
950 <p>The progress callback can be used to display the status of a lengthy
951 query or to process GUI events during a lengthy query.</p>
952 <a name="collate"></a>
953 <h3>The "collate" method</h3>
954
955
956 <p>This method registers new text collating sequences. There are
957 two arguments: the name of the collating sequence and the name of a
958 TCL procedure that implements a comparison function for the collating
959 sequence.
960 </p>
961
962 <p>For example, the following code implements a collating sequence called
963 "NOCASE" that sorts in text order without regard to case:
964 </p>
965
966 <blockquote><pre><b>
967 proc nocase_compare {a b} {
968 return &#91;string compare &#91;string tolower $a] &#91;string tolower $b]]
969 }
970 db collate NOCASE nocase_compare
971 </b></pre></blockquote>
972 <a name="collation_needed"></a>
973 <h3>The "collation_needed" method</h3>
974
975
976 <p>This method registers a callback routine that is invoked when the SQLite
977 engine needs a particular collating sequence but does not have that
978 collating sequence registered. The callback can register the collating
979 sequence. The callback is invoked with a single parameter which is the
980 name of the needed collating sequence.</p>
981 <a name="commit_hook"></a>
982 <h3>The "commit_hook" method</h3>
983
984
985 <p>This method registers a callback routine that is invoked just before
986 SQLite tries to commit changes to a database. If the callback throws
987 an exception or returns a non-zero result, then the transaction rolls back
988 rather than commit.</p>
989 <a name="rollback_hook"></a>
990 <h3>The "rollback_hook" method</h3>
991
992
993 <p>This method registers a callback routine that is invoked just before
994 SQLite tries to do a rollback. The script argument is run without change.</p>
995 <a name="status"></a>
996 <h3>The "status" method</h3>
997
998 <p>This method returns status information from the most recently evaluated
999 SQL statement. The status method takes a single argument which should be
1000 either "steps" or "sorts". If the argument is "steps", then the method
1001 returns the number of full table scan steps that the previous SQL statement
1002 evaluated. If the argument is "sorts", the method returns the number of
1003 sort operations. This information can be used to detect queries that are
1004 not using indices to speed search or sorting.</p>
1005
1006 <p>The status method is basically a wrapper on the
1007 <a href="c3ref/stmt_status.html">sqlite3_stmt_status()</a> C-language interface.</p>
1008 <a name="update_hook"></a>
1009 <h3>The "update_hook" method</h3>
1010
1011
1012 <p>This method registers a callback routine that is invoked just after
1013 each row is modified by an UPDATE, INSERT, or DELETE statement. Four
1014 arguments are appended to the callback before it is invoked:</p>
1015
1016 <ul>
1017 <li>The keyword "INSERT", "UPDATE", or "DELETE", as appropriate</li>
1018 <li>The name of the database which is being changed</li>
1019 <li>The table that is being changed</li>
1020 <li>The rowid of the row in the table being changed</li>
1021 </ul>
1022
1023 <p>The callback must not do anything that will modify the database connection
1024 that invoked the update hook such as running queries.</p>
1025 <a name="preupdate"></a>
1026 <h3>The "preupdate" method</h3>
1027
1028
1029 <p>This method either registers a callback routine that is invoked just
1030 before each row is modified by an UPDATE, INSERT, or DELETE statement,
1031 or may perform certain operations related to the impending update.</p>
1032
1033 <p>To register or remove a preupdate callback, use this syntax:
1034 <blockquote>
1035 <i>dbcmd</i>&nbsp;&nbsp;<b>preupdate&nbsp;&nbsp;hook</b>&nbsp;&nbsp;<i>?SCRIPT?</i>
1036 </blockquote>
1037 When a preupdate callback is registered, then prior to each row modification,
1038 the callback is run with these arguments:
1039 <ul>
1040 <li>The keyword "INSERT", "UPDATE", or "DELETE", as appropriate</li>
1041 <li>The name of the database which is being changed</li>
1042 <li>The table that is being changed</li>
1043 <li>The original rowid of the row in the table being changed</li>
1044 <li>The new rowid (if any) of the row in the table being changed</li>
1045 </ul>
1046 The callback must not do anything that will modify the database connection
1047 that invoked the preupdate hook such as running queries.</p>
1048
1049 <p>When the callback is executing, and only then, these preupdate operations
1050 may be performed by use of the indicated syntax:
1051 <blockquote>
1052 <i>dbcmd</i>&nbsp;&nbsp;<b>preupdate&nbsp;&nbsp;count</b><br>
1053 <i>dbcmd</i>&nbsp;&nbsp;<b>preupdate&nbsp;&nbsp;depth</b><br>
1054 <i>dbcmd</i>&nbsp;&nbsp;<b>preupdate&nbsp;&nbsp;new</b>&nbsp;&nbsp;<i>INDEX</i><br>
1055 <i>dbcmd</i>&nbsp;&nbsp;<b>preupdate&nbsp;&nbsp;old</b>&nbsp;&nbsp;<i>INDEX</i><br>
1056 </blockquote>
1057 </p>
1058
1059 <p>The <b>count</b> submethod returns the number of columns in the row that is
1060 being inserted, updated, or deleted.</p>
1061 <p>The <b>depth</b> submethod returns the update nesting depth. This will be
1062 0 for a direct insert, update, or delete operation;
1063 1 for inserts, updates, or deletes invoked by top-level triggers;
1064 or higher values for changes resulting from trigger-invoked triggers.</p>
1065
1066 <p>The <b>old</b> and <b>new</b> submethods return the selected original
1067 or changed column value respectively of the row being updated.</p>
1068
1069 <p>Note that the Tcl interface (and underlying SQLite library) must have
1070 been built with the preprocessor symbol SQLITE_ENABLE_PREUPDATE_HOOK
1071 defined for the <b>preupdate</b> method to be available.</p>
1072 <a name="wal_hook"></a>
1073 <h3>The "wal_hook" method</h3>
1074
1075
1076 <p>This method registers a callback routine that is invoked after transaction
1077 commit when the database is in <a href="wal.html">WAL mode</a>. Two arguments are appended to the
1078 callback command before it is invoked:</p>
1079
1080 <ul>
1081 <li>The name of the database on which the transaction was committed
1082 <li>The number of entries in the write-ahead log (WAL) file for that database
1083 </ul>
1084
1085 <p>This method might decide to run a <a href="wal.html#ckpt">checkpoint</a> either itself or as a
1086 subsequent idle callback. Note that SQLite only allows a single WAL hook.
1087 By default this single WAL hook is used for the auto-checkpointing. If you
1088 set up an explicit WAL hook, then that one WAL hook must ensure that checkpoints
1089 are occurring since the auto-checkpointing mechanism will be disabled.</p>
1090
1091 <p>This method should return an integer value that is equivalent to an
1092 SQLite error code (usually 0 for SQLITE_OK in the case of success or 1 for
1093 SQLITE_ERROR if some error occurs). As in <a href="c3ref/wal_hook.html">sqlite3_wal_hook()</a>, the results of
1094 returning an integer that does not correspond to an SQLite error code are
1095 undefined. If the value returned by the script cannot be interpreted as an
1096 integer value, or if the script throws a Tcl exception, no error is returned to
1097 SQLite but a Tcl background-error is raised.
1098 <a name="incrblob"></a>
1099 <h3>The "incrblob" method</h3>
1100
1101
1102 <p>This method opens a TCL channel that can be used to read or write
1103 into a preexisting BLOB in the database. The syntax is like this:</p>
1104
1105 <blockquote>
1106 <i>dbcmd</i>&nbsp;&nbsp;<b>incrblob</b>&nbsp;&nbsp;<b>?-readonly?</b>
1107 &nbsp;&nbsp;<i>?DB?&nbsp;&nbsp;TABLE&nbsp;&nbsp;COLUMN&nbsp;&nbsp;ROWID</i>
1108 </blockquote>
1109
1110 <p>
1111 The command returns a new TCL channel for reading or writing to the BLOB.
1112 The channel is opened using the underlying
1113 <a href="c3ref/blob_open.html">sqlite3_blob_open()</a> C-language
1114 interface. Close the channel using the <b>close</b> command of TCL.
1115 </p>
1116 <a name="errorcode"></a>
1117 <h3>The "errorcode" method</h3>
1118
1119
1120 <p>This method returns the numeric error code that resulted from the most
1121 recent SQLite operation.</p>
1122 <a name="trace"></a>
1123 <h3>The "trace" method</h3>
1124
1125
1126 <p>The "trace" method registers a callback that is invoked as each SQL
1127 statement is compiled. The text of the SQL is appended as a single string
1128 to the command before it is invoked. This can be used (for example) to
1129 keep a log of all SQL operations that an application performs.
1130 </p>
1131 <a name="trace_v2"></a>
1132 <h3>The "trace_v2" method</h3>
1133
1134
1135 <p>The "trace_v2" method registers a callback that is invoked as each SQL
1136 statement is compiled. The syntax is as follows:
1137
1138
1139 <blockquote>
1140 <i>dbcmd</i>&nbsp;&nbsp;<b>trace_v2</b>&nbsp;&nbsp;?<i>callback</i>?&nbsp;&nbsp;?<i>mask</i>?
1141 </blockquote>
1142
1143 <p>This command causes the "callback" script to be invoked whenever
1144 certain conditions occurs. The conditions are determined by the <i>mask</i>
1145 argument, which should be a TCL-list of zero or more of the following
1146 keywords:
1147
1148 <ul>
1149 <li> <b>statement</b>
1150 <li> <b>profile</b>
1151 <li> <b>row</b>
1152 <li> <b>close</b>
1153 </ul>
1154
1155 <p>Traces for <b>statement</b> invoke the callback with two arguments
1156 whenever a new SQL statement is run.
1157 The first argument is an integer which is the value of the pointer
1158 to the underlying <a href="c3ref/stmt.html">sqlite3_stmt</a> object. This integer can be used
1159 to correlate SQL statement text with the result of a <b>profile</b>
1160 or <b>row</b> callback. The second argument is the
1161 unexpanded text of the SQL statement being run. By "unexpanded", we
1162 mean that variable substitutions in the text are not expanded into the
1163 variable values. This is different from the behavior of the "trace"
1164 method which does expand variable substitutions.
1165
1166 <p>Traces for <b>profile</b> invoke the callback with two arguments
1167 as each SQL statement finishes. The first argument is an integer which
1168 is the value of the underlying <a href="c3ref/stmt.html">sqlite3_stmt</a> object. The second
1169 argument is the approximate run-time for the statement in nanoseconds.
1170 The run-time is the best estimate available depending on the capabilities
1171 of the platform on which the application is running.
1172
1173 <p>Traces for <b>row</b> invoke the callback with a single argument
1174 whenever a new result row is available from an SQL statement.
1175 The argument is an integer which is the value of the underlying
1176 <a href="c3ref/stmt.html">sqlite3_stmt</a> object pointer.
1177
1178 <p>Traces for <b>close</b> invoke the callback with a single argument
1179 as the database connection is closing. The argument is an integer which
1180 is the value of a pointer to the underlying <a href="c3ref/sqlite3.html">sqlite3</a> object that is
1181 closing.
1182
1183 <p>There can only be a single trace callback registered on a database
1184 connection. Each use of "trace" or "trace_v2" cancels all prior
1185 trace callback.
1186 <a name="backup"></a>
1187 <h3>The "backup" method</h3>
1188
1189
1190 <p>The "backup" method makes a backup copy of a live database. The
1191 command syntax is like this:</p>
1192
1193 <blockquote>
1194 <i>dbcmd</i>&nbsp;&nbsp;<b>backup</b>&nbsp;&nbsp;?<i>source-database</i>?&nbsp;&nbsp;<i>backup-filename</i>
1195 </blockquote>
1196
1197 <p>The optional <i>source-database</i> argument tells which database in
1198 the current connection should be backed up. The default value is <b>main</b>
1199 (or, in other words, the primary database file). To back up TEMP tables
1200 use <b>temp</b>. To backup an auxiliary database added to the connection
1201 using the <a href="lang_attach.html">ATTACH</a> command, use the name of that database as it was assigned
1202 in the <a href="lang_attach.html">ATTACH</a> command.</p>
1203
1204 <p>The <i>backup-filename</i> is the name of a file into which the backup is
1205 written. <i>Backup-filename</i> does not have to exist ahead of time, but if
1206 it does, it must be a well-formed SQLite database.</p>
1207 <a name="restore"></a>
1208 <h3>The "restore" method</h3>
1209
1210
1211 <p>The "restore" method copies the content from a separate database file
1212 into the current database connection, overwriting any preexisting content.
1213 The command syntax is like this:</p>
1214
1215 <blockquote>
1216 <i>dbcmd</i>&nbsp;&nbsp;<b>restore</b>&nbsp;&nbsp;?<i>target-database</i>?&nbsp;&nbsp;<i>source-filename</i>
1217 </blockquote>
1218
1219 <p>The optional <i>target-database</i> argument tells which database in
1220 the current connection should be overwritten with new content.
1221 The default value is <b>main</b>
1222 (or, in other words, the primary database file). To repopulate the TEMP tables
1223 use <b>temp</b>. To overwrite an auxiliary database added to the connection
1224 using the <a href="lang_attach.html">ATTACH</a> command, use the name of that database as it was assigned
1225 in the <a href="lang_attach.html">ATTACH</a> command.</p>
1226
1227 <p>The <i>source-filename</i> is the name of an existing well-formed SQLite
1228 database file from which the content is extracted.</p>
1229 <a name="serialize"></a>
1230 <h3>The "serialize" method</h3>
1231
1232
1233 <p>The "serialize" method creates a BLOB which is a complete copy of an
1234 underlying database. The syntax is like this:
1235
1236 <blockquote>
1237 <i>dbcmd</i>&nbsp;&nbsp;<b>serialize</b>&nbsp;&nbsp;?<i>database</i>?
1238 </blockquote>
1239
1240 <p>The optional argument is the name of the schema or database to be serialized.
1241 The default value is "main".
1242
1243 <p>This routine returns a TCL byte-array that is the complete content of
1244 the identified database. This byte-array can be written into a file and
1245 then used as an ordinary SQLite database, or it can be sent over a TCP/IP
1246 connection to some other application, or passed to the "deserialize" method
1247 of another database connection.
1248
1249 <p>This method only functions if SQLite is compiled with -DSQLITE_ENABLE_DESERIALIZE
1250 <a name="deserialize"></a>
1251 <h3>The "deserialize" method</h3>
1252
1253 <p>The "deserialize" method takes a TCL byte-array that contains an SQLite
1254 database file and adds it to the database connection. The syntax is:
1255
1256 <blockquote>
1257 <i>dbcmd</i>&nbsp;&nbsp;<b>deserialize</b>&nbsp;&nbsp;?<i>database</i>?&nbsp;&nbsp;<i>value</i>
1258 </blockquote>
1259
1260 <p>The option <i>database</i> argument identifies which attached database
1261 should receive the deserialization. The default is "main".
1262
1263 <p>This command causes SQLite to disconnect from the previous database and
1264 reattach to an in-memory database with the content in <i>value</i>. If <i>value</i>
1265 is not a byte-array containing a well-defined SQLite database, then subsequent
1266 commands will likely return <a href="rescode.html#corrupt">SQLITE_CORRUPT</a> errors.
1267
1268 <p>This method only functions if SQLite is compiled with -DSQLITE_ENABLE_DESERIALIZE
1269 <a name="interrupt"></a>
1270 <h3>The "interrupt" method</h3>
1271
1272 <p>The "interrupt" method invokes the <a href="c3ref/interrupt.html">sqlite3_interrupt()</a> interface, causing
1273 any pending queries to halt.
1274 <a name="version"></a>
1275 <h3>The "version" method</h3>
1276
1277 Return the current library version. For example, "3.23.0".
1278 <a name="profile"></a>
1279 <h3>The "profile" method</h3>
1280
1281 <p>This method is used to profile the execution of SQL statements run by
1282 the application. The syntax is as follows:
1283 </p>
1284
1285 <blockquote>
1286 <i>dbcmd</i>&nbsp;&nbsp;<b>profile</b>&nbsp;&nbsp;?<i>script</i>?
1287 </blockquote>
1288
1289 <p>Unless <i>script</i> is an empty string, this method arranges for the
1290 <i>script</i> to be evaluated after the execution of each SQL statement.
1291 Two arguments are appended to <i>script</i> before it is invoked: the
1292 text of the SQL statement executed and the time elapsed while executing
1293 the statement, in nanoseconds.
1294 </p>
1295 <p>A database handle may only have a single profile script registered at
1296 any time. If there is already a script registered when the profile method
1297 is invoked, the previous profile script is replaced by the new one. If the
1298 <i>script</i> argument is an empty string, any previously registered
1299 profile callback is canceled but no new profile script is registered.
1300 </p>
1301 <a name="unlock_notify"></a>
1302 <h3>The "unlock_notify" method</h3>
1303
1304 <p>The unlock_notify method is used access the <a href="c3ref/unlock_notify.html">sqlite3_unlock_notify()</a>
1305 interface to the SQLite core library for testing purposes. The use of
1306 this method by applications is discouraged.
1307 </p>
1308
1309
1310