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

Error connecting to SQLite database #208

Closed
zamith opened this Issue Feb 12, 2016 · 13 comments

Comments

Projects
None yet
2 participants
@zamith

zamith commented Feb 12, 2016

I have this rust program that uses diesel, but fails when running with 'Error connection to "/Users/zamith/Library/Application Support/ActionAlly/action_ally.db": BadConnection("unable to open database file")':

extern crate rusty_ally;
extern crate diesel;

use self::rusty_ally::*;
use self::diesel::prelude::*;

fn main() {
    let connection = establish_connection();
}

This is strange, because accessing the same DB in Ruby works just fine:

require "sqlite3"
require "dotenv"
Dotenv.load

db = SQLite3::Database.new ENV["DATABASE_URL"]

db.execute("select * from tasks limit 10") do |row|
  p row
end

Am I doing anything wrong? Is this even an issue with Diesel or with libsqlite3-sys?

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 12, 2016

Can you give some steps to reproduce? Do you have write access to that directory? (Also does that directory exist?)

@zamith

This comment has been minimized.

zamith commented Feb 12, 2016

Even if I copy the database to the project's folder it's the same thing. I even tried creating a new database with sqlite3 test.db, change the DATABASE_URL to be ./test.db, and ran the ruby and rust scripts again.

The result for ruby was an error because there was no table (expected), on diesel it complained with the "unable to open database file" again.

There's probably something I'm doing wrong in the rust code. I've pushed it up here.

@zamith

This comment has been minimized.

zamith commented Feb 12, 2016

I get the same error if I remove the test.db file, and run diesel setup. I'm guessing this never happens on the tests because they all start the connection with :memory:.

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 12, 2016

When I run the test suite against a file backed database with RUST_TEST_THREADS=1, the tests pass.

@zamith

This comment has been minimized.

zamith commented Feb 12, 2016

How can I run that? I can try that locally on my machine, and try to figure out what I'm doing wrong.

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 12, 2016

https://gist.github.com/sgrif/1495ed44ac1d83cf2381, then set DATABASE_URL to something like foo.sqlite3 and run the tests.

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 12, 2016

I was able to replicate the error by attempting to run tests with a file in the Application Support directory.

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 12, 2016

It appears that SQLite doesn't like paths with a space in them.

@zamith

This comment has been minimized.

zamith commented Feb 12, 2016

Hum... But even if I have the file on the same directory it is failing for me. I'm defining the path as ./foo.db is that not the correct way?

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 12, 2016

No, foo.db is the valid way, not ./foo.db. It looks like dotenv on Rust doesn't handle any type of quoting or escaping. If you're using that, make sure that the DATABASE_URL line is set to:

DATABASE_URL=/Users/zamith/Library/Application Support/ActionAlly/action_ally.db

and not

DATABASE_URL="/Users/zamith/Library/Application Support/ActionAlly/action_ally.db"

or

DATABASE_URL=/Users/zamith/Library/Application\ Support/ActionAlly/action_ally.db

Based on your original post, I'm guessing that you have double quotes in your .env file.

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 12, 2016

Also, doing ./foo.db seems to work for me just fine. Not sure what's up with that.

@zamith

This comment has been minimized.

zamith commented Feb 12, 2016

That's correct. By removing the quotes it works. That's probably something that could be added to dotenv rust then.

Thanks and sorry for the trouble.

@zamith zamith closed this Feb 12, 2016

@sgrif

This comment has been minimized.

Member

sgrif commented Feb 12, 2016

I'm opening an issue there now. No worries, glad we got it resolved.

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