~ubuntu-branches/ubuntu/wily/sqlite3/wily

« back to all changes in this revision

Viewing changes to inmemorydb.html

  • Committer: Package Import Robot
  • Author(s): Laszlo Boszormenyi (GCS)
  • Date: 2012-06-13 21:43:48 UTC
  • mto: This revision was merged to the branch mainline in revision 23.
  • Revision ID: package-import@ubuntu.com-20120613214348-uy14uupdeq0hh04k
Tags: upstream-3.7.13/www
Import upstream version 3.7.13, component www

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
<!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
 
2
<html><head>
 
3
<meta http-equiv="content-type" content="text/html; charset=UTF-8">
 
4
<title>In-Memory Databases</title>
 
5
<style type="text/css">
 
6
body {
 
7
    margin: auto;
 
8
    font-family: Verdana, sans-serif;
 
9
    padding: 8px 1%;
 
10
}
 
11
 
 
12
a { color: #044a64 }
 
13
a:visited { color: #734559 }
 
14
 
 
15
.logo { position:absolute; margin:3px; }
 
16
.tagline {
 
17
  float:right;
 
18
  text-align:right;
 
19
  font-style:italic;
 
20
  width:300px;
 
21
  margin:12px;
 
22
  margin-top:58px;
 
23
}
 
24
 
 
25
.toolbar {
 
26
  text-align: center;
 
27
  line-height: 1.6em;
 
28
  margin: 0;
 
29
  padding: 0px 8px;
 
30
}
 
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; }
 
34
 
 
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; }
 
39
 
 
40
/* rounded corners */
 
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 }
 
45
 
 
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. */
 
64
 
 
65
</style>
 
66
  
 
67
</head>
 
68
<body>
 
69
<div><!-- container div to satisfy validator -->
 
70
 
 
71
<a href="index.html">
 
72
<img class="logo" src="images/sqlite370_banner.gif" alt="SQLite Logo"
 
73
 border="0"></a>
 
74
<div><!-- IE hack to prevent disappearing logo--></div>
 
75
<div class="tagline">Small. Fast. Reliable.<br>Choose any three.</div>
 
76
 
 
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>
 
80
  <td width=100%>
 
81
  <div class="toolbar">
 
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>
 
89
  </div>
 
90
<script>
 
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"
 
97
  }
 
98
  function leavesearch() {
 
99
    var q = document.getElementById("q");
 
100
    if( q.value == "" ) { 
 
101
      q.value = gMsg
 
102
      q.style.color = "#044a64"
 
103
      q.style.fontStyle = "italic"
 
104
    }
 
105
  }
 
106
</script>
 
107
<td>
 
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">
 
113
    </form>
 
114
    </div>
 
115
  </table>
 
116
</div></div></div></div>
 
117
</td></tr></table>
 
118
<div class=startsearch></div>
 
119
  
 
120
 
 
121
 
 
122
 
 
123
<h1 align="center">In-Memory Databases</h1>
 
124
 
 
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
 
127
memory.</p>
 
128
 
 
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
 
134
example:</p>
 
135
 
 
136
<blockquote><pre>
 
137
rc = sqlite3_open(":memory:", &amp;db);
 
138
</pre></blockquote>
 
139
 
 
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>
 
146
 
 
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>
 
150
 
 
151
<blockquote><pre>
 
152
ATTACH DATABASE ':memory:' AS aux1;
 
153
</pre></blockquote>
 
154
 
 
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>
 
159
 
 
160
<p>The special ":memory:" filename also works when using <a href="uri.html">URI filenames</a>.
 
161
For example:
 
162
 
 
163
<blockquote><pre>
 
164
rc = sqlite3_open("file::memory:", &amp;db);
 
165
</pre></blockquote>
 
166
 
 
167
Or,
 
168
 
 
169
<blockquote><pre>
 
170
ATTACH DATABASE 'file::memory:' AS aux1;
 
171
</pre></blockquote>
 
172
 
 
173
<a name="sharedmemdb"></a>
 
174
 
 
175
<h2>In-memory Databases And Shared Cache</h2>
 
176
 
 
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:
 
183
 
 
184
<blockquote><pre>
 
185
rc = sqlite3_open("file::memory:?cache=shared", &amp;db);
 
186
</pre></blockquote>
 
187
 
 
188
Or,
 
189
 
 
190
<blockquote><pre>
 
191
ATTACH DATABASE 'file::memory:?cache=shared' AS aux1;
 
192
</pre></blockquote>
 
193
 
 
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.
 
199
 
 
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:
 
203
 
 
204
<blockquote><pre>
 
205
rc = sqlite3_open("file:memdb1?mode=memory&amp;cache=shared", &amp;db);
 
206
</pre></blockquote>
 
207
 
 
208
Or,
 
209
 
 
210
<blockquote><pre>
 
211
ATTACH DATABASE 'file:memdb1?mode=memory&amp;cache=shared' AS aux1;
 
212
</pre></blockquote>
 
213
 
 
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.
 
216
 
 
217
 
 
218
 
 
219
<a name="temp_db"></a>
 
220
 
 
221
<h2>Temporary Databases</h2>
 
222
 
 
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
 
225
the database.</p>
 
226
 
 
227
<blockquote><pre>
 
228
rc = sqlite3_open("", &amp;db);
 
229
</pre></blockquote>
 
230
 
 
231
<blockquote><pre>
 
232
ATTACH DATABASE '' AS aux2;
 
233
</pre></blockquote>
 
234
 
 
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>
 
239
 
 
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>
 
247
 
 
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.
 
252
</p>
 
253