Debian Codegraph @

www/inmemorydb.html

raw
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>In-Memory Databases</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
119
120
121 <h1 align="center">In-Memory Databases</h1>
122
123 <p>An SQLite database is normally stored in a single ordinary disk
124 file. However, in certain circumstances, the database might be stored in
125 memory.</p>
126
127 <p>The most common way to force an SQLite database to exist purely
128 in memory is to open the database using the special filename
129 "<b>:memory:</b>". In other words, instead of passing the name of
130 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
131 <a href="c3ref/open.html">sqlite3_open_v2()</a> functions, pass in the string ":memory:". For
132 example:</p>
133
134 <blockquote><pre>
135 rc = sqlite3_open(":memory:", &amp;db);
136 </pre></blockquote>
137
138 <p>When this is done, no disk file is opened.
139 Instead, a new database is created
140 purely in memory. The database ceases to exist as soon as the database
141 connection is closed. Every :memory: database is distinct from every
142 other. So, opening two database connections each with the filename
143 ":memory:" will create two independent in-memory databases.</p>
144
145 <p>The special filename ":memory:" can be used anywhere that a database
146 filename is permitted. For example, it can be used as the
147 <i>filename</i> in an <a href="lang_attach.html">ATTACH</a> command:</p>
148
149 <blockquote><pre>
150 ATTACH DATABASE ':memory:' AS aux1;
151 </pre></blockquote>
152
153 <p>Note that in order for the special ":memory:" name to apply and to
154 create a pure in-memory database, there must be no additional text in the
155 filename. Thus, a disk-based database can be created in a file by prepending
156 a pathname, like this: "./:memory:".</p>
157
158 <p>The special ":memory:" filename also works when using <a href="uri.html">URI filenames</a>.
159 For example:
160
161 <blockquote><pre>
162 rc = sqlite3_open("file::memory:", &amp;db);
163 </pre></blockquote>
164
165 Or,
166
167 <blockquote><pre>
168 ATTACH DATABASE 'file::memory:' AS aux1;
169 </pre></blockquote>
170
171 <a name="sharedmemdb"></a>
172
173 <h2>In-memory Databases And Shared Cache</h2>
174
175 <p>In-memory databases are allowed to use <a href="sharedcache.html">shared cache</a> if they are
176 opened using a <a href="uri.html">URI filename</a>. If the unadorned ":memory:" name is used
177 to specify the in-memory database, then that database always has a private
178 cache and is only visible to the database connection that originally
179 opened it. However, the same in-memory database can be opened by two or
180 more database connections as follows:
181
182 <blockquote><pre>
183 rc = sqlite3_open("file::memory:?cache=shared", &amp;db);
184 </pre></blockquote>
185
186 Or,
187
188 <blockquote><pre>
189 ATTACH DATABASE 'file::memory:?cache=shared' AS aux1;
190 </pre></blockquote>
191
192 <p>This allows separate database connections to share the same
193 in-memory database. Of course, all database connections sharing the
194 in-memory database need to be in the same process. The database is
195 automatically deleted and memory is reclaimed when the last connection
196 to the database closes.
197
198 <p>If two or more distinct but shareable in-memory databases are needed
199 in a single process, then the <a href="uri.html#coreqp">mode=memory</a> query parameter can
200 be used with a <a href="uri.html">URI filename</a> to create a named in-memory database:
201
202 <blockquote><pre>
203 rc = sqlite3_open("file:memdb1?mode=memory&amp;cache=shared", &amp;db);
204 </pre></blockquote>
205
206 Or,
207
208 <blockquote><pre>
209 ATTACH DATABASE 'file:memdb1?mode=memory&amp;cache=shared' AS aux1;
210 </pre></blockquote>
211
212 <p>When an in-memory database is named in this way, it will only share its
213 cache with another connection that uses exactly the same name.
214
215
216
217 <a name="temp_db"></a>
218
219 <h2>Temporary Databases</h2>
220
221 <p>When the name of the database file handed to <a href="c3ref/open.html">sqlite3_open()</a> or to
222 <a href="lang_attach.html">ATTACH</a> is an empty string, then a new temporary file is created to hold
223 the database.</p>
224
225 <blockquote><pre>
226 rc = sqlite3_open("", &amp;db);
227 </pre></blockquote>
228
229 <blockquote><pre>
230 ATTACH DATABASE '' AS aux2;
231 </pre></blockquote>
232
233 <p>A different temporary file is created each time so that, just as
234 with the special ":memory:" string, two database connections to temporary
235 databases each have their own private database. Temporary databases are
236 automatically deleted when the connection that created them closes.</p>
237
238 <p>Even though a disk file is allocated for each temporary database, in
239 practice the temporary database usually resides in the in-memory pager cache
240 and hence there is very little difference between a pure in-memory database
241 created by ":memory:" and a temporary database created by an empty filename.
242 The sole difference is that a ":memory:" database must remain in memory
243 at all times whereas parts of a temporary database might be flushed to disk
244 if the database becomes large or if SQLite comes under memory pressure.</p>
245
246 <p>The previous paragraphs describe the behavior of temporary databases
247 under the default SQLite configuration. An application can use the
248 <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
249 force temporary databases to behave as pure in-memory databases, if desired.
250 </p>
251 <p align="center"><small><i>This page was last updated on 2022-04-18 02:55:50Z </small></i></p>
252