?? vdbe.html
字號:
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd"><html><head><title>The Virtual Database Engine of SQLite</title><style type="text/css">body { margin: auto; font-family: "Verdana" "sans-serif"; padding: 8px 1%;}a { color: #45735f }a:visited { color: #734559 }.logo { position:absolute; margin:3px; }.tagline { float:right; text-align:right; font-style:italic; width:240px; margin:12px; margin-top:58px;}.toolbar { font-variant: small-caps; text-align: center; line-height: 1.6em; margin: 0; padding:1px 8px;}.toolbar a { color: white; text-decoration: none; padding: 6px 12px; }.toolbar a:visited { color: white; }.toolbar a:hover { color: #80a796; background: white; }.content { margin: 5%; }.content dt { font-weight:bold; }.content dd { margin-bottom: 25px; margin-left:20%; }.content ul { padding:0px; padding-left: 15px; margin:0px; }/* rounded corners */.se { background: url(images/se.png) 100% 100% no-repeat #80a796}.sw { background: url(images/sw.png) 0% 100% no-repeat }.ne { background: url(images/ne.png) 100% 0% no-repeat }.nw { background: url(images/nw.png) 0% 0% no-repeat }</style><meta http-equiv="content-type" content="text/html; charset=UTF-8"> </head><body><div><!-- container div to satisfy validator --><a href="index.html"><img class="logo" src="images/SQLite.gif" alt="SQLite Logo" border="0"></a><div><!-- IE hack to prevent disappearing logo--></div><div class="tagline">Small. Fast. Reliable.<br>Choose any three.</div><table width=100% style="clear:both"><tr><td> <div class="se"><div class="sw"><div class="ne"><div class="nw"> <div class="toolbar"> <a href="about.html">About</a> <a href="sitemap.html">Sitemap</a> <a href="docs.html">Documentation</a> <a href="download.html">Download</a> <a href="copyright.html">License</a> <a href="news.html">News</a> <a href="http://www.sqlite.org/cvstrac/index">Developers</a> <a href="support.html">Support</a> </div></div></div></div></div></td></tr></table> <h2>The Virtual Database Engine of SQLite</h2><blockquote><b>This document describes the virtual machine used in SQLite version 2.8.0. The virtual machine in SQLite version 3.0 and 3.1 is very similar inconcept but many of the opcodes have changed and the algorithms aresomewhat different. Use this document as a rough guide to the ideabehind the virtual machine in SQLite version 3, not as a reference onhow the virtual machine works.</b></blockquote><p>If you want to know how the SQLite library works internally,you need to begin with a solid understanding of the Virtual DatabaseEngine or VDBE. The VDBE occurs right in the middle of theprocessing stream (see the <a href="arch.html">architecture diagram</a>)and so it seems to touch most parts of the library. Evenparts of the code that do not directly interact with the VDBEare usually in a supporting role. The VDBE really is the heart ofSQLite.</p><p>This article is a brief introduction to how the VDBEworks and in particular how the various VDBE instructions(documented <a href="opcode.html">here</a>) work togetherto do useful things with the database. The style is tutorial,beginning with simple tasks and working toward solving morecomplex problems. Along the way we will visit mostsubmodules in the SQLite library. After completeing this tutorial,you should have a pretty good understanding of how SQLite worksand will be ready to begin studying the actual source code.</p><h2>Preliminaries</h2><p>The VDBE implements a virtual computer that runs a program inits virtual machine language. The goal of each program is to interrogate or change the database. Toward this end, the machinelanguage that the VDBE implements is specifically designed tosearch, read, and modify databases.</p><p>Each instruction of the VDBE language contains an opcode andthree operands labeled P1, P2, and P3. Operand P1 is an arbitraryinteger. P2 is a non-negative integer. P3 is a pointer to a data structure or null-terminated string, possibly null. Only a few VDBEinstructions use all three operands. Many instructions use onlyone or two operands. A significant number of instructions useno operands at all but instead take their data and store their resultson the execution stack. The details of what each instructiondoes and which operands it uses are described in the separate<a href="opcode.html">opcode description</a> document.</p><p>A VDBE program beginsexecution on instruction 0 and continues with successive instructionsuntil it either (1) encounters a fatal error, (2) executes aHalt instruction, or (3) advances the program counter past thelast instruction of the program. When the VDBE completes execution,all open database cursors are closed, all memory is freed, and everything is popped from the stack.So there are never any worries about memory leaks or undeallocated resources.</p><p>If you have done any assembly language programming or haveworked with any kind of abstract machine before, all of thesedetails should be familiar to you. So let's jump right in andstart looking as some code.</p><a name="insert1"></a><h2>Inserting Records Into The Database</h2><p>We begin with a problem that can be solved using a VDBE programthat is only a few instructions long. Suppose we have an SQLtable that was created like this:</p><blockquote><pre>CREATE TABLE examp(one text, two int);</pre></blockquote><p>In words, we have a database table named "examp" that has twocolumns of data named "one" and "two". Now suppose we want to insert a singlerecord into this table. Like this:</p><blockquote><pre>INSERT INTO examp VALUES('Hello, World!',99);</pre></blockquote><p>We can see the VDBE program that SQLite uses to implement thisINSERT using the <b>sqlite</b> command-line utility. First startup <b>sqlite</b> on a new, empty database, then create the table.Next change the output format of <b>sqlite</b> to a form thatis designed to work with VDBE program dumps by entering the".explain" command.Finally, enter the INSERT statement shown above, but precede theINSERT with the special keyword "EXPLAIN". The EXPLAIN keywordwill cause <b>sqlite</b> to print the VDBE program rather than execute it. We have:</p><blockquote><tt>$ <b>sqlite test_database_1</b><br>sqlite> <b>CREATE TABLE examp(one text, two int);</b><br>sqlite> <b>.explain</b><br>sqlite> <b>EXPLAIN INSERT INTO examp VALUES('Hello, World!',99);</b><br>addr opcode p1 p2 p3 <br>---- ------------ ----- ----- -----------------------------------<br>0 Transaction 0 0 <br>1 VerifyCookie 0 81 <br>2 Transaction 1 0 <br>3 Integer 0 0 <br>4 OpenWrite 0 3 examp <br>5 NewRecno 0 0 <br>6 String 0 0 Hello, World! <br>7 Integer 99 0 99 <br>8 MakeRecord 2 0 <br>9 PutIntKey 0 1 <br>10 Close 0 0 <br>11 Commit 0 0 <br>12 Halt 0 0</tt></blockquote>
?? 快捷鍵說明
復(fù)制代碼
Ctrl + C
搜索代碼
Ctrl + F
全屏模式
F11
切換主題
Ctrl + Shift + D
顯示快捷鍵
?
增大字號
Ctrl + =
減小字號
Ctrl + -