3

I received a sqlite file that I need to read with Python.

I am able to open the file with the application "DB Browser for SQLite". I can see the table structure and execute select statements within that application. So the file seem OK. (I did not create it and do not know how it was created.)

However, when I try to read it from Python I get an error:

sqlite3.DatabaseError: file is encrypted or is not a database

import sqlite3 as lite

sqlite3.sqlite_version
# '3.6.21'

con = lite.connect('path\file.sqlite') 
cur = con.cursor()    
cur.execute('SELECT * from mytable')

# sqlite3.DatabaseError: file is encrypted or is not a database

If I open the file with a text editor then the first words are "SQLite format 3" followed by unreadable text. So it was generated with version 3? Not sure if it needs to be converted to a db file?

I'm not sure where to go from here.

5
  • \f is the control character for form feed. Reference: en.wikipedia.org/wiki/Control_character#In_ASCII You can use raw strings or better yet use os.path.join. Commented Sep 3, 2015 at 22:55
  • Same error if I CD to the directory and supply the filename without any \. Also, if I provide a filename that does not exists then the error is different so I do think it's finding the file. Commented Sep 3, 2015 at 23:07
  • 1
    You could use the sqlite browser to dump the database to SQL, at least then you could see what's in there and load it back to sqlite, hopefully in a way that lets you open it from Python. Commented Sep 3, 2015 at 23:27
  • That worked: exporting to sql and then importing again. Python will open that. Weird! Commented Sep 4, 2015 at 4:21
  • 1
    Your old Python ships with an old SQLite that apparently does not support some new feature used by DB Browser. Commented Sep 4, 2015 at 7:23

1 Answer 1

1

This message may appear when the SQLite database is in a new format that is not compatible with the version of SQLite in your Python installation.

For example, my stock Python reports:

>>> import sqlite3
>>> print sqlite3.sqlite_version
3.6.21

and the file I'm trying to open is, according to the MSYS2 file command:

$ file testfile.sqlite
testfile.sqlite: SQLite 3.x database, last written using SQLite version 3008010

version 3008010 => 3.8.10.

On Windows, you can upgrade the SQLite version in your Python install in-place by downloading a new prebuilt SQLite version from the SQLite Download Page (choose 32-bit or 64-bit to match your Python installation) and dropping its sqlite3.dll in place of the old one in your \PythonX\DLLs directory.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.