Skip to content

User-selectable sqlite library #252

Closed

Description

@aplavin

There are several reasonably popular sqlite library modifications with certain features on top: e.g., sqlcipher with encryption. They are drop-in compatible with the basic sqlite.
It would be useful to let the user choose another library when opening a database in SQLite.jl.

Currently, to use sqlcipher I just forked SQLite.jl, changed the jll library name, and renamed the package to SQLCipher (registered). The only relevant change is: replace using SQLite_jll with using SQLCipher_jll: libsqlcipher as libsqlite.

I imagine something like SQLite.DB("filename", libsqlite=...) should be possible somehow, but not totally sure...

Activity

  1. quinnj commented on Oct 20, 2021

    @quinnj
    Member

    This is tricky because we have to hard-code dependencies to make them useable and performant. I don't have major issues with making changes to the core sqlite_jll package if it's not disruptive for normal cases and just making some of these features the "default" for everyone. We did that w/ a few features already like thread safety.

    The problem w/ passing it as a keyword arg is that there are some dozens, maybe even hundreds of places where you'd have to support and plumb through passing a custom sqlite library. Having your own fork that makes the change is also not unreasonable, honestly. If other's start asking for the same functionality, we could possibly even register; it would just be slightly annoying to keep the SQLCipher up to date w/ changes that happen here.

    Another idea is that we just take both binary packages as dependencies in SQLite and then bake the library selection into a global Ref that each ccall would check. We do something slightly similar in ODBC.jl for letting users use different odbc libraries (and taking all possible choices as dependencies). I'd be open to something like that as long as the extra dependencies seem to warrant it.

  2. aplavin commented on Oct 20, 2021

    @aplavin
    ContributorAuthor

    we have to hard-code dependencies to make them useable and performant

    That's my understanding of the current state as well. Unfortunately, this loses a major feature of dynamic libraries: they can naturally be replaceable by other libraries having the same interface, at runtime.

    I don't have major issues with making changes to the core sqlite_jll package if it's not disruptive for normal cases and just making some of these features the "default" for everyone

    sqlcipher is a separate library based on a specific sqlite version, not just a compilation option.

  3. aviks commented on Jan 10, 2022

    @aviks
    Member

    I don't have major issues with making changes to the core sqlite_jll package if it's not disruptive for normal cases and just making some of these features the "default" for everyone

    Some of the sqlite extensions are proprietary, not open source (Example)-- so this is unfortunately not possible in many cases.

    I'd also like this functionality, but yes, I don't see an easy way to achieve this.

  4. JohnCobbler commented on Jun 4, 2026

    @JohnCobbler
    Contributor

    Revisiting this — modern JLLWrappers + Preferences.jl already solve the drop-in case (sqlcipher, SEE) with zero code changes to SQLite.jl, which sidesteps both concerns raised here: no kwarg to plumb through the ccall sites, and it works for proprietary closed-source libs too since the user points at their own .so.

    SQLite_jll resolves libsqlite at load time via a libsqlite_path preference. Add to your project's LocalPreferences.toml:

    [SQLite_jll]
    libsqlite_path = "/path/to/libsqlcipher.so"

    Verified end-to-end (Julia 1.11, SQLite_jll 3.51.2): with the shipped lib, SELECT sqlite_version() returns 3.51.2; after pointing libsqlite_path at a different system libsqlite (3.45.1), the same query returns 3.45.1, and /proc/<pid>/maps confirms only the overridden .so is mapped. The dlopen is on the literal path, so the override file can be named libsqlcipher.so — no SONAME rename needed.

    Caveats: it's per-environment (one engine per process, not per-DB()), editing the preference triggers one recompile, and the target must be ABI-compatible with the sqlite3 C API (sqlcipher/SEE are by design). But for "I want this project to use sqlcipher/SEE instead of stock sqlite," this covers it.

    If that matches the intent here, happy to send a short docs PR (a "Using an alternative SQLite library" section) rather than any invasive change.

  5. aplavin commented on Jun 4, 2026

    @aplavin
    ContributorAuthor

    per-environment is quite limiting...
    and not sure I understand how exactly it looks from user PoV: say I install SQLite.jl and SQLCipher_jll, how to use the latter then?

  6. JohnCobbler commented on Jun 4, 2026

    @JohnCobbler
    Contributor

    Following up on the earlier LocalPreferences.toml override note — that one used a system-libsqlite3
    version swap to prove the mechanism. I've now upgraded it to real SQLCipher, built from source, with
    encryption actually exercised end-to-end
    through unmodified SQLite.jl.

    Setup — built SQLCipher from source (community 4.16.0, SQLite core 3.53.1, OpenSSL provider) and
    pointed SQLite.jl at it with nothing but:

    # LocalPreferences.toml
    [SQLite_jll]
    libsqlite_path = "/abs/path/to/libsqlcipher.so"

    Engine identity (the smoking gun) — stock SQLite returns zero rows for PRAGMA cipher_version;
    through the override it returns the cipher version:

    db = SQLite.DB()
    DBInterface.execute(db, "SELECT sqlite_version()")   # 3.53.1
    DBInterface.execute(db, "PRAGMA cipher_version")     # 4.16.0 community

    Encryption round-trips through the normal SQLite.jl API. PRAGMA key must come first, before any
    schema/DML:

    db = SQLite.DB("encrypted.db")
    DBInterface.execute(db, "PRAGMA key = 'testkey-correct-horse'")   # key BEFORE anything else
    DBInterface.execute(db, "CREATE TABLE secrets(id INTEGER PRIMARY KEY, msg TEXT)")
    DBInterface.execute(db, "INSERT INTO secrets(msg) VALUES('alpha-classified')")

    Results:

    • Encrypted on disk — first 16 bytes are random ciphertext, not the SQLite format 3\0 magic:
      hex   : 20 6f 9f b1 e0 67 8b af c7 09 75 80 ec d5 13 61
      matches "SQLite format 3\0"? false
      
      (file encrypted.db → data, and the header is different every time — SQLCipher salt randomization.)
    • No key ⇒ locked out — reopening and querying without PRAGMA key throws:
      SQLiteException("file is not a database")
      
    • With key ⇒ rows decrypt — reopen, PRAGMA key='...', and all rows read back intact.
    • Interop — the file SQLite.jl wrote is read back by the standalone sqlcipher CLI with the same key,
      and rejected without it.

    I also confirmed the live library is genuinely SQLCipher via Libdl.dlopen(..., RTLD_NOLOAD) +
    Libdl.dlpath (resolves to the SQLCipher .so) and by resolving the SQLCipher-exclusive symbol
    sqlcipher_codec_pragma through that handle; a stock-SQLite_jll control shows none of these
    (no cipher_version, no libcrypto mapped, no sqlcipher_* symbols).

    Two small gotchas worth recording: (1) changing libsqlite_path auto-triggers SQLite_jll
    precompilation — no manual cache clearing needed to swap engines; (2) PRAGMA key ordering matters —
    it must be the first statement after opening.

    Net: [SQLite_jll] libsqlite_path is a clean, code-change-free way to run SQLite.jl on a SQLCipher
    drop-in with full transparent encryption.

  7. aplavin commented on Jun 4, 2026

    @aplavin
    ContributorAuthor

    seems like pure llm comments

  8. JohnCobbler commented on Jun 5, 2026

    @JohnCobbler
    Contributor

    Yes, my ai drafted the walkthrough for me, I thought you were asking for one. I cant's tell if your objection was aimed at lack of brevity or you are questioning the solution aproach itself? I tested it locally first before posting.
    TLDR - [SQLite_jll] libsqlite_path in LocalPreferences.toml does what your SQLCipher fork does, without the fork - PRAGMA cipher_version returns 4.16.0 through unmodified SQLite.jl. Happy to send a docs PR or test a different approach.

  9. quinnj-bot commented on Nov 18, 2026

    @quinnj-bot
    Contributor

    Thanks @JohnCobbler, the preference route is the right answer here: setting libsqlite_path under [SQLite_jll] in LocalPreferences.toml points SQLite.jl at another SQLite build, such as SQLCipher, with no change to SQLite.jl. A short section in the docs showing that would be welcome as a PR from anyone. Closing the issue, since no code change is needed.

    [comment generated via AI]

  10. aplavin commented on Nov 18, 2026

    @aplavin
    ContributorAuthor

    I still don't understand how that would work in practice. A actual concrete explanation would help!
    For the simplest scenario: say I have a package that needs sqlcipher. What exactly do I put in that package so users install it as ]add MyPackage and everything works properly?

  11. quinnj commented on Nov 21, 2026

    @quinnj
    Member

    Sorry for the slow response here. Yeah, the ability to use preferences would work in a specific Project environment/app, but wouldn't work for a case where you wanted your package to use a certain sqlite lib for any downstream users. i.e. it's end-user-controlled. Not sure if that's the case you're asking for @aplavin, but I assume so. For that case to work, we'd need to make the sqlite lib string be configurable, probably as a field in the SQLite.DB struct, defaulitng to sqlite_jll. I think that should work. I'll have my bot post the example of how you can use teh Preferences approach for end-users.

  12. quinnj-bot commented on Nov 21, 2026

    @quinnj-bot
    Contributor

    Here's the end-user Preferences setup Jacob mentioned. In the project or app environment that should use SQLCipher:

    pkg> add SQLite SQLite_jll SQLCipher_jll Preferences
    
    julia> using Preferences, SQLite_jll, SQLCipher_jll
    
    julia> set_preferences!(SQLite_jll, "libsqlite_path" => SQLCipher_jll.libsqlcipher_path)

    This writes a LocalPreferences.toml next to the project's Project.toml. Restart Julia. From then on, plain using SQLite in that project runs on SQLCipher:

    julia> using SQLite
    
    julia> db = SQLite.DB("secret.db");
    
    julia> DBInterface.execute(db, "PRAGMA key = 'my key'");  # must be the first statement

    Tested on macOS with Julia 1.12, SQLite.jl 1.11.0 and SQLCipher_jll 4.13.0: PRAGMA cipher_version returns 4.13.0 community, the file on disk has no SQLite format 3 header, and reading it without the key fails with "file is not a database".

    Limits:

    • It applies to every SQLite.DB in the process.
    • The stored path is specific to the machine. Re-run the set_preferences! line after updating SQLCipher_jll, or it keeps pointing at the old build.
    • SQLite_jll has to be listed in the project (hence the add line), because Julia only reads preferences for packages the project lists.

    [comment generated via AI]

  13. aplavin commented on Nov 21, 2026

    @aplavin
    ContributorAuthor

    Yes that's what I thought as well – not really convenient... Will continue using SQLCipher.jl then, very unfortunate but apparently unavoidable duplication of code: it's literally the SQLite.jl package, just with library switched.

  14. quinnj commented on Nov 22, 2026

    @quinnj
    Member

    Let me throw some tokens at what it would look like to make that configurable. We've had this kind of problem with HTTP before around how to "bake in" middleware client-side in downstream libraries. We finally settled on the HTTP.@client macro, which essentially provides your own package's HTTP "verbs", but with your own layers baked in. e.g. you get AWS.get, AWS.post, AWS.put, etc. So it "feels" HTTP-like, basically the same interface, but you get to get the customized stack.

    Would something like that work in your case? either via a wrapper struct or via interface methods? If you can help me settle on what would work, I feel like we could adapt SQLite to probably make it work.

  15. aplavin commented on Nov 22, 2026

    @aplavin
    ContributorAuthor

    Idk, something like SQLite.DB(<path>, library=<library path>) maybe? Default = from sqlite_jll, but if user installs sqlcipher_jll, they can just pass library path from it.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions