我正在尝试使用教程制作一个简单的论坛网站,并且我正在为该网站制作一个数据库。但是,每当我尝试使用 db.create_all() 进行创建时,我总是会收到无效的 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
保存您的程序并退出。然后重试。因为它错误地采用了//。 它应该需要///。在连接 URL 中使用三个斜杠:qlite:///slisff.db
我可以通过将 sqlite 目录视为虚拟环境目录中的基础来解决这个问题。
最简单的方法是将 slisff.db 文件存储在 sqlite 可执行文件的同一位置。
sql_alchemy_engine = create_engine('sqlite:///slisff.db', pool_recycle=3600)
本场景遵循的目录结构是
。环境 # 这是虚拟根
。 env\sqlite_db\
# sqlite 可执行文件和二进制文件存储在这里
。 env\sqlite_db\slisff.db # 数据库文件与sqlite.exe存放在同一目录下