Prev
Back up cursor P1 so that it points to the previous key/data pair in its table or index.
Jumps: P2 is an instruction address. In the listings here, a jump is drawn as an arc and the address is a link.
An example
6 of the 178 statements we compiled against the sample database use Prev. This is the one with the shortest program; the rows that use Prev are highlighted.
SELECT name FROM artists ORDER BY name DESC LIMIT 3
| addr | opcode | p1 | p2 | p3 | p4 | p5 |
|---|---|---|---|---|---|---|
| 0 | Init | 0 | 10 | 0 | 0 | |
| 1 | Noop | 1 | 3 | 0 | 0 | |
| 2 | Integer | 3 | 1 | 0 | 0 | |
| 3 | OpenRead | 2 | 6 | 0 | k(2,,) | 0 |
| 4 | Last | 2 | 9 | 2 | 0 | 0 |
| 5 | Column | 2 | 0 | 2 | 0 | |
| 6 | ResultRow | 2 | 1 | 0 | 0 | |
| 7 | DecrJumpZero | 1 | 9 | 0 | 0 | |
| 8 | Prev | 2 | 5 | 0 | 1 | |
| 9 | Halt | 0 | 0 | 0 | 0 | |
| 10 | Transaction | 0 | 0 | 1 | 0 | 1 |
| 11 | Goto | 0 | 1 | 0 | 0 |
Open it in the EXPLAIN visualizer →
Also in this program: Column, DecrJumpZero, Goto, Halt, Init, Integer, Last, Noop, OpenRead, ResultRow, Transaction.
What the SQLite documentation says
Back up cursor P1 so that it points to the previous key/data pair in its table or index. If there is no previous key/value pairs then fall through to the following instruction. But if the cursor backup was successful, jump immediately to P2.
The Prev opcode is only valid following an SeekLT, SeekLE, or Last opcode used to position the cursor. Prev is not allowed to follow SeekGT, SeekGE, or Rewind.
The P1 cursor must be for a real table, not a pseudo-table. If P1 is not open then the behavior is undefined.
The P3 value is a hint to the btree implementation. If P3==1, that means P1 is an SQL index and that this instruction could have been omitted if that index had been unique. P3 is usually 0. P3 is always either 0 or 1.
If P5 is positive and the jump is taken, then event counter number P5-1 in the prepared statement is incremented.
From sqlite.org's list of opcodes for SQLite 3.54.1, in the public domain. Opcodes are not a stable interface and change between releases.
← Permutation · Program →