| | @@ -31,11 +31,10 @@ |
| 31 | 31 | |
| 32 | 32 | <nowiki><pre> |
| 33 | 33 | query $stmt step |
| 34 | 34 | query step $stmt |
| 35 | 35 | |
| 36 | | - |
| 37 | 36 | query $stmt finalize |
| 38 | 37 | query finalize $stmt |
| 39 | 38 | |
| 40 | 39 | query col string $stmt 1 |
| 41 | 40 | query $stmt col string 1 |
| | @@ -42,27 +41,44 @@ |
| 42 | 41 | |
| 43 | 42 | query bind string $stmt 1 "foo" |
| 44 | 43 | query $stmt bind string 1 "foo" |
| 45 | 44 | </pre></nowiki> |
| 46 | 45 | |
| 46 | +The "prefered" form is: |
| 47 | + |
| 48 | +<nowiki><pre> |
| 49 | +query StmtId command ... |
| 50 | +</pre></nowiki> |
| 51 | + |
| 52 | +(Why, then, are both forms accepted? Because the "preferred" form only |
| 53 | +evolved only after using the first form in script code.) |
| 54 | + |
| 47 | 55 | |
| 48 | 56 | <h2>prepare</h2> |
| 49 | 57 | |
| 50 | 58 | This subcommand prepares a query for execution. It returns a statement handle |
| 51 | 59 | ID which must be passed to any other functions using the API. |
| 52 | 60 | |
| 53 | 61 | All prepared statements must be <tt>finalize</tt>d when they have outlived |
| 54 | 62 | their usefulness. |
| 63 | + |
| 64 | +<nowiki><pre> |
| 65 | +set stmt [query prepare {SELECT ...}] |
| 66 | +... |
| 67 | +query $stmt finalize |
| 68 | +</pre></nowiki> |
| 69 | + |
| 55 | 70 | |
| 56 | 71 | <h2>finalize</h2> |
| 57 | 72 | |
| 58 | 73 | Releases all resources associated with the statement. Note that future |
| 59 | 74 | calls to <tt>prepare</tt> might re-use the same statement statement |
| 60 | 75 | ID. |
| 61 | 76 | |
| 62 | 77 | <nowiki><pre> |
| 63 | 78 | set stmt [query prepare "SELECT ..."] |
| 79 | +... |
| 64 | 80 | query $stmt finalize |
| 65 | 81 | </pre></nowiki> |
| 66 | 82 | |
| 67 | 83 | |
| 68 | 84 | <h2>step</h2> |
| | @@ -70,11 +86,11 @@ |
| 70 | 86 | This subcommand steps the result set by one row. It returns 0 |
| 71 | 87 | at the end of the set, a positive value if a new row is available, |
| 72 | 88 | and throws for any other condition. |
| 73 | 89 | |
| 74 | 90 | <nowiki><pre> |
| 75 | | -for {} {0 < [query $stmt step]} {} { |
| 91 | +for {} {[query $stmt step]} {} { |
| 76 | 92 | puts [query $stmt col string 0] "\n" |
| 77 | 93 | } |
| 78 | 94 | </pre></nowiki> |
| 79 | 95 | |
| 80 | 96 | |
| | @@ -92,14 +108,14 @@ |
| 92 | 108 | <h2>bind xxx</h2> |
| 93 | 109 | |
| 94 | 110 | The <tt>bind xxx</tt> family of subcommands attach values to queries |
| 95 | 111 | before stepping through them. The subcommands include: |
| 96 | 112 | |
| 97 | | - * <tt>bind int StmtId Index Value</tt> |
| 98 | | - * <tt>bind double StmtId Index Value</tt> |
| 99 | | - * <tt>bind null StmtId Index</tt> |
| 100 | | - * <tt>bind string StmtId Index Value</tt> |
| 113 | + * <tt>bind StmtId int Index Value</tt> |
| 114 | + * <tt>bind StmtId double Index Value</tt> |
| 115 | + * <tt>bind StmtId null Index</tt> |
| 116 | + * <tt>bind StmtId string Index Value</tt> |
| 101 | 117 | |
| 102 | 118 | Note that all of those optionally accept the statement handle directly after |
| 103 | 119 | the "query" command (before the "col" subcommand). e.g. |
| 104 | 120 | <tt>query bind null $stmt 1</tt> and |
| 105 | 121 | <tt>query $stmt bind null 1</tt> are equivalent. They also accept the column index |
| | @@ -110,29 +126,26 @@ |
| 110 | 126 | Achtung: the bind API uses 1-based indexes, just like SQL does. |
| 111 | 127 | |
| 112 | 128 | <nowiki><pre> |
| 113 | 129 | set stmt [query prepare "SELECT ... WHERE user=?"] |
| 114 | 130 | query $stmt bind int 1 drh |
| 115 | | -if {0 < [query $stmt step]} { |
| 131 | +if {[query $stmt step]} { |
| 116 | 132 | puts [query $stmt col string 0] "\n" |
| 117 | 133 | } |
| 118 | 134 | query $stmt finalize |
| 119 | 135 | </pre></nowiki> |
| 120 | 136 | |
| 121 | 137 | |
| 122 | 138 | <h2>col xxx</h2> |
| 123 | 139 | |
| 124 | | -The <tt>col xxx</tt> familys of subcommands are for fetching |
| 125 | | -values and metadata from result rows. |
| 126 | | - |
| 127 | | - * <tt>col count StmtId</tt> Returns the number of result columns in the statement. |
| 128 | | - * <tt>col isnull StmtId Index</tt> Returns non-0 if the given column contains an SQL NULL value. |
| 129 | | - * <tt>col double StmtId Index</tt> |
| 130 | | - * <tt>col int StmtId Index</tt> |
| 131 | | - * <tt>col string StmtId Index</tt> |
| 132 | | - * <tt>col string StmtId Index Format Modifiers</tt> See below. |
| 133 | | - * <tt>col type StmtId Index</tt> Return value corresponds to one of the <tt>SQLITE_TYPENAME</tt> family of constants. |
| 140 | +The <tt>col xxx</tt> familys of subcommands are for fetching values and metadata from result rows. |
| 141 | + |
| 142 | + * <tt>col StmtId count</tt> Returns the number of result columns in the statement. |
| 143 | + * <tt>col StmtId isnull Index</tt> Returns non-0 if the given column contains an SQL NULL value. |
| 144 | + * <tt>col StmtId (double|int|string) Index</tt> Fetches a column's value as either a number or string. |
| 145 | + * <tt>col StmtId time Index Format Modifiers</tt> Formats a time value. See below. |
| 146 | + * <tt>col StmtId type Index</tt> Returns the given column's type as a value from the <tt>SQLITE_TYPENAME</tt> family of constants. |
| 134 | 147 | |
| 135 | 148 | Note that all of those optionally accept the statement handle directly after |
| 136 | 149 | the "query" command (before the "col" subcommand). e.g. |
| 137 | 150 | <tt>query $stmt col count</tt> and |
| 138 | 151 | <tt>query col count $stmt</tt> are equivalent. They also accept the column index |
| | @@ -169,5 +182,18 @@ |
| 169 | 182 | <nowiki><pre> |
| 170 | 183 | query strftime %s 1319211587 unixepoch |
| 171 | 184 | query strftime {%Y%m%d @ %H:%M:%S} [query $stmt col string 2] {+10 years}] |
| 172 | 185 | </pre></nowiki> |
| 173 | 186 | |
| 187 | +<h2>Global Variables</h2> |
| 188 | + |
| 189 | +This API installs the following global variables, all of which correspond to |
| 190 | +<tt>SQLITE_xxx</tt> constant values: |
| 191 | + |
| 192 | + * <tt>SQLITE_BLOB</tt> |
| 193 | + * <tt>SQLITE_FLOAT</tt> |
| 194 | + * <tt>SQLITE_INTEGER</tt> |
| 195 | + * <tt>SQLITE_NULL</tt> |
| 196 | + * <tt>SQLITE_TEXT</tt> |
| 197 | + |
| 198 | +These values are used only by the <tt>col type</tt> function. They can be |
| 199 | +accessed from script code via <tt>$::SQLITE_xxx</tt>. |
| 174 | 200 | |