0

I am trying to connect with a full path but I get this problem

>>> path = "/home/astro/Fun LAB/DBlist"
>>> db = sql.connect(path)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
sqlite3.OperationalError: unable to open database file
>>> 

i 've also tried this

>>> path = "/home/astro/Fun\ LAB/DBlist"
>>> db = sql.connect(path)
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
sqlite3.OperationalError: unable to open database file
>>> 

I know it's because of the whitespace cause I've tried this and it works

>>> path = "/home/astro/DBlist"
>>> db = sql.connect(path)
>>> 

so is there is an easy way to escape the whitespaces in the path or am I doing something wrong?

astro
  • 55
  • 1
  • 5
  • 1
    Perhaps it is a permission issue. Do you have write and execute permission on the `Fun LAB` directory? – unutbu Mar 20 '16 at 23:58
  • Maybe this answer can help? http://stackoverflow.com/a/4637055/168377 – emil.p.stanchev Mar 21 '16 at 00:03
  • @unutbu I also tried to run the script from the same folder like this " ~/Fun Lab $ python3 Python 3.4.3 (default, Oct 14 2015, 20:28:29) [GCC 4.8.4] on linux Type "help", "copyright", "credits" or "license" for more information. >>> import sqlite3 as sql >>> path = "DBlist" >>> db = sql.connect(path) >>> " so that's why I don't think it's a permission problem – astro Mar 21 '16 at 00:21
  • @emil.p.stanchev I've already seen this before asking but I couldn't find where does it help me – astro Mar 21 '16 at 00:30

3 Answers3

1

You might try the uri style approach - it worked for me in getting past connection issues due to spaces in file paths with sqlite3:

  • Escaping the spaces with %20
  • prefixing the path with file:///

Examples:

  • file:///C:/Documents%20and%20Settings/fred/Desktop/data.db file:///C:/Documents%20and%20Settings/fred/Desktop/
0

I would suggest naming files or folders without spaces. (I don't think it's possible either) Instead, use dash or underscores.

The next problem is to see where you're running your Python interpreter. If it's nested inside a directory and you're trying to create a path from the outside, then your link would raise an error.

>>> path = "/home/astro/Fun LAB/DBlist"

or

>>> path = "/home/astro/Fun\ LAB/DBlist"

or

>>> path = "/home/astro/DBlist"

wouldn't work if you run python inside these directories or from another.

To correct this, try:

>>> path = "../home/path/to/file"
Van
  • 65
  • 9
0

Having spaces is indeed a problem. I would suggest bring a symlink and work with that:

!ln -sf '$path_to_db_file' .
db = sql.connect('db_file')