sqlite3.DatabaseError: file is not a database
Asked Answered
C

4

5

I get the above error for executing the below INSERT-statement. The database file ce.db is in the same directory as my code and I have successfully created the tables therein.

My sqlite version is 2.8.17 and I am confident that my db file exists as I can see it in my directory and have succeeded in creating tables therein.

import sqlite3

@app.route("/sign_up", methods=["GET", "POST"])
def sign_up():
  # [..other code..]

    conn = sqlite3.connect("ce.db")
    c = conn.cursor()
    result = c.execute("INSERT INTO users (name, hash) VALUES (:name, :hash)", {"name":request.form.get("username"), "hash":hashp})
    conn.commit()

Debugger shows "sqlite3.DatabaseError: file is not a database" error for the line starting with "result=...".

Celadon answered 28/10, 2019 at 4:16 Comment(3)
The file you're trying to open as a database is obviously not a sqlite database; it's some other file that happens to have the same name as what you're trying to open - maybe at a different path? Always a possibility when using a relative path to the database file instead of an absolute one.Spacious
Also, sqlite 2.8.17? Er.... why? That was the last sqlite2 release and dates from 2005. There is no reason to be using such an old obsolete version. Hmm. Maybe you're trying to open a sqlite3 database?Spacious
I figured it out - posting answer belowCeladon
C
4

Did some more digging and it turns out that 2.6.0 is the DB-API version (obtained via print(sqlite3.version) - not entirely sure what that is or what it's for) and my sqlite version (print(sqlite3.sqlite_version)) is 3.22.0. Also realised (running file ce.db in bash) that my ce.db was created on (or with?) 2.x... managed to remove and recreate it with sqlite3 ce.db statement.. seems to work now. All beginner problems, I know but figured it might helpt to share this for any future lost souls like me :)

Celadon answered 28/10, 2019 at 8:56 Comment(1)
Just fell for this after having created a db through cli and trying to access it with sqlalchemy!Koosis
H
6

I also had this problem with creating a db in Python using sqlite3, what solved it for me was to remove .db from database name:

conn = sqlite3.connect("ce.db") --> conn = sqlite3.connect("ce")

(+ also have 2.6.0 DB-API version and my sqlite version is 3.31.1)

Hughett answered 10/11, 2021 at 14:29 Comment(0)
C
4

Did some more digging and it turns out that 2.6.0 is the DB-API version (obtained via print(sqlite3.version) - not entirely sure what that is or what it's for) and my sqlite version (print(sqlite3.sqlite_version)) is 3.22.0. Also realised (running file ce.db in bash) that my ce.db was created on (or with?) 2.x... managed to remove and recreate it with sqlite3 ce.db statement.. seems to work now. All beginner problems, I know but figured it might helpt to share this for any future lost souls like me :)

Celadon answered 28/10, 2019 at 8:56 Comment(1)
Just fell for this after having created a db through cli and trying to access it with sqlalchemy!Koosis
F
1

Anna, please, take a close look at your code. You are trying to connect to ce.db, but, as you say, your database is cd.db. You have typo mistake in your code, here is Sqlite trying you to say file ce.db is not a database file.

conn = sqlite3.connect("ce.db") # here is misspelled database name
Farandole answered 28/10, 2019 at 4:22 Comment(1)
Apologies. It is ce.db. I mistyped in the text. Both the code and the filename are ce.db.Celadon
B
0

Sometimes the DB files are not checked out correctly which can lead to this error. Try doing a git-lfs pull so that it checked out the lfs file correctly as a binary file.

Baptiste answered 25/7 at 15:46 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.