I am practicing making an API in Python, using Flask and its Flask-SQLAlchemy extension. So, in the "app.config" line, I first used 'sqlite:///data.db' (which I saw in this video: (https://www.youtube.com/watch?v=qbLc5a9jdXoas) as the URI, and it went ok. Here's my script:
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
app = Flask(__name__)
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///data.db'
db = SQLAlchemy(app)
class User(db.Model):
id = db.Column(db.Integer, primary_key=True)
username = db.Column(db.String(80), unique=True, nullable=False)
email = db.Column(db.String(120), unique=True, nullable=False)
def __repr__(self):
return f"{self.username} - {self.email}"
@app.route("/")
def hello():
return "<h1>Welcome to this user database</h1>"
@app.route("/menu")
def add_user():
all = User.query.all()
return {"users":str(all)}
So I decided to try another db address, from the official documentation of Flask-SQLAlchemy, which is 'sqlite:////tmp/test.db' and then started getting the following error (I've replaced my paths with 'PATH' for privacy):
>>> db.create_all()
Traceback (most recent call last):
File "PATH", line 3212, in _wrap_pool_connect
return fn()
File "PATH", line 301, in connect
return _ConnectionFairy._checkout(self)
File "PATH", line 761, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "PATH", line 419, in checkout
rec = pool._do_get()
File "PATH", line 259, in _do_get
return self._create_connection()
File "PATH", line 247, in _create_connection
return _ConnectionRecord(self)
File "PATH", line 362, in __init__
self.__connect()
File "PATH", line 605, in __connect
pool.logger.debug("Error on connect(): %s", e)
File "PATH", line 70, in __exit__
compat.raise_(
File "PATH", line 211, in raise_
raise exception
File "PATH", line 599, in __connect
connection = pool._invoke_creator(self)
File "PATH", line 578, in connect
return dialect.connect(*cargs, **cparams)
File "PATH", line 584, in connect
return self.dbapi.connect(*cargs, **cparams)
sqlite3.OperationalError: unable to open database file
The above exception was the direct cause of the following exception:
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "PATH", line 1094, in create_all
self._execute_for_all_tables(app, bind, 'create_all')
File "PATH", line 1086, in _execute_for_all_tables
op(bind=self.get_engine(app, bind), **extra)
File "PATH", line 4744, in create_all
bind._run_ddl_visitor(
File "PATH", line 3078, in _run_ddl_visitor
with self.begin() as conn:
File "PATH", line 2994, in begin
conn = self.connect(close_with_result=close_with_result)
File "PATH", line 3166, in connect
return self._connection_cls(self, close_with_result=close_with_result)
File "PATH", line 96, in __init__
else engine.raw_connection()
File "PATH", line 3245, in raw_connection
return self._wrap_pool_connect(self.pool.connect, _connection)
File "PATH", line 3215, in _wrap_pool_connect
Connection._handle_dbapi_exception_noconnection(
File "PATH", line 2068, in _handle_dbapi_exception_noconnection
util.raise_(
File "PATH", line 211, in raise_
raise exception
File "PATH", line 3212, in _wrap_pool_connect
return fn()
File "PATH", line 301, in connect
return _ConnectionFairy._checkout(self)
File "PATH", line 761, in _checkout
fairy = _ConnectionRecord.checkout(pool)
File "PATH", line 419, in checkout
rec = pool._do_get()
File "PATH", line 259, in _do_get
return self._create_connection()
File "PATH", line 247, in _create_connection
return _ConnectionRecord(self)
File "PATH", line 362, in __init__
self.__connect()
File "PATH", line 605, in __connect
pool.logger.debug("Error on connect(): %s", e)
File "PATH", line 70, in __exit__
compat.raise_(
File "PATH", line 211, in raise_
raise exception
File "PATH", line 599, in __connect
connection = pool._invoke_creator(self)
File "PATH", line 578, in connect
return dialect.connect(*cargs, **cparams)
File "PATH", line 584, in connect
return self.dbapi.connect(*cargs, **cparams)
sqlalchemy.exc.OperationalError: (sqlite3.OperationalError) unable to open database file
(Background on this error at: http://sqlalche.me/e/14/e3q8)
I can see that it says a lot about connection problems. Also, coming back to 'sqlite:///data.db' "solves" the problem (I mean, what if I needed to use a certain address and it presented this exception?)
I've opened this link at the end of the exception, and it pretty much says that this error might be out of the programmer's control and can be related to corruptions in the file or connection problems(as I suspect). If this is not the case, and as a beginner, it seems a bit advanced for me work around. I mean, if it wasn't for the video I mentioned above, I'd be even more clueless.
Can someone give me a hand on how to investigate this?
Or is it just that this address, coming from an official documentation, possibly the most common used around, is, say, exausted?
Oh, and I might have forgot VSCode open overnight with flask running (the laptop screen was shut, though).
Thank you.