Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Reading sqlite file with Python: sqlite3.DatabaseError: file is encrypted or is not a database

Tags:

python

sqlite

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.

like image 285
user984003 Avatar asked Feb 14 '26 09:02

user984003


1 Answers

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.

like image 126
rakslice Avatar answered Feb 17 '26 00:02

rakslice



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!