| 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>SQLite As An Application File Format</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 |
SQLite As An Application File Format |
| 122 |
</div> |
| 123 |
<details class="fancy_toc"> |
| 124 |
<summary>Table Of Contents</summary> |
| 125 |
<div id="toc_sub"><div class="fancy-toc1"><a href="#executive_summary">1. Executive Summary</a></div> |
| 126 |
<div class="fancy-toc1"><a href="#what_is_an_application_file_format_">2. What Is An Application File Format?</a></div> |
| 127 |
<div class="fancy-toc1"><a href="#sqlite_as_the_application_file_format">3. SQLite As The Application File Format</a></div> |
| 128 |
<div class="fancy-toc1"><a href="#security_considerations">4. Security Considerations</a></div> |
| 129 |
<div class="fancy-toc1"><a href="#conclusion">5. Conclusion</a></div> |
| 130 |
</div> |
| 131 |
</details> |
| 132 |
</div> |
| 133 |
|
| 134 |
|
| 135 |
|
| 136 |
|
| 137 |
|
| 138 |
<h1 id="executive_summary"><span>1. </span>Executive Summary</h1> |
| 139 |
|
| 140 |
<p>An SQLite database file with a defined schema |
| 141 |
often makes an excellent application file format. |
| 142 |
Here are a dozen reasons why this is so: |
| 143 |
|
| 144 |
</p><ol> |
| 145 |
<li> Simplified Application Development |
| 146 |
</li><li> Single-File Documents |
| 147 |
</li><li> High-Level Query Language |
| 148 |
</li><li> Accessible Content |
| 149 |
</li><li> Cross-Platform |
| 150 |
</li><li> Atomic Transactions |
| 151 |
</li><li> Incremental And Continuous Updates |
| 152 |
</li><li> Easily Extensible |
| 153 |
</li><li> Performance |
| 154 |
</li><li> Concurrent Use By Multiple Processes |
| 155 |
</li><li> Multiple Programming Languages |
| 156 |
</li><li> Better Applications |
| 157 |
</li></ol> |
| 158 |
|
| 159 |
<p>Each of these points will be described in more detail below, |
| 160 |
after first considering more closely the meaning of |
| 161 |
"application file format". See also the <a href="aff_short.html">short version</a> of this |
| 162 |
whitepaper. |
| 163 |
|
| 164 |
</p><h1 id="what_is_an_application_file_format_"><span>2. </span>What Is An Application File Format?</h1> |
| 165 |
|
| 166 |
<p> |
| 167 |
An "application file format" is the file format |
| 168 |
used to persist application state to disk or to exchange |
| 169 |
information between programs. |
| 170 |
There are thousands of application file formats in use today. |
| 171 |
Here are just a few examples: |
| 172 |
|
| 173 |
</p><ul> |
| 174 |
<li>DOC - Word Perfect and Microsoft Office documents |
| 175 |
</li><li>DXF - AutoCAD drawings |
| 176 |
</li><li>PDF - Portable Document Format from Adobe |
| 177 |
</li><li>XLS - Microsoft Excel Spreadsheet |
| 178 |
</li><li>GIT - Git source code repository |
| 179 |
</li><li>EPUB - The Electronic Publication format used by non-Kindle eBooks |
| 180 |
</li><li>ODT - The Open Document format used by OpenOffice and others |
| 181 |
</li><li>PPT - Microsoft PowerPoint presentations |
| 182 |
</li><li>ODP - The Open Document presentation format used by OpenOffice and others |
| 183 |
</li></ul> |
| 184 |
|
| 185 |
<p>We make a distinction between a "file format" and an "application format". |
| 186 |
A file format is used to store a single object. So, for example, a GIF or |
| 187 |
JPEG file stores a single image, and an XHTML file stores text, |
| 188 |
so those are "file formats" and not "application formats". An EPUB file, |
| 189 |
in contrast, stores both text and images (as contained XHTML and GIF/JPEG |
| 190 |
files) and so it is considered an "application format". This article is |
| 191 |
about "application formats". |
| 192 |
|
| 193 |
</p><p>The boundary between a file format and an application format is fuzzy. |
| 194 |
This article calls JPEG a file format, but for an image editor, JPEG |
| 195 |
might be considered the application format. Much depends on context. |
| 196 |
For this article, let us say that a file format stores a single object |
| 197 |
and an application format stores many different objects and their relationships |
| 198 |
to one another. |
| 199 |
|
| 200 |
</p><p>Most application formats fit into one of these three categories: |
| 201 |
|
| 202 |
</p><ol> |
| 203 |
<li><p><b>Fully Custom Formats.</b> |
| 204 |
Custom formats are specifically designed for a single application. |
| 205 |
DOC, DXF, PDF, XLS, and PPT are examples of custom formats. Custom |
| 206 |
formats are usually contained within a single file, for ease of transport. |
| 207 |
They are also usually binary, though the DXF format is a notable exception. |
| 208 |
Custom file formats require specialized application code |
| 209 |
to read and write and are not normally accessible from commonly |
| 210 |
available tools such as unix command-line programs and text editors. |
| 211 |
In other words, custom formats are usually "opaque blobs". |
| 212 |
To access the content of a custom application file format, one needs |
| 213 |
a tool specifically engineered to read and/or write that format. |
| 214 |
|
| 215 |
</p></li><li><p><b>Pile-of-Files Formats.</b> |
| 216 |
Sometimes the application state is stored as a hierarchy of |
| 217 |
files. Git is a prime example of this, though the phenomenon occurs |
| 218 |
frequently in one-off and bespoke applications. A pile-of-files format |
| 219 |
essentially uses the filesystem as a key/value database, storing small |
| 220 |
chunks of information into separate files. This gives the |
| 221 |
advantage of making the content more accessible to common utility |
| 222 |
programs such as text editors or "awk" or "grep". But even if many |
| 223 |
of the files in a pile-of-files format |
| 224 |
are easily readable, there are usually some files that have their |
| 225 |
own custom format (example: Git "Packfiles") and are hence |
| 226 |
"opaque blobs" that are not readable |
| 227 |
or writable without specialized tools. It is also much less convenient |
| 228 |
to move a pile-of-files from one place or machine to another, than |
| 229 |
it is to move a single file. And it is hard to make a pile-of-files |
| 230 |
document into an email attachment, for example. Finally, a pile-of-files |
| 231 |
format breaks the "document metaphor": |
| 232 |
there is no one file that a user can point to |
| 233 |
that is "the document". |
| 234 |
|
| 235 |
</p></li><li><p><b>Wrapped Pile-of-Files Formats.</b> |
| 236 |
Some applications use a Pile-of-Files that is then encapsulated into |
| 237 |
some kind of single-file container, usually a ZIP archive. |
| 238 |
EPUB, ODT,and ODP are examples of this approach. |
| 239 |
An EPUB book is really just a ZIP archive that contains various |
| 240 |
XHTML files for the text of book chapters, GIF and JPEG images for |
| 241 |
the artwork, and a specialized catalog file that tells the eBook |
| 242 |
reader how all the XML and image files fit together. OpenOffice |
| 243 |
documents (ODT and ODP) are also ZIP archives containing XML and |
| 244 |
images that represent their content as well as "catalog" files that |
| 245 |
show the interrelationships between the component parts. |
| 246 |
|
| 247 |
</p><p>A wrapped pile-of-files format is a compromise between a full |
| 248 |
custom file format and a pure pile-of-files format. |
| 249 |
A wrapped pile-of-files format is not an opaque blob in the same sense |
| 250 |
as a custom format, since the component parts can still be accessed |
| 251 |
using any common ZIP archiver, but the format is not quite as accessible |
| 252 |
as a pure pile-of-files format because one does still need the ZIP |
| 253 |
archiver, and one cannot normally use command-line tools like "find" |
| 254 |
on the file hierarchy without first un-zipping it. On the other |
| 255 |
hand, a wrapped pile-of-files format does preserve the document |
| 256 |
metaphor by putting all content into a single disk file. And |
| 257 |
because it is compressed, the wrapped pile-of-files format tends to |
| 258 |
be more compact. |
| 259 |
|
| 260 |
</p><p>As with custom file formats, and unlike pure pile-of-file formats, |
| 261 |
a wrapped pile-of-files format is not as easy to edit, since |
| 262 |
usually the entire file must be rewritten in order to change any |
| 263 |
component part. |
| 264 |
</p></li></ol> |
| 265 |
|
| 266 |
<p>The purpose of this document is to argue in favor of a fourth |
| 267 |
new category of application file format: An SQLite database file. |
| 268 |
|
| 269 |
</p><h1 id="sqlite_as_the_application_file_format"><span>3. </span>SQLite As The Application File Format</h1> |
| 270 |
|
| 271 |
<p> |
| 272 |
Any application state that can be recorded in a pile-of-files can |
| 273 |
also be recorded in an SQLite database with a simple key/value schema |
| 274 |
like this: |
| 275 |
|
| 276 |
</p><blockquote><pre> |
| 277 |
CREATE TABLE files(filename TEXT PRIMARY KEY, content BLOB); |
| 278 |
</pre></blockquote> |
| 279 |
|
| 280 |
<p> |
| 281 |
If the content is compressed, then such an <a href="sqlar.html">SQLite Archive</a> database is |
| 282 |
<a href="affcase1.html#smaller">the same size</a> (±1%) |
| 283 |
as an equivalent ZIP archive, and it has the advantage |
| 284 |
of being able to update individual "files" without rewriting |
| 285 |
the entire document. |
| 286 |
|
| 287 |
</p><p> |
| 288 |
But an SQLite database is not limited to a simple key/value structure |
| 289 |
like a pile-of-files database. An SQLite database can have dozens |
| 290 |
or hundreds or thousands of different tables, with dozens or |
| 291 |
hundreds or thousands of fields per table, each with different datatypes |
| 292 |
and constraints and particular meanings, all cross-referencing each other, |
| 293 |
appropriately and automatically indexed for rapid retrieval, |
| 294 |
and all stored efficiently and compactly in a single disk file. |
| 295 |
And all of this structure is succinctly documented for humans |
| 296 |
by the SQL schema. |
| 297 |
|
| 298 |
</p><p>In other words, an SQLite database can do everything that a |
| 299 |
pile-of-files or wrapped pile-of-files format can do, plus much more, |
| 300 |
and with greater lucidity. |
| 301 |
An SQLite database is a more versatile container than a key/value |
| 302 |
filesystem or a ZIP archive. (For a detailed example, see the |
| 303 |
<a href="affcase1.html">OpenOffice case study</a> essay.) |
| 304 |
|
| 305 |
</p><p>The power of an SQLite database could, in theory, be achieved using |
| 306 |
a custom file format. But any custom file format that is as expressive |
| 307 |
as a relational database would likely require an enormous design specification |
| 308 |
and many tens or hundreds of thousands of lines of code to |
| 309 |
implement. And the end result would be an "opaque blob" that is |
| 310 |
inaccessible without specialized tools. |
| 311 |
|
| 312 |
</p><p> |
| 313 |
Hence, in comparison to other approaches, the use of |
| 314 |
an SQLite database as an application file format has |
| 315 |
compelling advantages. Here are a few of these advantages, |
| 316 |
enumerated and expounded: |
| 317 |
</p> |
| 318 |
|
| 319 |
<ol> |
| 320 |
<li><p><b>Simplified Application Development.</b> |
| 321 |
No new code is needed for reading or writing the application file. |
| 322 |
One has merely to link against the SQLite library, or include the |
| 323 |
<a href="amalgamation.html">single "sqlite3.c" source file</a> with the rest of the |
| 324 |
application C code, and SQLite will take care of all of the application |
| 325 |
file I/O. This can reduce application code size by many thousands of |
| 326 |
lines, with corresponding saving in development and maintenance costs. |
| 327 |
|
| 328 |
</p><p>SQLite is one of the |
| 329 |
<a href="mostdeployed.html">most used</a> software libraries in the world. |
| 330 |
There are literally tens of billions of SQLite database files in use |
| 331 |
daily, on smartphones and gadgets and in desktop applications. |
| 332 |
SQLite is <a href="testing.html">carefully tested</a> and proven reliable. It is not |
| 333 |
a component that needs much tuning or debugging, allowing developers |
| 334 |
to stay focused on application logic. |
| 335 |
|
| 336 |
</p></li><li><p><b>Single-File Documents.</b> |
| 337 |
An SQLite database is contained in a single file, which is easily |
| 338 |
copied or moved or attached. The "document" metaphor is preserved. |
| 339 |
|
| 340 |
</p><p>SQLite does not have any file naming requirements |
| 341 |
and so the application can use any custom file suffix that it wants |
| 342 |
to help identify the file as "belonging" to the application. |
| 343 |
SQLite database files contain a 4-byte <a href="fileformat2.html#appid">Application ID</a> in |
| 344 |
their headers that can be set to an application-defined value |
| 345 |
and then used to identify the "type" of the document for utility |
| 346 |
programs such as <a href="http://linux.die.net/man/1/file">file(1)</a>, further |
| 347 |
enhancing the document metaphor. |
| 348 |
|
| 349 |
|
| 350 |
</p></li><li><p><b>High-Level Query Language.</b> |
| 351 |
SQLite is a complete relational database engine, which means that the |
| 352 |
application can access content using high-level queries. Application |
| 353 |
developers need not spend time thinking about "how" to retrieve the |
| 354 |
information they need from a document. Developers can write SQL that |
| 355 |
expresses "what" information they want and let the database engine |
| 356 |
figure out how to best retrieve that content. This helps developers |
| 357 |
operate "heads up" and remain focused on solving the user's problem, |
| 358 |
and avoid time spent "heads down" fiddling with low-level file |
| 359 |
formatting details. |
| 360 |
|
| 361 |
</p><p>A pile-of-files format can be viewed as a key/value database. |
| 362 |
A key/value database is better than no database at all. |
| 363 |
But without transactions or indices or a high-level query language or |
| 364 |
a proper schema, |
| 365 |
it is much harder and more error prone to use a key/value database than |
| 366 |
a relational database. |
| 367 |
|
| 368 |
</p></li><li><p><b>Accessible Content.</b> |
| 369 |
Information held in an SQLite database file is accessible using |
| 370 |
commonly available open-source command-line tools - tools that |
| 371 |
are installed by default on Mac and Linux systems and that are |
| 372 |
freely available as a self-contained EXE file on Windows. |
| 373 |
Unlike custom file formats, application-specific programs are |
| 374 |
not required to read or write content in an SQLite database. |
| 375 |
An SQLite database file is not an opaque blob. It is true |
| 376 |
that command-line tools such as text editors or "grep" or "awk" are |
| 377 |
not useful on an SQLite database, but the SQL query language is a much |
| 378 |
more powerful and convenient way for examining the content, so the |
| 379 |
inability to use "grep" and "awk" and the like is not seen as a loss. |
| 380 |
|
| 381 |
</p><p>An SQLite database is a <a href="fileformat2.html">well-defined and well-documented</a> |
| 382 |
file format that is in widespread use by literally millions of applications |
| 383 |
and is backwards compatible to its inception in 2004 and which promises |
| 384 |
to continue to be compatible in decades to come. The longevity of |
| 385 |
SQLite database files is particularly important to bespoke applications, |
| 386 |
since it allows the document content to be accessed far in the |
| 387 |
future, long after all traces of the original application have been lost. |
| 388 |
Data lives longer than code. |
| 389 |
SQLite databases are <a href="locrsf.html">recommended by the US Library of Congress</a> |
| 390 |
as a storage format for long-term preservation of digital content. |
| 391 |
|
| 392 |
|
| 393 |
</p></li><li><p><b>Cross-Platform.</b> |
| 394 |
SQLite database files are portable between 32-bit and 64-bit machines and |
| 395 |
between big-endian and little-endian architectures and between any of the |
| 396 |
various flavors of Windows and Unix-like operating systems. |
| 397 |
The application using an SQLite application file format can store |
| 398 |
binary numeric data without having to worry about the byte-order of |
| 399 |
integers or floating point numbers. |
| 400 |
Text content can be read or written as UTF-8, UTF-16LE, or UTF-16BE and |
| 401 |
SQLite will automatically perform any necessary translations on-the-fly. |
| 402 |
|
| 403 |
</p></li><li><p><b>Atomic Transactions.</b> |
| 404 |
Writes to an SQLite database are <a href="atomiccommit.html">atomic</a>. |
| 405 |
They either happen completely |
| 406 |
or not at all, even during system crashes or power failures. So |
| 407 |
there is no danger of corrupting a document just because the power happened |
| 408 |
to go out at the same instant that a change was being written to disk. |
| 409 |
|
| 410 |
</p><p>SQLite is transactional, meaning that multiple changes can be grouped |
| 411 |
together such that either all or none of them occur, and so that the |
| 412 |
changes can be rolled back if a problem is found prior to commit. |
| 413 |
This allows an application to make a change incrementally, then run |
| 414 |
various sanity and consistency checks on the resulting data prior to |
| 415 |
committing the changes to disk. The |
| 416 |
<a href="http://www.fossil-scm.org/">Fossil</a> DVCS |
| 417 |
<a href="http://www.fossil-scm.org/fossil/doc/tip/www/selfcheck.wiki">uses this technique</a> |
| 418 |
to verify that no repository history has been lost prior to each change. |
| 419 |
|
| 420 |
</p></li><li><p><b>Incremental And Continuous Updates.</b> |
| 421 |
When writing to an SQLite database file, only those parts of the file that |
| 422 |
actually change are written out to disk. This makes the writing happen faster |
| 423 |
and saves wear on SSDs. This is an enormous advantage over custom |
| 424 |
and wrapped pile-of-files formats, both of which usually require a |
| 425 |
rewrite of the entire document in order to change a single byte. |
| 426 |
Pure pile-of-files formats can also |
| 427 |
do incremental updates to some extent, though the granularity of writes is |
| 428 |
usually larger with pile-of-file formats (a single file) than with SQLite |
| 429 |
(a single page). |
| 430 |
|
| 431 |
</p><p>SQLite also supports continuous update. |
| 432 |
Instead of collecting changes in memory and then writing |
| 433 |
them to disk only on a File/Save action, changes can be written back to |
| 434 |
the disk as they occur. This avoids loss of work on a system crash or |
| 435 |
power failure. An <a href="undoredo.html">automated undo/redo stack</a>, managed using triggers, |
| 436 |
can be kept in the on-disk database, meaning that undo/redo can occur |
| 437 |
across session boundaries. |
| 438 |
|
| 439 |
</p></li><li><p><b>Easily Extensible.</b> |
| 440 |
As an application grows, new features can be added to an |
| 441 |
SQLite application file format simply by adding new tables to the schema |
| 442 |
or by adding new columns to existing tables. Adding columns or tables |
| 443 |
does not change the meaning of prior queries, so with a |
| 444 |
modicum of care to ensuring that the meaning of legacy columns and |
| 445 |
tables are preserved, backwards compatibility is maintained. |
| 446 |
|
| 447 |
</p><p>It is possible to extend custom or pile-of-files formats too, of course, |
| 448 |
but doing so is often much harder. If indices are added, then all application |
| 449 |
code that changes the corresponding tables must be located and modified to |
| 450 |
keep those indices up-to-date. If columns are added, then all application |
| 451 |
code that accesses the corresponding table must be located and modified to |
| 452 |
take into account the new columns. |
| 453 |
|
| 454 |
</p></li><li><p><b>Performance.</b> |
| 455 |
In many cases, an SQLite application file format will be |
| 456 |
<a href="fasterthanfs.html">faster than a pile-of-files format</a> or |
| 457 |
a custom format. In addition to being faster for raw read and |
| 458 |
writes, SQLite can often dramatically improves start-up times because |
| 459 |
instead of having to |
| 460 |
read and parse the entire document into memory, the application can |
| 461 |
do queries to extract only the information needed for the initial screen. |
| 462 |
As the application progresses, it only needs to load as much material as |
| 463 |
is needed to draw the next screen, and can discard information from |
| 464 |
prior screens that is no longer in use. This helps keep the memory |
| 465 |
footprint of the application under control. |
| 466 |
|
| 467 |
</p><p>A pile-of-files format can be read incrementally just like SQLite. |
| 468 |
But many developers are surprised to learn that SQLite can read and |
| 469 |
write smaller BLOBs (less than about 100KB in size) from its database |
| 470 |
faster than those same blobs can be read or written as separate files |
| 471 |
from the filesystem. (See |
| 472 |
<a href="fasterthanfs.html">35% Faster Than The Filesystem</a> and |
| 473 |
<a href="intern-v-extern-blob.html">Internal Versus External BLOBs</a> for further information.) |
| 474 |
There is overhead associated with operating a relational |
| 475 |
database engine, however one should not assume that direct file I/O |
| 476 |
is faster than SQLite database I/O, as often it is not. |
| 477 |
|
| 478 |
</p><p>In either case, if performance problems do arise in an SQLite application |
| 479 |
those problems can often be resolved by adding one or two <a href="lang_createindex.html">CREATE INDEX</a> |
| 480 |
statements to the schema or perhaps running <a href="lang_analyze.html">ANALYZE</a> one time |
| 481 |
and without having to touch a single line of |
| 482 |
application code. But if a performance problem comes up in a custom or |
| 483 |
pile-of-files format, the fix will often require extensive changes |
| 484 |
to application code to add and maintain new indices or to extract |
| 485 |
information using different algorithms. |
| 486 |
|
| 487 |
</p></li><li><p><b>Concurrent Use By Multiple Processes.</b> |
| 488 |
SQLite automatically coordinates concurrent access to the same |
| 489 |
document from multiple threads and/or processes. Two or more |
| 490 |
applications can connect and read from the same document at the |
| 491 |
same time. Writes are serialized, but as writes normally only |
| 492 |
take milliseconds, applications simply take turns writing. |
| 493 |
SQLite automatically ensures that the low-level format of the |
| 494 |
document is uncorrupted. Accomplishing the same with a custom |
| 495 |
or pile-of-files format, in contrast, requires extensive support |
| 496 |
in the application. And the application logic needed to support |
| 497 |
concurrency is a notorious bug-magnet. |
| 498 |
|
| 499 |
</p></li><li><p><b>Multiple Programming Languages.</b> |
| 500 |
Though SQLite is itself written in ANSI-C, interfaces exist for |
| 501 |
just about every other programming language you can think of: |
| 502 |
C++, C#, Objective-C, Java, Tcl, Perl, Python, Ruby, Erlang, |
| 503 |
JavaScript, and so forth. So programmers can develop in whatever |
| 504 |
language they are most comfortable with and which best matches |
| 505 |
the needs of the project. |
| 506 |
|
| 507 |
</p><p>An SQLite application file format is a great |
| 508 |
choice in cases where there is a collection or "federation" of |
| 509 |
separate programs, often written in different languages and by |
| 510 |
different development teams. |
| 511 |
This comes up commonly in research or laboratory |
| 512 |
environments where one team is responsible for data acquisition |
| 513 |
and other teams are responsible for various stages of analysis. |
| 514 |
Each team can use whatever hardware, operating system, |
| 515 |
programming language and development methodology that they are |
| 516 |
most comfortable with, and as long as all programs use an SQLite |
| 517 |
database with a common schema, they can all interoperate. |
| 518 |
|
| 519 |
|
| 520 |
</p></li><li><p><b>Better Applications.</b> |
| 521 |
If the application file format is an SQLite database, the complete |
| 522 |
documentation for that file format consists of the database schema, |
| 523 |
with perhaps a few extra words about what each table and column |
| 524 |
represents. The description of a custom file format, |
| 525 |
on the other hand, typically runs on for hundreds of |
| 526 |
pages. A pile-of-files format, while much simpler and easier to |
| 527 |
describe than a fully custom format, still tends to be much larger |
| 528 |
and more complex than an SQL schema dump, since the names and format |
| 529 |
for the individual files must still be described. |
| 530 |
|
| 531 |
</p><p>This is not a trivial point. A clear, concise, and easy to understand |
| 532 |
file format is a crucial part of any application design. |
| 533 |
Fred Brooks, in his all-time best-selling computer science text, |
| 534 |
<i>The Mythical Man-Month</i> says: |
| 535 |
</p><blockquote><i>Representation is the |
| 536 |
essence of computer programming.<br /="1">...<br /="1"> |
| 537 |
Show me your flowcharts and conceal your tables, and I shall |
| 538 |
continue to be mystified. Show me your tables, and I won't usually |
| 539 |
need your flowcharts; they'll be obvious.</i></blockquote> |
| 540 |
<p>Rob Pike, in his |
| 541 |
<i>Rules of Programming</i> expresses the same idea this way: |
| 542 |
</p><blockquote> |
| 543 |
<i>Data dominates. If you've chosen the right data structures |
| 544 |
and organized things well, the algorithms will almost always |
| 545 |
be self-evident. Data structures, not algorithms, are central |
| 546 |
to programming.</i></blockquote> |
| 547 |
<p> Linus Torvalds used different words to say |
| 548 |
much the same thing on the Git mailing list on 2006-06-27: |
| 549 |
</p><blockquote> |
| 550 |
<i>Bad programmers worry about the code. Good programmers worry |
| 551 |
about data structures and their relationships.</i> |
| 552 |
</blockquote> |
| 553 |
|
| 554 |
<p>The point is this: an SQL database schema almost always does |
| 555 |
a far better job of defining and organizing the tables and |
| 556 |
data structures and their relationships. |
| 557 |
And having clear, concise, and well-defined representation |
| 558 |
almost always results in an application that performs better, |
| 559 |
has fewer problems, and is easier to develop and maintain. |
| 560 |
</p></li></ol> |
| 561 |
|
| 562 |
<h1 id="security_considerations"><span>4. </span>Security Considerations</h1> |
| 563 |
|
| 564 |
<p>SQLite is robust against maliciously malformed database files and |
| 565 |
SQL inputs. An attacker will not be able to provoke a memory error |
| 566 |
by corrupting an SQLite database used as an application file. |
| 567 |
|
| 568 |
</p><p>There are attacks that a clever attacker can carry out against |
| 569 |
an application by tricking a user to open an application file that |
| 570 |
is an SQLite database. However, a few simple precautions can prevent |
| 571 |
any serious infiltration or damage. If you do choose to use SQLite |
| 572 |
(or any relational database for that matter) as an application file |
| 573 |
format, please review the |
| 574 |
<a href="security.html">defense against dark arts</a> document, and especially the section on |
| 575 |
<a href="security.html#baddb">untrusted database files</a> to make sure you have prudent defensive |
| 576 |
measures in place and have not accidently opened any security holes. |
| 577 |
</p> |
| 578 |
|
| 579 |
<h1 id="conclusion"><span>5. </span>Conclusion</h1> |
| 580 |
|
| 581 |
<p> |
| 582 |
SQLite is not the perfect application file format for every situation. |
| 583 |
But in many cases, SQLite is a far better choice than either a custom |
| 584 |
file format, a pile-of-files, or a wrapped pile-of-files. |
| 585 |
SQLite is a high-level, stable, reliable, cross-platform, widely-deployed, |
| 586 |
extensible, performant, accessible, concurrent file format. It deserves |
| 587 |
your consideration as the standard file format on your next application |
| 588 |
design. |
| 589 |
</p> |
| 590 |
|