1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
3
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
4
<title>In-Memory Databases</title>
5
<style type="text/css">
8
font-family: Verdana, sans-serif;
13
a:visited { color: #734559 }
15
.logo { position:absolute; margin:3px; }
31
.toolbar a { color: white; text-decoration: none; padding: 6px 12px; }
32
.toolbar a:visited { color: white; }
33
.toolbar a:hover { color: #044a64; background: white; }
35
.content { margin: 5%; }
36
.content dt { font-weight:bold; }
37
.content dd { margin-bottom: 25px; margin-left:20%; }
38
.content ul { padding:0px; padding-left: 15px; margin:0px; }
41
.se { background: url(images/se.gif) 100% 100% no-repeat #044a64}
42
.sw { background: url(images/sw.gif) 0% 100% no-repeat }
43
.ne { background: url(images/ne.gif) 100% 0% no-repeat }
44
.nw { background: url(images/nw.gif) 0% 0% no-repeat }
46
/* Things for "fancyformat" documents start here. */
47
.fancy img+p {font-style:italic}
48
.fancy .codeblock i { color: darkblue; }
49
.fancy h1,.fancy h2,.fancy h3,.fancy h4 {font-weight:normal;color:#044a64}
50
.fancy h2 { margin-left: 10px }
51
.fancy h3 { margin-left: 20px }
52
.fancy h4 { margin-left: 30px }
53
.fancy th {white-space:nowrap;text-align:left;border-bottom:solid 1px #444}
54
.fancy th, .fancy td {padding: 0.2em 1ex; vertical-align:top}
55
.fancy #toc a { color: darkblue ; text-decoration: none }
56
.fancy .todo { color: #AA3333 ; font-style : italic }
57
.fancy .todo:before { content: 'TODO:' }
58
.fancy p.todo { border: solid #AA3333 1px; padding: 1ex }
59
.fancy img { display:block; }
60
.fancy :link:hover, .fancy :visited:hover { background: wheat }
61
.fancy p,.fancy ul,.fancy ol { margin: 1em 5ex }
62
.fancy li p { margin: 1em 0 }
63
/* End of "fancyformat" specific rules. */
69
<div><!-- container div to satisfy validator -->
72
<img class="logo" src="images/sqlite370_banner.gif" alt="SQLite Logo"
74
<div><!-- IE hack to prevent disappearing logo--></div>
75
<div class="tagline">Small. Fast. Reliable.<br>Choose any three.</div>
77
<table width=100% style="clear:both"><tr><td>
78
<div class="se"><div class="sw"><div class="ne"><div class="nw">
79
<table width=100% style="padding:0;margin:0;cell-spacing:0"><tr>
82
<a href="about.html">About</a>
83
<a href="sitemap.html">Sitemap</a>
84
<a href="docs.html">Documentation</a>
85
<a href="download.html">Download</a>
86
<a href="copyright.html">License</a>
87
<a href="news.html">News</a>
88
<a href="support.html">Support</a>
91
gMsg = "Search SQLite Docs..."
92
function entersearch() {
93
var q = document.getElementById("q");
94
if( q.value == gMsg ) { q.value = "" }
95
q.style.color = "black"
96
q.style.fontStyle = "normal"
98
function leavesearch() {
99
var q = document.getElementById("q");
100
if( q.value == "" ) {
102
q.style.color = "#044a64"
103
q.style.fontStyle = "italic"
108
<div style="padding:0 1em 0px 0;white-space:nowrap">
109
<form name=f method="GET" action="http://www.sqlite.org/search">
110
<input id=q name=q type=text
111
onfocus="entersearch()" onblur="leavesearch()" style="width:24ex;padding:1px 1ex; border:solid white 1px; font-size:0.9em ; font-style:italic;color:#044a64;" value="Search SQLite Docs...">
112
<input type=submit value="Go" style="border:solid white 1px;background-color:#044a64;color:white;font-size:0.9em;padding:0 1ex">
116
</div></div></div></div>
118
<div class=startsearch></div>
123
<h1 align="center">In-Memory Databases</h1>
125
<p>An SQLite database is normally stored in a single ordinary disk
126
file. However, in certain circumstances, the database might be stored in
129
<p>The most common way to force an SQLite database to exist purely
130
in memory is to open the database using the special filename
131
"<b>:memory:</b>". In other words, instead of passing the name of
132
a real disk file into one of the <a href="c3ref/open.html">sqlite3_open()</a>, <a href="c3ref/open.html">sqlite3_open16()</a>, or
133
<a href="c3ref/open.html">sqlite3_open_v2()</a> functions, pass in the string ":memory:". For
137
rc = sqlite3_open(":memory:", &db);
140
<p>When this is done, no disk file is opened.
141
Instead, a new database is created
142
purely in memory. The database ceases to exist as soon as the database
143
connection is closed. Every :memory: database is distinct from every
144
other. So, opening two database connections each with the filename
145
":memory:" will create two independent in-memory databases.</p>
147
<p>The special filename ":memory:" can be used anywhere that a database
148
filename is permitted. For example, it can be used as the
149
<i>filename</i> in an <a href="lang_attach.html">ATTACH</a> command:</p>
152
ATTACH DATABASE ':memory:' AS aux1;
155
<p>Note that in order for the special ":memory:" name to apply and to
156
create a pure in-memory database, there must be no additional text in the
157
filename. Thus, a disk-based database can be created in a file by prepending
158
a pathname, like this: "./:memory:".</p>
160
<p>The special ":memory:" filename also works when using <a href="uri.html">URI filenames</a>.
164
rc = sqlite3_open("file::memory:", &db);
170
ATTACH DATABASE 'file::memory:' AS aux1;
173
<a name="sharedmemdb"></a>
175
<h2>In-memory Databases And Shared Cache</h2>
177
<p>In-memory databases are allowed to use <a href="sharedcache.html">shared cache</a> if they are
178
opened using a <a href="uri.html">URI filename</a>. If the unadorned ":memory:" name is used
179
to specify the in-memory database, then that database always has a private
180
cache and is this only visible to the database connection that originally
181
opened it. However, the same in-memory database can be opened by two or
182
more database connections as follows:
185
rc = sqlite3_open("file::memory:?cache=shared", &db);
191
ATTACH DATABASE 'file::memory:?cache=shared' AS aux1;
194
<p>This allows separate database connections to share the same
195
in-memory database. Of course, all database connections sharing the
196
in-memory database need to be in the same process. The database is
197
automatically deleted and memory is reclaimed when the last connection
198
to the database closes.
200
<p>If two or more distinct but shareable in-memory databases are needed
201
in a single process, then the <a href="uri.html#coreqp">mode=memory</a> query parameter can
202
be used with a <a href="uri.html">URI filename</a> to create a named in-memory database:
205
rc = sqlite3_open("file:memdb1?mode=memory&cache=shared", &db);
211
ATTACH DATABASE 'file:memdb1?mode=memory&cache=shared' AS aux1;
214
<p>When an in-memory database is named in this way, it will only share its
215
cache with another connection that uses exactly the same name.
219
<a name="temp_db"></a>
221
<h2>Temporary Databases</h2>
223
<p>When the name of the database file handed to <a href="c3ref/open.html">sqlite3_open()</a> or to
224
<a href="lang_attach.html">ATTACH</a> is an empty string, then a new temporary file is created to hold
228
rc = sqlite3_open("", &db);
232
ATTACH DATABASE '' AS aux2;
235
<p>A different temporary file is created each time, so that just like as
236
with the special ":memory:" string, two database connections to temporary
237
databases each have their own private database. Temporary databases are
238
automatically deleted when the connection that created them closes.</p>
240
<p>Even though a disk file is allocated for each temporary database, in
241
practice the temporary database usually resides in the in-memory pager
242
cache and hence is very little difference between a pure in-memory database
243
created by ":memory:" and a temporary database created by an empty filename.
244
The sole difference is that a ":memory:" database must remain in memory
245
at all times whereas parts of a temporary database might be flushed to
246
disk if database becomes large or if SQLite comes under memory pressure.</p>
248
<p>The previous paragraphs describe the behavior of temporary databases
249
under the default SQLite configuration. An application can use the
250
<a href="pragma.html#pragma_temp_store">temp_store pragma</a> and the <a href="compile.html#temp_store">SQLITE_TEMP_STORE</a> compile-time parameter to
251
force temporary databases to behave as pure in-memory databases, if desired.