Python的SQLite URL无效(SQLAlchemy)

2024-04-19 00:36:46 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试使用教程制作一个简单的论坛网站,我正在为该网站制作一个数据库。但是,每当我尝试使用db.create_all()创建SQLite URL时,总会出现无效的SQLite URL错误。我是这方面的新手,所以我真的需要一些帮助(我想主要是因为这个代码

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

尝试添加斜杠,粘贴我的项目/venv的实际地址,什么都没有。都尝试了cmd和pycharm,但仍然不成功。我正在使用Windows笔记本电脑和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}')"

这是我收到的错误信息

>>> 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

Tags: inselffalseflaskdbsqlitevenvsqlalchemy