-1

I'm trying on making a simple forums website using a tutorial and I'm on the point of making a database for the website. However, I always get the Invalid SQLite URL Error whenever I attempt to make it using db.create_all(). I'm an absolute beginner at this stuff so I would really need some help. :( I think It's mainly due to this code

app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///slisff.db'

Tried adding slashes, pasting the actual address of my project/venv, nothing. Both tried on cmd and pycharm, but still unsuccessful. I'm using a Windows Laptop and PyCharm.

from flask import Flask, render_template, request, redirect, url_for, session, flash
from forms import RegistrationForm, LoginForm
import sqlite3
from flask_sqlalchemy import SQLAlchemy
from datetime import  datetime
# from flask_login import LoginManager
#
# login_manager = LoginManager()


app = Flask(__name__)
#Protect Against Cookie Modification
app.config['SECRET_KEY'] = 'f9a2fd8f07fae783cf24ae35997a2a7c'
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///slisff.db'
db = SQLAlchemy(app)

#Unique ID for each user/entry in the user database
#User Info Class
class User(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    username = db.Column(db.String(25), unique=True, nullable=False)
    email = db.Column(db.String(50), unique=True, nullable=False)
    prof_pic = db.Column(db.String(20), nullable=False, default='default.jpg')
    password = db.Column(db.String(60), nullable=False)
    #shows relationship of posts made to a single author that wrote them, backref is adding pseudo column to post model
    posts = db.relationship('Post', backref='author', lazy=True)
    #How our user object (info) will be printed
    def __repr__(self):
        return f"User('{self.username}', '{self.email}', '{self.prof_pic}')"

#Class for posts made in the Freedom Forums
class Post(db.Model):
    id = db.Column(db.Integer, primary_key=True)
    title = db.Column(db.String(90), nullable=False)
    date_posted = db.Column(db.DateTime, nullable=False, default=datetime.utcnow)
    content = db.Column(db.Text, nullable=False)
    user_id = db.Column(db.Integer, db.ForeignKey('user.id'), nullable=False)

    def __repr__(self):
        return f"Post('{self.title}', '{self.date_posted}')"

This is the error message I'm getting.

>>> db.create_all()
Traceback (most recent call last):
  File "<stdin>", line 1, in <module>
  File "C:\Users\Francois\Desktop\LIS 161\SLISForums\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 109
4, in create_all
    self._execute_for_all_tables(app, bind, 'create_all')
  File "C:\Users\Francois\Desktop\LIS 161\SLISForums\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 108
6, in _execute_for_all_tables
    op(bind=self.get_engine(app, bind), **extra)
  File "C:\Users\Francois\Desktop\LIS 161\SLISForums\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 101
7, in get_engine
    return connector.get_engine()
  File "C:\Users\Francois\Desktop\LIS 161\SLISForums\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 594
, in get_engine
    self._engine = rv = self._sa.create_engine(sa_url, options)
  File "C:\Users\Francois\Desktop\LIS 161\SLISForums\venv\lib\site-packages\flask_sqlalchemy\__init__.py", line 102
7, in create_engine
    return sqlalchemy.create_engine(sa_url, **engine_opts)
  File "<string>", line 2, in create_engine
  File "C:\Users\Francois\Desktop\LIS 161\SLISForums\venv\lib\site-packages\sqlalchemy\util\deprecations.py", line
298, in warned
    return fn(*args, **kwargs)
  File "C:\Users\Francois\Desktop\LIS 161\SLISForums\venv\lib\site-packages\sqlalchemy\engine\create.py", line 564, in create_engine
    (cargs, cparams) = dialect.create_connect_args(u)
  File "C:\Users\Francois\Desktop\LIS 161\SLISForums\venv\lib\site-packages\sqlalchemy\dialects\sqlite\pysqlite.py", line 545, in create_connect_args
    raise exc.ArgumentError(
sqlalchemy.exc.ArgumentError: Invalid SQLite URL: sqlite://site.db
Valid SQLite URL forms are:
 sqlite:///:memory: (or, sqlite://)
 sqlite:///relative/path/to/file.db
 sqlite:////absolute/path/to/file.db
  • Is `slisff.db` in the same directory than your flask app ? If not, make sure the path is correct because with the value `'sqlite:///slisff.db'` you are considering that your db is in the same directory. – Flo Jun 28 '21 at 13:27
  • It's supposed to be. But thank you, it's all solved now. Idk what happened, but I just relaunched PyCharm and it worked surprisingly – JF Castillo Jun 28 '21 at 13:47

1 Answers1

0

save your program and exit it. then retry. because it is taking // in error. it should take ///. use three slashes in your connection URL: qlite:///slisff.db