Skip to content

Helper function for constructing URIs for sqlite3.connect() #136560

Open
@brettcannon

Description

@brettcannon

Feature or enhancement

Proposal:

When working with SQLite3 URIs, one very subtle thing about them is if you are on Windows and the database path starts with a drive letter, you need to make sure there's a leading /. You must also use POSIX file path separators.

A non-optimized solution is:

db_path = path.resolve(strict=True).as_posix()
if not db_path.startswith("/"):
    # Starts with a Windows drive letter, so add leading slash.
    db_path = "/" + db_path

This seems subtle enough to potentially warrant a function to take a pathlib.Path object and construct the resulting file: URI appropriately. I'm sure something nicer that doesn't force the absolute path could be used to save on any extra stat calls (if people are even that concerned for a single call that opens a database).

Has this already been discussed elsewhere?

No response given

Links to previous discussion of this feature:

No response

Metadata

Metadata

Assignees

No one assigned

    Labels

    Projects

    Status

    TODO: Feature

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions