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

📄 vdbe.html

📁 这是sqlite3.56的文档。拿来给大家阅读使用
💻 HTML
📖 第 1 页 / 共 5 页
字号:
<p>As you can see above, our simple insert statement isimplemented in 12 instructions.  The first 3 and last 2 instructions are a standard prologue and epilogue, so the real work is done in the middle 7 instructions.  There are no jumps, so the program executes once through from top to bottom.  Let's now look at each instruction in detail.<p><blockquote><tt>0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Transaction&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;VerifyCookie&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;81&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Transaction&nbsp;&nbsp;&nbsp;1&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0</tt></blockquote><p>The instruction <a href="opcode.html#Transaction">Transaction</a> begins a transaction.  The transaction ends when a Commit or Rollback opcode is encountered.  P1 is the index of the database file on which the transaction is started.  Index 0 is the main database file.  A write lock is obtained on the database file when a transaction is started.  No other process can read or write the file while the transaction is underway.  Starting a transaction also creates a rollback journal.  A transaction must be started before any changes can be made to the database.</p><p>The instruction <a href="opcode.html#VerifyCookie">VerifyCookie</a>checks cookie 0 (the database schema version) to make sure it is equal to P2 (the value obtained when the database schema was last read).  P1 is the database number (0 for the main database).  This is done to make sure the database schema hasn't been changed by another thread, in which case it has to be reread.</p><p> The second <a href="opcode.html#Transaction">Transaction</a> instruction begins a transaction and starts a rollback journal for database 1, the database used for temporary tables.</p><blockquote><tt>3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Integer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;<br>4&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;OpenWrite&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;3&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;examp</tt></blockquote><p> The instruction <a href="opcode.html#Integer">Integer</a> pushes the integer value P1 (0) onto the stack.  Here 0 is the number of the database to use in the following OpenWrite instruction.  If P3 is not NULL then it is a string representation of the same integer.  Afterwards the stack looks like this:</p><blockquote><table border=2><tr><td align=left>(integer) 0</td></tr></table></blockquote><p> The instruction <a href="opcode.html#OpenWrite">OpenWrite</a> opens a new read/write cursor with handle P1 (0 in this case) on table "examp", whose root page is P2 (3, in this database file).  Cursor handles can be any non-negative integer.  But the VDBE allocates cursors in an array with the size of the array being one more than the largest cursor.  So to conserve memory, it is best to use handles beginning with zero and working upward consecutively.  Here P3 ("examp") is the name of the table being opened, but this is unused, and only generated to make the code easier to read.  This instruction pops the database number to use (0, the main database) from the top of the stack, so afterwards the stack is empty again.</p><blockquote><tt>5&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;NewRecno&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0</tt></blockquote><p> The instruction <a href="opcode.html#NewRecno">NewRecno</a> creates a new integer record number for the table pointed to by cursor P1.  The record number is one not currently used as a key in the table.  The new record number is pushed onto the stack.  Afterwards the stack looks like this:</p><blockquote><table border=2><tr><td align=left>(integer) new record key</td></tr></table></blockquote><blockquote><tt>6&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;String&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Hello,&nbsp;World!</tt></blockquote><p> The instruction <a href="opcode.html#String">String</a> pushes its P3 operand onto the stack.  Afterwards the stack looks like this:</p><blockquote><table border=2><tr><td align=left>(string) "Hello, World!"</td></tr><tr><td align=left>(integer) new record key</td></tr></table></blockquote><blockquote><tt>7&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;Integer&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;99&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;99</tt></blockquote><p> The instruction <a href="opcode.html#Integer">Integer</a> pushes its P1 operand (99) onto the stack.  Afterwards the stack looks like this:</p><blockquote><table border=2><tr><td align=left>(integer) 99</td></tr><tr><td align=left>(string) "Hello, World!"</td></tr><tr><td align=left>(integer) new record key</td></tr></table></blockquote><blockquote><tt>8&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;MakeRecord&nbsp;&nbsp;&nbsp;&nbsp;2&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0</tt></blockquote><p> The instruction <a href="opcode.html#MakeRecord">MakeRecord</a> pops the top P1 elements off the stack (2 in this case) and converts them into the binary format used for storing records in a database file.  (See the <a href="fileformat.html">file format</a> description for details.)  The new record generated by the MakeRecord instruction is pushed back onto the stack.  Afterwards the stack looks like this:</p></ul><blockquote><table border=2><tr><td align=left>(record) "Hello, World!", 99</td></tr><tr><td align=left>(integer) new record key</td></tr></table></blockquote><blockquote><tt>9&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;PutIntKey&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;1</tt></blockquote><p> The instruction <a href="opcode.html#PutIntKey">PutIntKey</a> uses the top 2 stack entries to write an entry into the table pointed to by cursor P1.  A new entry is created if it doesn't already exist or the data for an existing entry is overwritten.  The record data is the top stack entry, and the key is the next entry down.  The stack is popped twice by this instruction.  Because operand P2 is 1 the row change count is incremented and the rowid is stored for subsequent return by the sqlite_last_insert_rowid() function.  If P2 is 0 the row change count is unmodified.  This instruction is where the insert actually occurs.</p><blockquote><tt>10&nbsp;&nbsp;&nbsp;&nbsp;Close&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0</tt></blockquote><p> The instruction <a href="opcode.html#Close">Close</a> closes a cursor previously opened as P1 (0, the only open cursor). If P1 is not currently open, this instruction is a no-op.</p><blockquote><tt>11&nbsp;&nbsp;&nbsp;&nbsp;Commit&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0</tt></blockquote><p> The instruction <a href="opcode.html#Commit">Commit</a> causes all modifications to the database that have been made since the last Transaction to actually take effect.  No additional modifications are allowed until another transaction is started.  The Commit instruction deletes the journal file and releases the write lock on the database.  A read lock continues to be held if there are still cursors open.</p><blockquote><tt>12&nbsp;&nbsp;&nbsp;&nbsp;Halt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0</tt></blockquote><p> The instruction <a href="opcode.html#Halt">Halt</a> causes the VDBE engine to exit immediately.  All open cursors, Lists, Sorts, etc are closed automatically.  P1 is the result code returned by sqlite_exec().  For a normal halt, this should be SQLITE_OK (0).  For errors, it can be some other value.  The operand P2 is only used when there is an error.  There is an implied "Halt 0 0 0" instruction at the end of every program, which the VDBE appends when it prepares a program to run.</p><a name="trace"></a><h2>Tracing VDBE Program Execution</h2><p>If the SQLite library is compiled without the NDEBUG preprocessor macro, then the PRAGMA <a href="pragma.html#pragma_vdbe_trace">vdbe_trace</a> causes the VDBE to trace the execution of programs.  Though this feature was originally intended for testing and debugging, it can also be useful in learning about how the VDBE operates.  Use "<tt>PRAGMA&nbsp;vdbe_trace=ON;</tt>" to turn tracing on and "<tt>PRAGMA&nbsp;vdbe_trace=OFF</tt>" to turn tracing back off.  Like this:</p><blockquote><tt>sqlite&gt;&nbsp;<b>PRAGMA&nbsp;vdbe_trace=ON;</b><br>&nbsp;&nbsp;&nbsp;0&nbsp;Halt&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;0<br>sqlite&gt;&nbsp;<b>INSERT&nbsp;INTO&nbsp;examp&nbsp;VALUES('Hello,&nbsp;World!',99);</b><br>&nbsp;&nbsp;&nbsp;0&nbsp;Transaction&n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sp;i:2<br>&nbsp;&nbsp;&nbsp;9&nbsp;PutIntKey&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;1<br>&nbsp;&nbsp;10&nbsp;Close&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;0&nbsp;&nbsp;&nbsp;&nbsp;0<br>

⌨️ 快捷键说明

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