GV

GeezerVison

The World Wide Web, restored to 800 pixels of glory.

Home|About|Gaming cabinet|Archives|How it works|Starter sites

On this site

  • Sign in
  • ActionsCI/CD and automation
  • CodespacesCloud development environments
  • All Features
  • Documentation
  • Training
  • Certifications
  • Build with Copilot
  • Customize Copilot
  • Integrations
  • Trending repos
  • GitHub Sponsors
  • Developer articles
  • Enterprises

This view

  • Original page
  • New address

GeezerVison is showing github.com — til/sqlite/python-sqlite-memory-to-file.md at main · simonw/til

Expand file tree

/

python-sqlite-memory-to-file.md

Copy path
More file actions
More file actions

Latest commit

 

History

History
History
37 lines (24 loc) · 976 Bytes
/

python-sqlite-memory-to-file.md

Copy path

File metadata and controls

37 lines (24 loc) · 976 Bytes
Raw
Copy raw file
Download raw file
Edit and raw actions

Saving an in-memory SQLite database to a file in Python

I was messing around in Python with an in-memory SQLite database, when I decided I actually wanted to save my experimental database to a file so I could explore it using Datasette.

In-memory databases can be created using sqlite3 like this:

import sqlite3

db = sqlite.connect(":memory:")

Or with sqlite-utils like this:

import sqlite_utils

db = sqlite_utils.Database(memory=True)

The VACUUM INTO command can be used to save a copy of the database to a new file. Here's how to use it:

import sqlite3

db = sqlite3.connect(":memory:")
db.execute("create table foo (bar text)")

db.execute("vacuum main into '/tmp/saved.db'")

# Or with sqlite-utils
import sqlite_utils

db = sqlite_utils.Database(memory=True)

db["foo"].insert({"bar": "Example record"})

db.execute("vacuum main into '/tmp/saved2.db'")
You are visitor 01402775 — GeezerVison does not store the pages you look up.
Best viewed with Netscape Navigator 4.7 or Internet Explorer 6 at 800×600. No cookies required.