I have created a database named database.db
When I create a table in the database I get the error:Error: file is not a database
nehal@nehal-Inspiron-5559:~/Desktop/UAV$ sqlite3 database.db
SQLite version 3.20.1 2017-08-24 16:21:36
Enter ".help" for usage hints.
sqlite> CREATE TABLE users(
...> password varchar(10),
...> email text,
...> name text
...> );
Error: file is not a database
How do I resolve the error?
asked Feb 25, 2018 at 13:44
5
Create database without the .db
extension.
for e.g. sqlite3 users //this creates a database names users
answered Feb 25, 2018 at 16:58
user7441user7441
4072 gold badges5 silver badges11 bronze badges
5
The error can occur when a database was created with cipher.
So sqlite3_open
succeded but any sqlite3_exec
or sqlite3_prepare
will be failed with this error.
answered Feb 2, 2021 at 0:00
Aikon MogwaiAikon Mogwai
4,9542 gold badges18 silver badges31 bronze badges
2
You can also use
.open users
it will also create the database
and to open an existing database users.db use
.open users.db
if .db is not used new database of same name will be created
answered Nov 10, 2019 at 5:45
DevanshuDevanshu
88310 silver badges22 bronze badges
Same problem with WPF and SQLiteAdministrator creating a SQLite v3 DB with Database->New->SQLite 3 DB option and «Data Source=whatever.s3db; Version=3» in the WPF code using System.Data.SQLite library.
Select Database->Migrate to SQLite3 option in SQLiteAdministrator solves the problem. Its works for me. I hope this helps someone.
answered Mar 29, 2022 at 15:10
I have created a database named database.db
When I create a table in the database I get the error:Error: file is not a database
nehal@nehal-Inspiron-5559:~/Desktop/UAV$ sqlite3 database.db
SQLite version 3.20.1 2017-08-24 16:21:36
Enter ".help" for usage hints.
sqlite> CREATE TABLE users(
...> password varchar(10),
...> email text,
...> name text
...> );
Error: file is not a database
How do I resolve the error?
asked Feb 25, 2018 at 13:44
5
Create database without the .db
extension.
for e.g. sqlite3 users //this creates a database names users
answered Feb 25, 2018 at 16:58
user7441user7441
4072 gold badges5 silver badges11 bronze badges
5
The error can occur when a database was created with cipher.
So sqlite3_open
succeded but any sqlite3_exec
or sqlite3_prepare
will be failed with this error.
answered Feb 2, 2021 at 0:00
Aikon MogwaiAikon Mogwai
4,9542 gold badges18 silver badges31 bronze badges
2
You can also use
.open users
it will also create the database
and to open an existing database users.db use
.open users.db
if .db is not used new database of same name will be created
answered Nov 10, 2019 at 5:45
DevanshuDevanshu
88310 silver badges22 bronze badges
Same problem with WPF and SQLiteAdministrator creating a SQLite v3 DB with Database->New->SQLite 3 DB option and «Data Source=whatever.s3db; Version=3» in the WPF code using System.Data.SQLite library.
Select Database->Migrate to SQLite3 option in SQLiteAdministrator solves the problem. Its works for me. I hope this helps someone.
answered Mar 29, 2022 at 15:10
The SQLite error «file is not a database» occurs when the SQLite library cannot read the file specified in the connection string as an SQLite database file. This can happen for a number of reasons, such as the file being corrupted, the file being an invalid format, or the file being encrypted and inaccessible without the proper key. In this article, we will explore several methods for resolving this issue and accessing the data stored in the SQLite database file.
Method 1: Check the file format
If you are getting «Sqlite Error: file is not a database» error, it means that your database file is not in the correct format or it has been corrupted. One of the ways to fix this issue is by checking the file format of the database file. Here are the steps to do it:
Step 1: Open the database file
To open the database file, you can use the following code:
import sqlite3
conn = sqlite3.connect('database.db')
Step 2: Check the file format
To check the file format, you can use the PRAGMA integrity_check
command. This command checks the integrity of the entire database file and returns a single row with the value «ok» if the database is in the correct format. Here is the code to do it:
cursor = conn.cursor()
cursor.execute('PRAGMA integrity_check')
result = cursor.fetchone()[0]
if result == 'ok':
print('Database file is in the correct format')
else:
print('Database file is not in the correct format')
Step 3: Close the database connection
After you have checked the file format, you should close the database connection using the following code:
By following these steps, you should be able to fix the «Sqlite Error: file is not a database» error by checking the file format of the database file.
Fixing Sqlite Error: file is not a database with a different SQLite library or tool
If you encounter the «file is not a database» error when working with SQLite, one solution is to try a different SQLite library or tool to access the database file. Here are some examples:
Using Python sqlite3 module
import sqlite3
db_file = 'example.db'
try:
conn = sqlite3.connect(db_file)
cur = conn.cursor()
cur.execute('SELECT * FROM my_table')
rows = cur.fetchall()
for row in rows:
print(row)
except sqlite3.Error as e:
print('Error:', e)
finally:
conn.close()
Using SQLite command-line tool
Using DB Browser for SQLite
- Download and install DB Browser for SQLite from https://sqlitebrowser.org/
- Open the tool and click on «Open Database» button
- Select the database file and click «Open»
- Browse the tables and data in the database
Using SQLite Manager add-on for Firefox
- Install SQLite Manager add-on for Firefox from https://addons.mozilla.org/en-US/firefox/addon/sqlite-manager/
- Open Firefox and click on «SQLite Manager» icon
- Click on «Database» menu and select «Connect Database»
- Select the database file and click «Open»
- Browse the tables and data in the database
These are just some examples of using different SQLite libraries or tools to access the database file and fix the «file is not a database» error. Depending on your programming language or platform, there may be other options available.
Method 3: Repair the database file
Repair the database file
If you encounter the «file is not a database» error when trying to access your SQLite database, it is likely that the file has become corrupted. In this case, you can try to repair the database file using the following steps:
-
Open the SQLite command-line tool by running the following command in your terminal:
-
Once you are in the SQLite command-line tool, open the corrupted database file by running the following command:
.open /path/to/corrupted/database/file
-
Check the integrity of the database file by running the following command:
This command will check the integrity of the database file and report any errors that it finds.
-
If the integrity check reports errors, you can try to repair the database file by running the following command:
pragma writable_schema=1; update sqlite_master set sql = '' where type = 'table' or type = 'index'; pragma writable_schema=0; .quit
This command will set the
writable_schema
pragma to 1, which allows you to modify the schema of the database file. It will then update thesqlite_master
table to remove any corrupted schema information, and set thewritable_schema
pragma back to 0. Finally, it will quit the SQLite command-line tool. -
Open the repaired database file by running the following command:
sqlite3 /path/to/repaired/database/file
-
Verify that the database file has been repaired by running the following command:
This command should report that the database file is now free of errors.
Here is the code example that you can try:
sqlite3
.open /path/to/corrupted/database/file
pragma integrity_check;
pragma writable_schema=1;
update sqlite_master set sql = '' where type = 'table' or type = 'index';
pragma writable_schema=0;
.quit
sqlite3 /path/to/repaired/database/file
pragma integrity_check;
That’s it! You should now be able to access your SQLite database file without encountering the «file is not a database» error.
Step 1: Open the database file
import sqlite3
conn = sqlite3.connect('example.db')
query = "SELECT * FROM table_name"
cursor = conn.execute(query)
data = cursor.fetchall()
new_conn = sqlite3.connect('new_database.db')
new_conn.execute('''CREATE TABLE table_name
(column1 datatype PRIMARY KEY,
column2 datatype,
column3 datatype);''')
new_conn.executemany('INSERT INTO table_name VALUES (?,?,?)', data)
new_conn.commit()
Step 4: Close the connections
conn.close()
new_conn.close()
By following these steps, you can extract data from the corrupted database file and save it to a new database file. This should fix the «Sqlite Error: file is not a database» issue.
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and
privacy statement. We’ll occasionally send you account related emails.
Already on GitHub?
Sign in
to your account
Closed
mtissington opened this issue
Mar 22, 2019
· 28 comments
Labels
bug
Confirmed bugs or reports that are very likely to be bugs.
Comments
I’m using the latest nightly build from 22/3/19 with Windows 10
I have a main encrypted database and an plain database.
I open the encrypted database and try to attach the plain database.
After choosing the name for the attached database I get back an error «File is not a database»
As a side note — it seems there are several errors working with attached database.
justinclift
added
the
bug
Confirmed bugs or reports that are very likely to be bugs.
label
Mar 23, 2019
Yeah, it sounds like something needs a bit of tweaking in the SQLCipher vs SQLite attachment code. 😦
@MKleusberg This one’s probably also related to #1758. 😉
MKleusberg
added a commit
that referenced
this issue
Mar 25, 2019
When opening a plain database and trying to attach an unencrypted database we need to explicitly specify that there is no key for the attached database. Otherwise SQLCipher is going to use the same key as for the main database which results in an error. See issue #1814.
Thanks @mtissington! Looks like I didn’t read the SQLCipher documentation carefully enough 😉 This is the problem here:
If no KEY paramater is specified then the attached database will use the exact same raw key and database salt as the main database
It should be fixed in tomorrow’s nightly build. Can you double check it’s working for you? We can then include the fix in our 3.11.2 release 😄
I’ll launch the Win64 nightly build script again in a minute, so there’s something to test sooner (~1/2 hour) rather than later. Hopefully that helps. 😄
No worries, just let me know. 😄
Just committed the second fix 😄
Cool. Just restarted the Win64 build. I’ll do the Win32 one afterwards too, just to keep them even. 😄
Ugh. Something has gone wrong with the VM. It can’t see the outside network at all. 😦
Investigating…
k. Win64 build is running again now. This time it should run ok. 😉
Hmm, still getting both the errors.
(I did a complete uninstall of previous version first)
Very strange. Especially the problem from issue #1799 seems so obvious in hindsight that I’m pretty sure it should be fixed.
@justinclift Are you sure the latest commits are included in the build? I just downloaded the zip file from the latest nightly link, unpacked it, and tried this:
$ strings DB\ Browser\ for\ SQLCipher.exe | grep kdf_iter
PRAGMA sqlitebrowser_edit_encryption.kdf_iter = %1
PRAGMA kdf_iter = %1;
PRAGMA %1.kdf_iter = %2
While this isn’t really the safest method, it looks like there is no mention of cipher_default_kdf_iter
which should be in there since this commit.
Hmmm, it does sound like something went wrong.
I’ll manually run the Win64 build again, watching as it does so. Gimme a few mins…
Nope. That particular script file is hard coded to master. Something truly bizarre would have to have gone wrong for it to be the wrong branch. That being said… we are talking computers after all, so I won’t say «impossible». 😉
Heh heh, I’m curiously awaiting your results then 😄 I have just updated issue #1777 because it could suffer from the same problem. In that case the problem would be at least 20 days old or so.
Just noticed something interesting. On the VM itself, in the directory the builds get saved to prior to upload… the Win32 .msi and .zip, and the Win64 .zip have correct looking timestamp entries.
The Win64 .msi has a timestamp that’s wrong. It’s a timestamp from the morning run.
Something seems to have gone wrong with that Win64 build. The .zip version might be ok, but not sure. Nuking then running things again now. 😉
I tried the zip version too, same issues …
Try the Win32 build. The timestamp on that looks ok.
I’ve just watched the new Win64 build pull down the source and change branches. So far it’s grabbed the right pieces. If 😉 the build completes ok, then the potential fixes will be in it. 😄
Looks promising 👍
$ strings DB\ Browser\ for\ SQLCipher.exe | grep kdf_iter
PRAGMA sqlitebrowser_edit_encryption.kdf_iter = %1
PRAGMA kdf_iter = %1;
PRAGMA cipher_default_kdf_iter = %1
Looking very good 😄 … initial testing seems to fix #1777 #1814 and #1799 …
Good job guys … I’ll test more but I think this is it.
Yay! Good stuff everyone! 🕺
Awesome! Let’s hope the build problem was just a transient thing 😄
Closing this now, as it seems fixed. 😄
MKleusberg
added a commit
that referenced
this issue
Mar 30, 2019
When opening a plain database and trying to attach an unencrypted database we need to explicitly specify that there is no key for the attached database. Otherwise SQLCipher is going to use the same key as for the main database which results in an error. See issue #1814.
Labels
bug
Confirmed bugs or reports that are very likely to be bugs.
Summary: This blog is summarized about the methods to resolve SQLite encryption error. Sometimes, we face a lot of errors during the operation of the SQLite database. But the SQLite encryption error is very common and the message shows like this:
“Fixed SQLite Error – file is encrypted or is not a database”
So, if you reluctantly getting this error and looking forward to a genuine solution to fix this encryption error then, you are in the right place to resolve this error. This is the most common error SQLite users face. Now, before going for the solution let’s have a look at a brief description of the SQLite database.
Introduction to SQLite
SQLite is a lighter version of the SQL database and widely deployed database in the world of application. It is different from the other SQL databases like it does not require a separate server process. And it directly performs reads and writes operations to ordinary disk files. In a single file, a whole SQL database is included containing multiple tables, indices, triggers, and views. The codes of SQLite are in the public domain by which it is widely used in personal and professional work.
Now here I am going to mention some more features, which makes it exclusive from other database engines.
Features of SQLite Database
- It does not require a separate server process, not like other SQL databases.
- The SQLite has a feature to directly read and write to ordinary disk files.
- This Database has ways to create, delete, execute SQL commands, and perform other common database management operations.
- Mostly the SQLite source code is meant for testing and verification.
- The SQLite is embedded in the end program. Its database engine is not a client-server.
Causes of SQLite File is Encrypted or is Not a Database Error
The two major reasons are responsible for the situation when the SQLite file is encrypted or is not a Database. These reasons are as follows:
- Maybe the data file is not password protected.
- SQLite is a sequential database which consists of a minimum of one table and one record. So error occurs when you have created a file as a new file in which it is out of the table.
Troubleshooting this SQLite Encryption Error Manually
- Install the SQLite3 Database:
>> sudo apt-get update
>> sudo apt-get install sqlite3 libsqlite3-dev - Under the install documentation there is an error there when determining database file in etc/turnserver.conf:
userdb=/etc/turnuserdb.conf
should be changed to:
userdb=/var/lib/turn/turndb
PS:
In case of need of the main parts of turnuserdb.conf:
# you can listen ports 80 and 443 instead of 3478/5349
listening-port=3478
alt-listening-port=0tls-listening-port=5349
alt-tls-listening-port=0listening-ip=YourPublicIPv4
relay-ip=YourPublicIPv4
external-ip=YourPublicIPv4# realm=yourdomain.com
# server-name=yourdomain.com# lt-cred-mech
# userdb=/etc/turnuserdb.conf OAuth
user=youruser: your password# use real-valid certificate/privatekey files
# cert=/etc/ssl/certificate.pem
# pkey=/etc/ssl/private.keyno-stdout-log
Automated Method
As we all can see the manual way is very complex and need deep knowledge to perform these steps. Hence, It is advisable to try a third-party tool like SysInfoTools SQLite Repair software to get rid of these complex errors in very easy steps. The software is inbuilt with a result-oriented algorithm that performs SQLite database recovery perfectly.
Conclusion
In the above section, we have discussed the manual and automated approaches to resolve SQLite encryption error. The manual method needs technical knowledge and is not suitable for a novice user, whereas using a third-party tool makes it easier and output-driven way. Anyway, you can choose any method which is suitable for you and if are having any suggestions for us please mention them in our comment section.