2
0

inmemorydb.html 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122
  1. <!DOCTYPE HTML PUBLIC "-//W3C//DTD HTML 4.01//EN" "http://www.w3.org/TR/html4/strict.dtd">
  2. <html><head>
  3. <title>In-Memory Databases</title>
  4. <style type="text/css">
  5. body {
  6. margin: auto;
  7. font-family: Verdana, sans-serif;
  8. padding: 8px 1%;
  9. }
  10. a { color: #45735f }
  11. a:visited { color: #734559 }
  12. .logo { position:absolute; margin:3px; }
  13. .tagline {
  14. float:right;
  15. text-align:right;
  16. font-style:italic;
  17. width:240px;
  18. margin:12px;
  19. margin-top:58px;
  20. }
  21. .toolbar {
  22. font-variant: small-caps;
  23. text-align: center;
  24. line-height: 1.6em;
  25. margin: 0;
  26. padding:1px 8px;
  27. }
  28. .toolbar a { color: white; text-decoration: none; padding: 6px 12px; }
  29. .toolbar a:visited { color: white; }
  30. .toolbar a:hover { color: #80a796; background: white; }
  31. .content { margin: 5%; }
  32. .content dt { font-weight:bold; }
  33. .content dd { margin-bottom: 25px; margin-left:20%; }
  34. .content ul { padding:0px; padding-left: 15px; margin:0px; }
  35. /* rounded corners */
  36. .se { background: url(images/se.png) 100% 100% no-repeat #80a796}
  37. .sw { background: url(images/sw.png) 0% 100% no-repeat }
  38. .ne { background: url(images/ne.png) 100% 0% no-repeat }
  39. .nw { background: url(images/nw.png) 0% 0% no-repeat }
  40. </style>
  41. <meta http-equiv="content-type" content="text/html; charset=UTF-8">
  42. </head>
  43. <body>
  44. <div><!-- container div to satisfy validator -->
  45. <h1 align="center">In-Memory Databases</h1>
  46. <p>An SQLite database is normally stored in a single ordinary disk
  47. file. However, in certain circumstances, the database might be stored in
  48. memory.</p>
  49. <p>The most common way to force an SQLite database to exist purely
  50. in memory is to open the database using the special filename
  51. "<b>:memory:</b>". In other words, instead of passing the name of
  52. a real disk file into the LoadDatase function, pass in the string ":memory:". For
  53. example:</p>
  54. <blockquote><pre>
  55. rc = sqlite3_open(":memory:", &db);
  56. </pre></blockquote>
  57. <p>When this is done, no disk file is opened.
  58. Instead, a new database is created
  59. purely in memory. The database ceases to exist as soon as the database
  60. connection is closed. Every :memory: database is distinct from every
  61. other. So, opening two database connections each with the filename
  62. ":memory:" will create two independent in-memory databases.</p>
  63. <p>The special filename ":memory:" can be used anywhere that a database
  64. filename is permitted. For example, it can be used as the
  65. <i>filename</i> in an <a href="lang_attach.html">ATTACH</a> command:</p>
  66. <blockquote>
  67. <b>ATTACH DATABASE ':memory:' AS aux1;</b>
  68. </blockquote>
  69. <p>Note that in order for the special ":memory:" name to apply and to
  70. create a pure in-memory database, there must be no additional text in the
  71. filename. Thus, a disk-based database can be created in a file by prepending
  72. a pathname, like this: "./:memory:".</p>
  73. <a name="temp_db"></a>
  74. <h2>Temporary Databases</h2>
  75. <p>When the name of the database file handed to LoadDatabase() or to
  76. <a href="lang_attach.html">ATTACH</a> is an empty string, then a new temporary file is created to hold
  77. the database.</p>
  78. <blockquote><pre>
  79. rc = sqlite3_open("", &db);
  80. </pre></blockquote>
  81. <blockquote><b>
  82. ATTACH DATABASE '' AS aux2;
  83. </b></blockquote>
  84. <p>A different temporary file is created each time, so that just like as
  85. with the special ":memory:" string, two database connections to temporary
  86. databases each have their own private database. Temporary databases are
  87. automatically deleted when the connection that created them closes.</p>
  88. <p>Even though a disk file is allocated for each temporary database, in
  89. practice the temporary database usually resides in the in-memory pager
  90. cache and hence is very little difference between a pure in-memory database
  91. created by ":memory:" and a temporary database created by an empty filename.
  92. The sole difference is that a ":memory:" database must remain in memory
  93. at all times whereas parts of a temporary database might be flushed to
  94. disk if database becomes large or if SQLite comes under memory pressure.</p>
  95. <p>The previous paragraphs describe the behavior of temporary databases
  96. under the default SQLite configuration.
  97. </p>