🚀 go-pugleaf

RetroBBS NetNews Server

Inspired by RockSolid Light RIP Retro Guy

Article View: comp.programming
Article #23

Atomically Creating An SQLite Database

#23
From: Lawrence D’Oliveiro
Date: Thu, 12 Nov 2026 04:00
41 lines
2917 bytes
Thinking about how one might use O_EXCL mode when opening an SQLite
database that would be automatically created if it didn’t exist, I
discovered that SQLite is not bothered if there is already a
zero-length file with the same name as the database you ask it to open
-- it will automatically fill it out as appropriate. It is easy enough
to create such a file, using O_EXCL to error out if the file already
exists.

With this in mind, I offer some code for opening a database file, with
a choice of opening modes: read-only (must exist); read-write (also
must exist), or read-write-create (will be created and initialized if
it does not exist):

    class DB_OPEN(enum.IntEnum) :
        "database opening modes."
        READONLY = 0 # existing database, only for reading
        READWRITE = 1 # existing database, for reading and writing
        READWRITECREATE = 2 # create database if it doesn’t exist, open for reading and writing
    #end DB_OPEN

    def open_db(dbname, mode) :
        "opens the specified database file and returns a new connection. mode is DB_OPEN value."
        if mode == DB_OPEN.READWRITECREATE :
            fd = os.open(dbname, flags = os.O_RDWR | os.O_CREAT | os.O_EXCL)
              # try to guarantee not overwriting existing file when creating database
        else :
            fd = None
        #end if
        db = sqlite.Connection \
          (
            dbname,
            flags =
                (sqlite.SQLITE_OPEN_READONLY, sqlite.SQLITE_OPEN_READWRITE)
                    [mode >= DB_OPEN.READWRITE]
          )
        if fd != None :
            os.close(fd)
        #end if
        ... initialize database tables as appropriate ...
        return db
    #end open_db_common
Technical Details
Message-ID: <11d3dlh$3keoj$1@dont-email.me>
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail

Show Full Headers
Complete Article Headers
Path: mbox-mo1.pugleaf.net!news-lax.pugleaf.net!srl.newsdeef.eu!news.hispagatos.org!eternal-september.org!feeder.eternal-september.org!nntp.eternal-september.org!.POSTED!not-for-mail
From: Lawrence D’Oliveiro <ldo@nz.invalid>
Newsgroups: comp.programming,comp.lang.python
Subject: Atomically Creating An SQLite Database
Date: Thu, 12 Nov 2026 04:00:50 -0000 (UTC)
Organization: A noiseless patient Spider
Lines: 41
Message-ID: <11d3dlh$3keoj$1@dont-email.me>
MIME-Version: 1.0
Content-Type: text/plain; charset=UTF-8
Content-Transfer-Encoding: 8bit
Injection-Date: Thu, 12 Nov 2026 04:00:50 +0000 (UTC)
Injection-Info: dont-email.me;
	logging-data="3816211"; mail-complaints-to="abuse@eternal-september.org";	posting-account="U2FsdGVkX1/oO3l95M1ufjcf1tb+xT0K"; posting-host="33d00acd775bfe61d11222a1d44a73f8"
User-Agent: Pan/0.165 (Kostiantynivka)
Cancel-Lock: sha1:fE38Tf0ZvDMPxLVRrrO5sfdIH7U= sha256:dtcPv5eD7qHzoMYdBEOT+uSDlZuvjJFCmZgnyXneT2U=
	sha1:8Dzh9vtrlPd+vFQ5+LFSi1fVjh0= sha256:LPGw8NvZ0BkjuiQrOeziomhqM3DbcOSDtWA8s4CtGt8=