| 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>An Introduction To The SQLite C/C++ Interface</title> |
| 7 |
<!-- path= --> |
| 8 |
</head> |
| 9 |
<body> |
| 10 |
<div class=nosearch> |
| 11 |
<a href="index.html"> |
| 12 |
<img class="logo" src="images/sqlite370_banner.svg" 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 |
<div class=fancy> |
| 119 |
<div class=nosearch> |
| 120 |
<div class="fancy_title"> |
| 121 |
An Introduction To The SQLite C/C++ Interface |
| 122 |
</div> |
| 123 |
<details class="fancy_toc"> |
| 124 |
<summary>Table Of Contents</summary> |
| 125 |
<div id="toc_sub"><div class="fancy-toc1"><a href="#summary">1. Summary</a></div> |
| 126 |
<div class="fancy-toc1"><a href="#introduction">2. Introduction</a></div> |
| 127 |
<div class="fancy-toc1"><a href="#core_objects_and_interfaces">3. Core Objects And Interfaces</a></div> |
| 128 |
<div class="fancy-toc1"><a href="#typical_usage_of_core_routines_and_objects">4. Typical Usage Of Core Routines And Objects</a></div> |
| 129 |
<div class="fancy-toc1"><a href="#convenience_wrappers_around_core_routines">5. Convenience Wrappers Around Core Routines</a></div> |
| 130 |
<div class="fancy-toc1"><a href="#binding_parameters_and_reusing_prepared_statements">6. Binding Parameters and Reusing Prepared Statements</a></div> |
| 131 |
<div class="fancy-toc1"><a href="#configuring_sqlite">7. Configuring SQLite</a></div> |
| 132 |
<div class="fancy-toc1"><a href="#extending_sqlite">8. Extending SQLite</a></div> |
| 133 |
<div class="fancy-toc1"><a href="#other_interfaces">9. Other Interfaces</a></div> |
| 134 |
</div> |
| 135 |
</details> |
| 136 |
</div> |
| 137 |
|
| 138 |
|
| 139 |
|
| 140 |
|
| 141 |
|
| 142 |
|
| 143 |
<h1 id="summary"><span>1. </span>Summary</h1> |
| 144 |
|
| 145 |
<p>The following two objects and eight methods comprise the essential |
| 146 |
elements of the SQLite interface: |
| 147 |
|
| 148 |
</p><ul> |
| 149 |
<li><p><b><a href="c3ref/sqlite3.html">sqlite3</a></b> → |
| 150 |
The database connection object. Created by |
| 151 |
<a href="c3ref/open.html">sqlite3_open()</a> and destroyed by <a href="c3ref/close.html">sqlite3_close()</a>. |
| 152 |
|
| 153 |
</p></li><li><p><b><a href="c3ref/stmt.html">sqlite3_stmt</a></b> → |
| 154 |
The prepared statement object. Created by |
| 155 |
<a href="c3ref/prepare.html">sqlite3_prepare()</a> and destroyed by <a href="c3ref/finalize.html">sqlite3_finalize()</a>. |
| 156 |
|
| 157 |
|
| 158 |
</p></li><li><p><b><a href="c3ref/open.html">sqlite3_open()</a></b> → |
| 159 |
Open a connection to a new or existing SQLite database. |
| 160 |
The constructor for <a href="c3ref/sqlite3.html">sqlite3</a>. |
| 161 |
|
| 162 |
</p></li><li><p><b><a href="c3ref/prepare.html">sqlite3_prepare()</a></b> → |
| 163 |
Compile SQL text into |
| 164 |
byte-code that will do the work of querying or updating the database. |
| 165 |
The constructor for <a href="c3ref/stmt.html">sqlite3_stmt</a>. |
| 166 |
|
| 167 |
</p></li><li><p><b><a href="c3ref/bind_blob.html">sqlite3_bind()</a></b> → |
| 168 |
Store application data into |
| 169 |
<a href="lang_expr.html#varparam">parameters</a> of the original SQL. |
| 170 |
|
| 171 |
|
| 172 |
</p></li><li><p><b><a href="c3ref/step.html">sqlite3_step()</a></b> → |
| 173 |
Advance an <a href="c3ref/stmt.html">sqlite3_stmt</a> to the next result row or to completion. |
| 174 |
|
| 175 |
</p></li><li><p><b><a href="c3ref/column_blob.html">sqlite3_column()</a></b> → |
| 176 |
Column values in the current result row for an <a href="c3ref/stmt.html">sqlite3_stmt</a>. |
| 177 |
|
| 178 |
</p></li><li><p><b><a href="c3ref/finalize.html">sqlite3_finalize()</a></b> → |
| 179 |
Destructor for <a href="c3ref/stmt.html">sqlite3_stmt</a>. |
| 180 |
|
| 181 |
</p></li><li><p><b><a href="c3ref/close.html">sqlite3_close()</a></b> → |
| 182 |
Destructor for <a href="c3ref/sqlite3.html">sqlite3</a>. |
| 183 |
|
| 184 |
</p></li><li><p><b><a href="c3ref/exec.html">sqlite3_exec()</a></b> → |
| 185 |
A wrapper function that does <a href="c3ref/prepare.html">sqlite3_prepare()</a>, <a href="c3ref/step.html">sqlite3_step()</a>, |
| 186 |
<a href="c3ref/column_blob.html">sqlite3_column()</a>, and <a href="c3ref/finalize.html">sqlite3_finalize()</a> for |
| 187 |
a string of one or more SQL statements. |
| 188 |
</p></li></ul> |
| 189 |
|
| 190 |
<h1 id="introduction"><span>2. </span>Introduction</h1> |
| 191 |
|
| 192 |
<p> |
| 193 |
SQLite has more than 225 APIs. |
| 194 |
However, most of the APIs are optional and very specialized |
| 195 |
and can be ignored by beginners. |
| 196 |
The core API is small, simple, and easy to learn. |
| 197 |
This article summarizes the core API. |
| 198 |
</p> |
| 199 |
|
| 200 |
<p> |
| 201 |
A separate document, <a href="c3ref/intro.html">The SQLite C/C++ Interface</a>, |
| 202 |
provides detailed |
| 203 |
specifications for all C/C++ APIs for SQLite. Once |
| 204 |
the reader |
| 205 |
understands the basic principles of operation for SQLite, |
| 206 |
<a href="c3ref/intro.html">that document</a> should be used as a reference |
| 207 |
guide. This article is intended as introduction only and is neither a |
| 208 |
complete nor authoritative reference for the SQLite API. |
| 209 |
</p> |
| 210 |
|
| 211 |
<h1 id="core_objects_and_interfaces"><span>3. </span>Core Objects And Interfaces</h1> |
| 212 |
|
| 213 |
<p> |
| 214 |
The principal task of an SQL database engine is to evaluate SQL statements |
| 215 |
of SQL. To accomplish this, the developer needs two objects: |
| 216 |
</p> |
| 217 |
|
| 218 |
<p></p><ul> |
| 219 |
<li> The <a href="c3ref/sqlite3.html">database connection</a> object: sqlite3 </li> |
| 220 |
<li> The <a href="c3ref/stmt.html">prepared statement</a> object: sqlite3_stmt </li> |
| 221 |
</ul> |
| 222 |
|
| 223 |
<p> |
| 224 |
Strictly speaking, the <a href="c3ref/stmt.html">prepared statement</a> object is not required since |
| 225 |
the convenience wrapper interfaces, <a href="c3ref/exec.html">sqlite3_exec</a> or |
| 226 |
<a href="c3ref/free_table.html">sqlite3_get_table</a>, can be used and these convenience wrappers |
| 227 |
encapsulate and hide the <a href="c3ref/stmt.html">prepared statement</a> object. |
| 228 |
Nevertheless, an understanding of |
| 229 |
<a href="c3ref/stmt.html">prepared statements</a> is needed to make full use of SQLite. |
| 230 |
</p> |
| 231 |
|
| 232 |
<p> |
| 233 |
The <a href="c3ref/sqlite3.html">database connection</a> and <a href="c3ref/stmt.html">prepared statement</a> objects are controlled |
| 234 |
by a small set of C/C++ interface routines listed below. |
| 235 |
</p> |
| 236 |
|
| 237 |
<p></p><ul> |
| 238 |
<li> <a href="c3ref/open.html">sqlite3_open()</a> </li> |
| 239 |
<li> <a href="c3ref/prepare.html">sqlite3_prepare()</a> </li> |
| 240 |
<li> <a href="c3ref/step.html">sqlite3_step()</a> </li> |
| 241 |
<li> <a href="c3ref/column_blob.html">sqlite3_column()</a> </li> |
| 242 |
<li> <a href="c3ref/finalize.html">sqlite3_finalize()</a> </li> |
| 243 |
<li> <a href="c3ref/close.html">sqlite3_close()</a> </li> |
| 244 |
</ul> |
| 245 |
|
| 246 |
<p> |
| 247 |
Note that the list of routines above is conceptual rather than actual. |
| 248 |
Many of these routines come in multiple versions. |
| 249 |
For example, the list above shows a single routine |
| 250 |
named <a href="c3ref/open.html">sqlite3_open()</a> when in fact there are three separate routines |
| 251 |
that accomplish the same thing in slightly different ways: |
| 252 |
<a href="c3ref/open.html">sqlite3_open()</a>, <a href="c3ref/open.html">sqlite3_open16()</a> and <a href="c3ref/open.html">sqlite3_open_v2()</a>. |
| 253 |
The list mentions <a href="c3ref/column_blob.html">sqlite3_column()</a> |
| 254 |
when in fact no such routine exists. |
| 255 |
The "sqlite3_column()" shown in the list is a placeholder for |
| 256 |
an entire family of routines that extract column |
| 257 |
data in various datatypes. |
| 258 |
</p> |
| 259 |
|
| 260 |
<p> |
| 261 |
Here is a summary of what the core interfaces do: |
| 262 |
</p> |
| 263 |
|
| 264 |
<ul> |
| 265 |
|
| 266 |
<li><p><b><a href="c3ref/open.html">sqlite3_open()</a></b> |
| 267 |
</p><p> |
| 268 |
This routine |
| 269 |
opens a connection to an SQLite database file and returns a |
| 270 |
<a href="c3ref/sqlite3.html">database connection</a> object. This is often the first SQLite API |
| 271 |
call that an application makes and is a prerequisite for most other |
| 272 |
SQLite APIs. Many SQLite interfaces require a pointer to |
| 273 |
the <a href="c3ref/sqlite3.html">database connection</a> object as their first parameter and can |
| 274 |
be thought of as methods on the <a href="c3ref/sqlite3.html">database connection</a> object. |
| 275 |
This routine is the constructor for the <a href="c3ref/sqlite3.html">database connection</a> object. |
| 276 |
</p> |
| 277 |
|
| 278 |
</li><li><p><b><a href="c3ref/prepare.html">sqlite3_prepare()</a></b> |
| 279 |
</p><p> |
| 280 |
This routine |
| 281 |
converts SQL text into a <a href="c3ref/stmt.html">prepared statement</a> object and returns a pointer |
| 282 |
to that object. This interface requires a <a href="c3ref/sqlite3.html">database connection</a> pointer |
| 283 |
created by a prior call to <a href="c3ref/open.html">sqlite3_open()</a> and a text string containing |
| 284 |
the SQL statement to be prepared. This API does not actually evaluate |
| 285 |
the SQL statement. It merely prepares the SQL statement for evaluation. |
| 286 |
|
| 287 |
</p><p>Think of each SQL statement as a small computer program. The purpose |
| 288 |
of <a href="c3ref/prepare.html">sqlite3_prepare()</a> is to compile that program into object code. |
| 289 |
The <a href="c3ref/stmt.html">prepared statement</a> is the object code. The <a href="c3ref/step.html">sqlite3_step()</a> interface |
| 290 |
then runs the object code to get a result. |
| 291 |
|
| 292 |
</p><p>New applications should always invoke <a href="c3ref/prepare.html">sqlite3_prepare_v2()</a> instead |
| 293 |
of <a href="c3ref/prepare.html">sqlite3_prepare()</a>. The older <a href="c3ref/prepare.html">sqlite3_prepare()</a> is retained for |
| 294 |
backwards compatibility. But <a href="c3ref/prepare.html">sqlite3_prepare_v2()</a> provides a much |
| 295 |
better interface.</p> |
| 296 |
|
| 297 |
</li><li><p><b><a href="c3ref/step.html">sqlite3_step()</a></b> |
| 298 |
</p><p> |
| 299 |
This routine is used to evaluate a <a href="c3ref/stmt.html">prepared statement</a> that has been |
| 300 |
previously created by the <a href="c3ref/prepare.html">sqlite3_prepare()</a> interface. The statement |
| 301 |
is evaluated up to the point where the first row of results are available. |
| 302 |
To advance to the second row of results, invoke <a href="c3ref/step.html">sqlite3_step()</a> again. |
| 303 |
Continue invoking <a href="c3ref/step.html">sqlite3_step()</a> until the statement is complete. |
| 304 |
Statements that do not return results (ex: INSERT, UPDATE, or DELETE |
| 305 |
statements) run to completion on a single call to <a href="c3ref/step.html">sqlite3_step()</a>. |
| 306 |
|
| 307 |
</p></li><li><p><b><a href="c3ref/column_blob.html">sqlite3_column()</a></b> |
| 308 |
</p><p> |
| 309 |
This routine returns a single column from the current row of a result |
| 310 |
set for a <a href="c3ref/stmt.html">prepared statement</a> that is being evaluated by <a href="c3ref/step.html">sqlite3_step()</a>. |
| 311 |
Each time <a href="c3ref/step.html">sqlite3_step()</a> stops with a new result set row, this routine |
| 312 |
can be called multiple times to find the values of all columns in that row. |
| 313 |
|
| 314 |
</p><p>As noted above, there really is no such thing as a "sqlite3_column()" |
| 315 |
function in the SQLite API. Instead, what we here call "sqlite3_column()" |
| 316 |
is a place-holder for an entire family of functions that return |
| 317 |
a value from the result set in various data types. There are also routines |
| 318 |
in this family that return the size of the result (if it is a string or |
| 319 |
BLOB) and the number of columns in the result set. |
| 320 |
|
| 321 |
</p><p></p><ul> |
| 322 |
<li> <a href="c3ref/column_blob.html">sqlite3_column_blob()</a> </li> |
| 323 |
<li> <a href="c3ref/column_blob.html">sqlite3_column_bytes()</a> </li> |
| 324 |
<li> <a href="c3ref/column_blob.html">sqlite3_column_bytes16()</a> </li> |
| 325 |
<li> <a href="c3ref/column_count.html">sqlite3_column_count()</a> </li> |
| 326 |
<li> <a href="c3ref/column_blob.html">sqlite3_column_double()</a> </li> |
| 327 |
<li> <a href="c3ref/column_blob.html">sqlite3_column_int()</a> </li> |
| 328 |
<li> <a href="c3ref/column_blob.html">sqlite3_column_int64()</a> </li> |
| 329 |
<li> <a href="c3ref/column_blob.html">sqlite3_column_text()</a> </li> |
| 330 |
<li> <a href="c3ref/column_blob.html">sqlite3_column_text16()</a> </li> |
| 331 |
<li> <a href="c3ref/column_blob.html">sqlite3_column_type()</a> </li> |
| 332 |
<li> <a href="c3ref/column_blob.html">sqlite3_column_value()</a> </li> |
| 333 |
</ul> |
| 334 |
|
| 335 |
|
| 336 |
</li><li><p><b><a href="c3ref/finalize.html">sqlite3_finalize()</a></b> |
| 337 |
</p><p> |
| 338 |
This routine destroys a <a href="c3ref/stmt.html">prepared statement</a> created by a prior call |
| 339 |
to <a href="c3ref/prepare.html">sqlite3_prepare()</a>. Every prepared statement must be destroyed using |
| 340 |
a call to this routine in order to avoid memory leaks. |
| 341 |
|
| 342 |
</p></li><li><p><b><a href="c3ref/close.html">sqlite3_close()</a></b> |
| 343 |
</p><p> |
| 344 |
This routine closes a <a href="c3ref/sqlite3.html">database connection</a> previously opened by a call |
| 345 |
to <a href="c3ref/open.html">sqlite3_open()</a>. All <a href="c3ref/stmt.html">prepared statements</a> associated with the |
| 346 |
connection should be <a href="c3ref/finalize.html">finalized</a> prior to closing the |
| 347 |
connection. |
| 348 |
</p></li></ul> |
| 349 |
|
| 350 |
<h1 id="typical_usage_of_core_routines_and_objects"><span>4. </span>Typical Usage Of Core Routines And Objects</h1> |
| 351 |
|
| 352 |
<p> |
| 353 |
An application will typically use |
| 354 |
<a href="c3ref/open.html">sqlite3_open()</a> to create a single <a href="c3ref/sqlite3.html">database connection</a> |
| 355 |
during initialization. |
| 356 |
Note that <a href="c3ref/open.html">sqlite3_open()</a> can be used to either open existing database |
| 357 |
files or to create and open new database files. |
| 358 |
While many applications use only a single <a href="c3ref/sqlite3.html">database connection</a>, there is |
| 359 |
no reason why an application cannot call <a href="c3ref/open.html">sqlite3_open()</a> multiple times |
| 360 |
in order to open multiple <a href="c3ref/sqlite3.html">database connections</a> - either to the same |
| 361 |
database or to different databases. Sometimes a multi-threaded application |
| 362 |
will create separate <a href="c3ref/sqlite3.html">database connections</a> for each thread. |
| 363 |
Note that a single <a href="c3ref/sqlite3.html">database connection</a> can access two or more |
| 364 |
databases using the <a href="lang_attach.html">ATTACH</a> SQL command, so it is not necessary to |
| 365 |
have a separate database connection for each database file. |
| 366 |
</p> |
| 367 |
|
| 368 |
<p> |
| 369 |
Many applications destroy their <a href="c3ref/sqlite3.html">database connections</a> using calls to |
| 370 |
<a href="c3ref/close.html">sqlite3_close()</a> at shutdown. Or, for example, an application that |
| 371 |
uses SQLite as its <a href="appfileformat.html">application file format</a> might |
| 372 |
open <a href="c3ref/sqlite3.html">database connections</a> in response to a File/Open menu action |
| 373 |
and then destroy the corresponding <a href="c3ref/sqlite3.html">database connection</a> in response |
| 374 |
to the File/Close menu. |
| 375 |
</p> |
| 376 |
|
| 377 |
<p> |
| 378 |
To run an SQL statement, the application follows these steps: |
| 379 |
</p> |
| 380 |
|
| 381 |
<p></p><ol> |
| 382 |
<li> Create a <a href="c3ref/stmt.html">prepared statement</a> using <a href="c3ref/prepare.html">sqlite3_prepare()</a>. </li> |
| 383 |
<li> Evaluate the <a href="c3ref/stmt.html">prepared statement</a> by calling <a href="c3ref/step.html">sqlite3_step()</a> one |
| 384 |
or more times. </li> |
| 385 |
<li> For queries, extract results by calling |
| 386 |
<a href="c3ref/column_blob.html">sqlite3_column()</a> in between |
| 387 |
two calls to <a href="c3ref/step.html">sqlite3_step()</a>. </li> |
| 388 |
<li> Destroy the <a href="c3ref/stmt.html">prepared statement</a> using <a href="c3ref/finalize.html">sqlite3_finalize()</a>. </li> |
| 389 |
</ol> |
| 390 |
|
| 391 |
<p> |
| 392 |
The foregoing is all one really needs to know in order to use SQLite |
| 393 |
effectively. All the rest is optimization and detail. |
| 394 |
</p> |
| 395 |
|
| 396 |
<h1 id="convenience_wrappers_around_core_routines"><span>5. </span>Convenience Wrappers Around Core Routines</h1> |
| 397 |
|
| 398 |
<p> |
| 399 |
The <a href="c3ref/exec.html">sqlite3_exec()</a> interface is a convenience wrapper that carries out |
| 400 |
all four of the above steps with a single function call. A callback |
| 401 |
function passed into <a href="c3ref/exec.html">sqlite3_exec()</a> is used to process each row of |
| 402 |
the result set. The <a href="c3ref/free_table.html">sqlite3_get_table()</a> is another convenience wrapper |
| 403 |
that does all four of the above steps. The <a href="c3ref/free_table.html">sqlite3_get_table()</a> interface |
| 404 |
differs from <a href="c3ref/exec.html">sqlite3_exec()</a> in that it stores the results of queries |
| 405 |
in heap memory rather than invoking a callback. |
| 406 |
</p> |
| 407 |
|
| 408 |
<p> |
| 409 |
It is important to realize that neither <a href="c3ref/exec.html">sqlite3_exec()</a> nor |
| 410 |
<a href="c3ref/free_table.html">sqlite3_get_table()</a> do anything that cannot be accomplished using |
| 411 |
the core routines. In fact, these wrappers are implemented purely in |
| 412 |
terms of the core routines. |
| 413 |
</p> |
| 414 |
|
| 415 |
|
| 416 |
<h1 id="binding_parameters_and_reusing_prepared_statements"><span>6. </span>Binding Parameters and Reusing Prepared Statements</h1> |
| 417 |
|
| 418 |
<p> |
| 419 |
In prior discussion, it was assumed that each SQL statement is prepared |
| 420 |
once, evaluated, then destroyed. However, SQLite allows the same |
| 421 |
<a href="c3ref/stmt.html">prepared statement</a> to be evaluated multiple times. This is accomplished |
| 422 |
using the following routines: |
| 423 |
</p> |
| 424 |
|
| 425 |
<p></p><ul> |
| 426 |
<li> <a href="c3ref/reset.html">sqlite3_reset()</a> </li> |
| 427 |
<li> <a href="c3ref/bind_blob.html">sqlite3_bind()</a> </li> |
| 428 |
</ul> |
| 429 |
|
| 430 |
<p> |
| 431 |
After a <a href="c3ref/stmt.html">prepared statement</a> has been evaluated by one or more calls to |
| 432 |
<a href="c3ref/step.html">sqlite3_step()</a>, it can be reset in order to be evaluated again by a |
| 433 |
call to <a href="c3ref/reset.html">sqlite3_reset()</a>. |
| 434 |
Think of <a href="c3ref/reset.html">sqlite3_reset()</a> as rewinding the <a href="c3ref/stmt.html">prepared statement</a> program |
| 435 |
back to the beginning. |
| 436 |
Using <a href="c3ref/reset.html">sqlite3_reset()</a> on an existing <a href="c3ref/stmt.html">prepared statement</a> rather than |
| 437 |
creating a new <a href="c3ref/stmt.html">prepared statement</a> avoids unnecessary calls to |
| 438 |
<a href="c3ref/prepare.html">sqlite3_prepare()</a>. |
| 439 |
For many SQL statements, the time needed |
| 440 |
to run <a href="c3ref/prepare.html">sqlite3_prepare()</a> equals or exceeds the time needed by |
| 441 |
<a href="c3ref/step.html">sqlite3_step()</a>. So avoiding calls to <a href="c3ref/prepare.html">sqlite3_prepare()</a> can give |
| 442 |
a significant performance improvement. |
| 443 |
</p> |
| 444 |
|
| 445 |
<p> |
| 446 |
It is not commonly useful to evaluate the <em>exact</em> same SQL |
| 447 |
statement more than once. More often, one wants to evaluate similar |
| 448 |
statements. For example, you might want to evaluate an INSERT statement |
| 449 |
multiple times with different values. Or you might want to evaluate |
| 450 |
the same query multiple times using a different key in the WHERE clause. |
| 451 |
To accommodate |
| 452 |
this, SQLite allows SQL statements to contain <a href="lang_expr.html#varparam">parameters</a> |
| 453 |
which are "bound" to values prior to being evaluated. These values can |
| 454 |
later be changed and the same <a href="c3ref/stmt.html">prepared statement</a> can be evaluated |
| 455 |
a second time using the new values. |
| 456 |
</p> |
| 457 |
|
| 458 |
<p> |
| 459 |
SQLite allows a <a href="lang_expr.html#varparam">parameter</a> wherever a string literal, |
| 460 |
blob literal, numeric constant, or NULL is allowed |
| 461 |
in queries or data modification statements. (DQL or DML) |
| 462 |
(Parameters may not be used for column or table names, |
| 463 |
or as values for constraints or default values. (DDL)) |
| 464 |
A <a href="lang_expr.html#varparam">parameter</a> takes one of the following forms: |
| 465 |
</p> |
| 466 |
|
| 467 |
<p></p><ul> |
| 468 |
<li> <b>?</b> </li> |
| 469 |
<li> <b>?</b><i>NNN</i> </li> |
| 470 |
<li> <b>:</b><i>AAA</i> </li> |
| 471 |
<li> <b>$</b><i>AAA</i> </li> |
| 472 |
<li> <b>@</b><i>AAA</i> </li> |
| 473 |
</ul> |
| 474 |
|
| 475 |
<p> |
| 476 |
In the examples above, <i>NNN</i> is an integer value and |
| 477 |
<i>AAA</i> is an identifier. |
| 478 |
A parameter initially has a value of NULL. |
| 479 |
Prior to calling <a href="c3ref/step.html">sqlite3_step()</a> for the first time or immediately |
| 480 |
after <a href="c3ref/reset.html">sqlite3_reset()</a>, the application can invoke the |
| 481 |
<a href="c3ref/bind_blob.html">sqlite3_bind()</a> interfaces to attach values |
| 482 |
to the parameters. Each call to <a href="c3ref/bind_blob.html">sqlite3_bind()</a> |
| 483 |
overrides prior bindings on the same parameter. |
| 484 |
</p> |
| 485 |
|
| 486 |
<p> |
| 487 |
An application is allowed to prepare multiple SQL statements in advance |
| 488 |
and evaluate them as needed. |
| 489 |
There is no arbitrary limit to the number of outstanding |
| 490 |
<a href="c3ref/stmt.html">prepared statements</a>. |
| 491 |
Some applications call <a href="c3ref/prepare.html">sqlite3_prepare()</a> multiple times at start-up to |
| 492 |
create all of the <a href="c3ref/stmt.html">prepared statements</a> they will ever need. Other |
| 493 |
applications keep a cache of the most recently used <a href="c3ref/stmt.html">prepared statements</a> |
| 494 |
and then reuse <a href="c3ref/stmt.html">prepared statements</a> out of the cache when available. |
| 495 |
Another approach is to only reuse <a href="c3ref/stmt.html">prepared statements</a> when they are |
| 496 |
inside of a loop. |
| 497 |
</p> |
| 498 |
|
| 499 |
<h1 id="configuring_sqlite"><span>7. </span>Configuring SQLite</h1> |
| 500 |
|
| 501 |
<p> |
| 502 |
The default configuration for SQLite works great for most applications. |
| 503 |
But sometimes developers want to tweak the setup to try to squeeze out |
| 504 |
a little more performance, or take advantage of some obscure feature. |
| 505 |
</p><p> |
| 506 |
The <a href="c3ref/config.html">sqlite3_config()</a> interface is used to make global, process-wide |
| 507 |
configuration changes for SQLite. The <a href="c3ref/config.html">sqlite3_config()</a> interface must |
| 508 |
be called before any <a href="c3ref/sqlite3.html">database connections</a> are created. The |
| 509 |
<a href="c3ref/config.html">sqlite3_config()</a> interface allows the programmer to do things like: |
| 510 |
</p><ul> |
| 511 |
<li>Adjust how SQLite does <a href="malloc.html">memory allocation</a>, including setting up |
| 512 |
alternative memory allocators appropriate for safety-critical |
| 513 |
real-time embedded systems and application-defined memory allocators. |
| 514 |
</li><li>Set up a process-wide <a href="errlog.html">error log</a>. |
| 515 |
</li><li>Specify an application-defined page cache. |
| 516 |
</li><li>Adjust the use of mutexes so that they are appropriate for various |
| 517 |
<a href="threadsafe.html">threading models</a>, or substitute an |
| 518 |
application-defined mutex system. |
| 519 |
</li></ul> |
| 520 |
<p> |
| 521 |
After process-wide configuration is complete and <a href="c3ref/sqlite3.html">database connections</a> |
| 522 |
have been created, individual database connections can be configured using |
| 523 |
calls to <a href="c3ref/limit.html">sqlite3_limit()</a> and <a href="c3ref/db_config.html">sqlite3_db_config()</a>. |
| 524 |
|
| 525 |
</p><h1 id="extending_sqlite"><span>8. </span>Extending SQLite</h1> |
| 526 |
|
| 527 |
<p> |
| 528 |
SQLite includes interfaces that can be used to extend its functionality. |
| 529 |
Such routines include: |
| 530 |
</p> |
| 531 |
|
| 532 |
<p></p><ul> |
| 533 |
<li> <a href="c3ref/create_collation.html">sqlite3_create_collation()</a> </li> |
| 534 |
<li> <a href="c3ref/create_function.html">sqlite3_create_function()</a> </li> |
| 535 |
<li> <a href="c3ref/create_module.html">sqlite3_create_module()</a> </li> |
| 536 |
<li> <a href="c3ref/vfs_find.html">sqlite3_vfs_register()</a> </li> |
| 537 |
</ul> |
| 538 |
|
| 539 |
<p> |
| 540 |
The <a href="c3ref/create_collation.html">sqlite3_create_collation()</a> interface is used to create new |
| 541 |
<a href="datatype3.html#collation">collating sequences</a> for sorting text. |
| 542 |
The <a href="c3ref/create_module.html">sqlite3_create_module()</a> interface is used to register new |
| 543 |
<a href="vtab.html">virtual table</a> implementations. |
| 544 |
The <a href="c3ref/vfs_find.html">sqlite3_vfs_register()</a> interface creates new <a href="vfs.html">VFSes</a>. |
| 545 |
</p> |
| 546 |
|
| 547 |
<p> |
| 548 |
The <a href="c3ref/create_function.html">sqlite3_create_function()</a> interface creates new SQL functions - |
| 549 |
either scalar or aggregate. The new function implementation typically |
| 550 |
makes use of the following additional interfaces: |
| 551 |
</p> |
| 552 |
|
| 553 |
<p></p><ul> |
| 554 |
<li> <a href="c3ref/aggregate_context.html">sqlite3_aggregate_context()</a> </li> |
| 555 |
<li> <a href="c3ref/result_blob.html">sqlite3_result()</a> </li> |
| 556 |
<li> <a href="c3ref/user_data.html">sqlite3_user_data()</a> </li> |
| 557 |
<li> <a href="c3ref/value_blob.html">sqlite3_value()</a> </li> |
| 558 |
</ul> |
| 559 |
|
| 560 |
<p> |
| 561 |
All of the built-in SQL functions of SQLite are created using exactly |
| 562 |
these same interfaces. Refer to the SQLite source code, and in particular |
| 563 |
the |
| 564 |
<a href="https://sqlite.org/src/doc/trunk/src/date.c">date.c</a> and |
| 565 |
<a href="https://sqlite.org/src/doc/trunk/src/func.c">func.c</a> source files |
| 566 |
for examples. |
| 567 |
</p> |
| 568 |
|
| 569 |
<p> |
| 570 |
Shared libraries or DLLs can be used as <a href="loadext.html">loadable extensions</a> to SQLite. |
| 571 |
|
| 572 |
</p><h1 id="other_interfaces"><span>9. </span>Other Interfaces</h1> |
| 573 |
|
| 574 |
<p> |
| 575 |
This article only mentions the most important and most commonly |
| 576 |
used SQLite interfaces. |
| 577 |
The SQLite library includes many other APIs implementing useful |
| 578 |
features that are not described here. |
| 579 |
A <a href="c3ref/funclist.html">complete list of functions</a> that form the SQLite |
| 580 |
application programming interface is found at the |
| 581 |
<a href="c3ref/intro.html">C/C++ Interface Specification</a>. |
| 582 |
Refer to that document for complete and authoritative information about |
| 583 |
all SQLite interfaces. |
| 584 |
</p> |
| 585 |
<p align="center"><small><i>This page was last updated on 2025-05-31 13:08:22Z </small></i></p> |
| 586 |
|