SQLITE file is encrypted or is not a database - 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.

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.

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 :
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.
There is software that repairs corrupted databases. Download one and use it to repair the database.
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

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.

If you've interacted with the database at some point while specifying journal_mode = WAL, and then later try to use the database from a client that does not support WAL (< v3.7.0), this error can also come up.
As noted in the SQLite documentation under Backwards Compatibility, to resolve that without having to recreate the database, explicitly set the journal mode to DELETE:
PRAGMA journal_mode=DELETE;

Your database did not become encrypted (this is only one of the two options in the error message).
Your data recovery tool did not recover the correct data; what you have in the file is something else.
You have to restore the database file from the backup.

The issue is with sqlcipher version upgrade in my case, Whenever I update my pod it automatically upgrade the sqlcipher and the error occurred.
For a quick fix just manually add the SDK instead of Pod install. And for a proper solution use this link GitHub Solution

Related

SQLite3 Database File Locked to Reads and Integrity Checks

A process crashed while trying to commit an insert of 2-3 million rows. Now, I'm finding the database file is locked for all activity, including reading, dumping, and running an integrity check.
Can I unlock this database in some way or get sqlite3 to recover the file?
Is there any way of recovering the contents of this database file? I do have a backup from 24 hours ago, but a variety of other data will be lost if I have to do a complete rollback to an older version of the database.
Code below:
$ sqlite3 dbFile.sqlite
SQLite version 3.19.3 2017-06-08 14:26:16
Enter ".help" for usage hints.
sqlite> PRAGMA integrity_check;
Error: database is locked
sqlite> .tables
Error: database is locked
sqlite> SELECT * FROM dbTable LIMIT 1;
Error: database is locked
Running fuser on the database file indicates that no processes are currently attempting to access the file. There is a hot file in the same directory (i.e. dbFile.sqlite-journal exists).
Using stat, I see that the folder in question is of type panfs, which seems likely to be the cause of the issue. What can I do here?
Notably not a duplicate of this question, as the database is (1) not locked by any running process, as mentioned above, (2) locked for reading as well, and (3) due to the full read/write lock the .dump solution in this answer is inapplicable (as .dump fails).
Closest related question is this question, although that one is on a different NFS filesystem.
Apparently, the PanFS server(s) did not notice the crash, and so still report the write lock.
You could try to unmount and re-mount the file system on this machine, if possible.
Alternatively, copy both files (database and journal) elsewhere. When you open that database, SQLite will roll back the partial transaction.

How to recover data from a corrupt SQLite3 database?

I've recovered data from a formatted hard drive for use in a lawsuit. The data is Skype logs, which are stored in SQLite3 databases. Unfortunately, the disk was formatted and a new copy of OS X was installed on the drive. I scanned the drive and found the files I am looking for, but it seems that the database I'm after is corrupt.
I tried the following command I found by searching on SO:
$ sqlite3 mydata.db ".dump" | sqlite3 new.db
Unfortunately, dumping this way excludes the table of records I'm looking for (Messages). Since I can get the format of the DB from Skype by just logging in with another account and generating a new main.db for it, do I have any additional options for extracting the contents of the corrupt DB? Failing that, is there a way to export the raw contents of the database in a text file or something? I only care about grabbing certain messages, which I can search for.
When the database is corrupt, the ".dump" command extracts all of the usable information, but then ends with a ROLLBACK because it encountered corruption.
Instead, store the output in a file:
$ sqlite3 mydata.db ".dump" > mydata.dump
Then, you can view the data directly in that file, or you can change the last line from "ROLLBACK" to "COMMIT" using a text editor. After that, you can load the valid portion of the data into a database using:
$ sqlite3 new.db < mydata.dump
First check for the PRAGMA integrity_check in command console and click on play button, note down the errors and repair them seperately or try exporting and then importing SQL file to new database and restart the database, it generally removes the slug files stored in the cache. If the above method does not work out then you can try SQLite database recovery tool https://www.recoveryandmanagement.com/repair-sqlite-database-manually/

how to upgrade the sqlite database without lost data?

I have an application in C# that uses System.Data.SQLite. In my case I use a recent version of SQL Lite database, by now I can see that the new versión has released, and int sqlite.org webpage says that is recommended to upgrade the database.
My question is how to upgrade without lost the information in my actual database.
How can I chech the version of the data
Thanks.
EDIT: what I mean is when I create a new database with the sqlite3 library, I guess that the database file, my database.db has a version. When I update the sqlite3 library, I am update the sqlite3 command line, but the database file still has the version that had when I created it.
So if in the new versions for example add new features to the database, for example triggers, foreign keys and so on, if I am not wrong, this features must be in the database file, not in the sqlite3 library, because when I access to the database for example with entity framework, I don't use sqlite3 library, I use System.SQLite.Data library.
am I wrong? the datafile is never update and only the library can be updated?
Thanks.
Upgrading the SQLite library will not have any effect on your database file.
Changes like foreign keys do not affect the database file.
The last change that affected the file format was a long time ago.

New to sqlite3, how do I execute .sql code on .db3 files?

Very basic question, having a hard time finding an explanation online.
I have a file code.sql that can be run on two different databases, a.db3 and b.db3. I used sqlite a.db3 to open the database in sqlite3. How do I run code.sql on it?
Use the .read code.sql command, or call sqlite3 with the file as input: sqlite3 a.db3 < code.sql.
I am guessing that you are trying to use the sqlite3 command line tool that you can dowload from the sqlite.org website.
I recomend that you use, instead, sqlitestudio http://sqlitestudio.one.pl
This has a feature to execute SQL from a file on a database:
Use DB Browser for SQLite is a high quality, visual, open source tool to create, design, and edit database files compatible with SQLite.
You can download the DB Browser at SQLite https://sqlitebrowser.org/

How to make a database service in Netbeans 6.5 to connect to SQLite databases?

I use Netbeans IDE (6.5) and I have a SQLite 2.x database. I installed a JDBC SQLite driver from zentus.com and added a new driver in Nebeans services panel. Then tried to connect to my database file from Services > Databases using this URL for my database:
jdbc:sqlite:/home/farzad/netbeans/myproject/mydb.sqlite
but it fails to connect. I get this exception:
org.netbeans.modules.db.dataview.meta.DBException: Unable to Connect to database : DatabaseConnection[name='jdbc:sqlite://home/farzad/netbeans/myproject/mydb.sqlite [ on session]']
at org.netbeans.modules.db.dataview.output.SQLExecutionHelper.initialDataLoad(SQLExecutionHelper.java:103)
at org.netbeans.modules.db.dataview.output.DataView.create(DataView.java:101)
at org.netbeans.modules.db.dataview.api.DataView.create(DataView.java:71)
at org.netbeans.modules.db.sql.execute.SQLExecuteHelper.execute(SQLExecuteHelper.java:105)
at org.netbeans.modules.db.sql.loader.SQLEditorSupport$SQLExecutor.run(SQLEditorSupport.java:480)
at org.openide.util.RequestProcessor$Task.run(RequestProcessor.java:572)
[catch] at org.openide.util.RequestProcessor$Processor.run(RequestProcessor.java:997)
What should I do? :(
The current version of Zentus SQLiteJDBC is v053, based on SQLite 3.6.1. It will not open a 2.x SQLite database. Perhaps you can use SQLite 2.x command line tool to .dump your database, and the Sqlite3 command line tool to .load it. The use Zentus SQLiteJDBC to access the new SQLite 3.x database.
Alternatively, use a JDBC driver that supports SQLite 2 such as this one.
It's againg me...
I have made two mistakes during my first attempt. After setting CLASSPATH as a system variable (hope I didn’t broke smth else :)), putting sqlite_jni.dll to the system32 folder and correcting JDBC url I have got a success :)
I also have downloaded their SQLite ODBC wrapper. Installed it and made a connection to my SQLite2 database via ordinary and UTF8 based ODBC driver. I also used built in NetBeans JDBC-ODBC Bridge driver to be able to set up this connection.
All three connections have been created but:
ordinary ODBC driver: I see text data in a wrong encoding. All other columns are displayed correctly
UTF8 ODBC driver: I don’t see text data at all. All other columns are displayed correctly
JDBC driver: I don’t see any column at all. "Select * from my_any_table" always returns an empty single column
I have Russian based data in my database.
So...currently I have returned to sqlite command line interface :))

Resources