简体   繁体   中英

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

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.

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM