Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Cannot connect to a SQLite database file if its path contains a space character #258

Closed
g-s-k opened this issue Apr 16, 2020 · 2 comments · Fixed by #259
Closed

Cannot connect to a SQLite database file if its path contains a space character #258

g-s-k opened this issue Apr 16, 2020 · 2 comments · Fixed by #259

Comments

@g-s-k
Copy link
Contributor

g-s-k commented Apr 16, 2020

Seems like this has something to do with handling connection strings as URLs. I set a breakpoint in core::str::trim_start_matches and checked on the call here - when passing in a connection string "sqlite://./test directory/data.db" the value of self is "sqlite://./test\\%20directory/data.db". This is good for postgres/mysql connections as they really are URLs, but not so much for SQLite. For reference, the SQLite docs say percent-encoding is optional, so the double-backslash is probably what trips it up.

Minimal reproduction:

# Cargo.toml
[package]
name = "sqlx_test"
version = "0.1.0"
edition = "2018"

[dependencies]
sqlx = { version = "0.3", default-features = false, features = [ "runtime-tokio", "sqlite" ] }
tokio = { version = "0.2.17", features = ["full"] }
/* src/main.rs */
use sqlx::{prelude::*, SqliteConnection};

#[tokio::main]
async fn main() {
    let db_file = std::env::args().nth(1).unwrap();
    let mut conn = SqliteConnection::connect("sqlite://".to_string() + &db_file)
        .await
        .unwrap();
    conn.execute("CREATE TABLE IF NOT EXISTS things (id INT PRIMARY KEY NOT NULL)")
        .await
        .unwrap();
}
$ mkdir 'test'
$ cargo run -- test/data.db
 ...
$ mkdir 'test directory'
$ cargo run -- 'test directory/data.db'
 ...
thread 'main' panicked at 'called `Result::unwrap()` on an `Err` value: Database(SqliteError { code: "21", message: "bad parameter or other API misuse" })', src/main.rs:5:20
note: run with `RUST_BACKTRACE=1` environment variable to display a backtrace

From what I saw in some other issue comments here, I understand that the URL handling may be a temporary solution (?) for cleaning up the connection strings - I would be willing to help on the path forward, whether that means a small fix for this or for larger changes. Let me know if that sounds appealing.

@g-s-k
Copy link
Contributor Author

g-s-k commented Apr 17, 2020

The PR linked above fixes this issue in-the-narrow, but I am still on board if you want help with a larger refactor 😄

@mehcode
Copy link
Member

mehcode commented Apr 18, 2020

For sure. We can discuss design in #174 on how to fix this in general.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants