'Error trying to create a database: sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file
I'm trying to create a database using Flask and Sqlite3, I am not sure this error is about the file path, as you can see in my code I have tried out different ways to specify the path but none seems to work, my code looks like this
from flask import Flask, request, jsonify, make_response
from flask_sqlalchemy import SQLAlchemy
import uuid
from werkzeug.security import generate_password_hash, check_password_hash
import jwt
import datetime
from functools import wraps
import os
app = Flask(__name__)
app.config['SECRET_KEY'] = 'thisissecret'
basedir = os.path.abspath(os.path.dirname(__file__))
app.config['SQLALCHEMY_DATABASE_URI']="sqlite:////"+os.path.join(basedir, "app.db")
#file_path = os.path.abspath(os.getcwd())+"\app.db"
#app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///'+file_path
#'sqlite:///mnt/c/Projects/bucketlist.db' C:\Users\MOSES MATOVU\Desktop
#app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:////mnt/C/Users/MOSES MATOVU/Desktop/bucketlist/app.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
public_id = db.Column(db.String(50), unique=True)
name = db.Column(db.String(50))
password = db.Column(db.String(80))
admin = db.Column(db.Boolean)
class Todo(db.Model):
id = db.Column(db.Integer, primary_key=True)
text = db.Column(db.String(50))
complete = db.Column(db.Boolean)
user_id = db.Column(db.Integer)
if __name__ == '__main__':
app.run(debug=True)
when I run the script: db.create_all() I get the following error
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file
Fulls Details on the above error below
**Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1094, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 1086, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\sql\schema.py", line 4883, in create_all
bind._run_ddl_visitor(
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\engine\base.py", line 3146, in _run_ddl_visitor
with self.begin() as conn:
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\engine\base.py", line 3062, in begin
conn = self.connect(close_with_result=close_with_result)
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\engine\base.py", line 3234, in connect
return self._connection_cls(self, close_with_result=close_with_result)
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\engine\base.py", line 96, in __init__
else engine.raw_connection()
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\engine\base.py", line 3313, in raw_connection
return self._wrap_pool_connect(self.pool.connect, _connection)
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\engine\base.py", line 3283, in _wrap_pool_connect
Connection._handle_dbapi_exception_noconnection(
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\engine\base.py", line 2117, in _handle_dbapi_exception_noconnection
util.raise_(
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
raise exception
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\engine\base.py", line 3280, in _wrap_pool_connect
return fn()
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\pool\base.py", line 310, in connect
return _ConnectionFairy._checkout(self)
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\pool\base.py", line 868, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\pool\base.py", line 476, in checkout
rec = pool._do_get()
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\pool\impl.py", line 256, in _do_get
return self._create_connection()
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\pool\base.py", line 256, in _create_connection
return _ConnectionRecord(self)
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\pool\base.py", line 371, in __init__
self.__connect()
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\pool\base.py", line 665, in __connect
with util.safe_reraise():
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\util\langhelpers.py", line 70, in __exit__
compat.raise_(
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\util\compat.py", line 207, in raise_
raise exception
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\pool\base.py", line 661, in __connect
self.dbapi_connection = connection = pool._invoke_creator(self)
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\engine\create.py", line 590, in connect
return dialect.connect(*cargs, **cparams)
File "C:\Users\MOSES MATOVU\Desktop\bucketlist\venv\lib\site-packages\sqlalchemy\engine\default.py", line 597, in connect
return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file
(Background on this error at: https://sqlalche.me/e/14/e3q8)**
Sources
This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.
Source: Stack Overflow
| Solution | Source |
|---|
