Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

SQLITE file is encrypted or is not a database

Tags:

sqlite

I have a huge problem... I am developing desktop app with SQLite but during copy/paste process I lost a power and process was terminated so base was lost. However, I found a way to recover it but base is encrypted. When I try to open connection using conn.Open(); I get this error. If I try to open it with DB Browser for SQLite it asks me a SQLCipher encryption password so it seams to me that data is lost..

Is there any default password ? Why did this happen and how to prevent it from happening again ? What can I do ?

Thanks in advance.

like image 643
Goran Avatar asked May 24 '17 14:05

Goran


3 Answers

Ok, finally found a solution that works so posting the answer if anybody will have same trouble as I did..

First of all, use good recover software. For repairing the database I found 3 solutions that work without backup :

  1. Open corrupted database using DB Browser an Export Database to SQL. Name it however you want. Then, create new database and import database from SQL.
  2. There is software that repairs corrupted databases. Download one and use it to repair the database.
  3. Download "sqlite3" from sqlite.org and in command line navigate to folder where "sqlite3" is unzipped. Then try to dump the entire database with .dump, and use those commands to create a new database:

    sqlite3 corrupt_table_name.sqlite ".dump" | sqlite3 new.sqlite
    
like image 154
Goran Avatar answered Nov 03 '22 02:11

Goran


Also check that SQLite version you're "connecting" with aligns with the DB file version.

For example, here's a DB file written by SQLite version 3+:

$ file foobar.db 
foobar.db: SQLite 3.x database, last written using SQLite version 3027002

And here I also have 2 versions of sqlite:

$ sqlite -version
2.8.17
$ sqlite3 -version
3.27.2 2019-02-25 16:06:06 bd49a8271d650fa89e446b42e513b595a717b9212c91dd384aab871fc1d0alt1

Obviously in hindsight, opening foobar.db with sqlite version 2 will fail, yielding the same error message:

$ sqlite foobar.db 
Unable to open database "foobar.db": file is encrypted or is not a database

But all is good with the correct version:

$ sqlite3 foobar.db
SQLite version 3.27.2 2019-02-25 16:06:06
Enter ".help" for usage hints.
sqlite> 
sqlite> .databases 
main: /tmp/foobar.db
sqlite> 

The error message is a catch-all, simply meaning that the file format was not recognized.

like image 45
ulidtko Avatar answered Nov 03 '22 01:11

ulidtko


I had the same error when I was trying to access a db dump in another system compared to compared to where it was obtained. When I tried to open on a dev machine, it threw the reported error in this thread:

$ sqlite3 db_dump.sqlite .tables
Error: file is encrypted or is not a database

This turned out to be due to the differences in the sqlite version between those systems. This dev system version was 3.6.20.

The version on the production system was 3.8.9. Once I had the sqlite3 upgraded to same version, I was able to access all its data. I am just showing below the tables are displayed as expected:

# sqlite3 -version
3.8.9
# sqlite3 db_dump.sqlite .tables
capture               diskio                transport     
consumer              filters               processes              

This error is rather misleading to begin with, though.

like image 21
Heelara Avatar answered Nov 03 '22 00:11

Heelara