问题:我刚开始在烧瓶上创建一个网站,我的代码运行顺利,没有任何错误,但是我无法在我的工作目录或我的项目中看到数据库文件,我不知道为什么数据库文件没有在我的目录中显示或创建。有人能帮我吗?
我的目录
Project
|
|__flask-session
|__ST_website
|____flask-session
|____static
|____templates
|______ __init__.py
|______ database.py
|______ primary.py
|______ user_identification.py
|__venv(library root)
|__run.py
(|__ST_database.db # wanted to create)init.py
import os
from flask_session import Session
from flask import Flask
from flask_sqlalchemy import SQLAlchemy
from flask_login import LoginManager
from datetime import timedelta
# Database
db = SQLAlchemy()
DB_NAME = "ST_database.db"
UPLOAD_FOLDER = 'ST_website/static/csv-files' # Upload Folder
def create_app():
""" Creating an App"""
app = Flask(__name__)
app.config['SECRET_KEY'] = 'hi'
app.config['SQLALCHEMY_DATABASE_URL'] = 'sqlite:///ST_database.db'
app.config["SQLAlCHEMY_TRACK_MODIFICATIONS"] = False # Handling Warnings
app.config["SESSION_TYPE"] = "filesystem"
app.permanent_session_lifetime = timedelta(minutes=20) # How long will the data last.
db.init_app(app)
Session(app)
app.config['UPLOAD_FOLDER'] = UPLOAD_FOLDER # Upload file
# file name Variable name
from .primary import primary
from .user_identification import user_identi
# Registering Blueprints
app.register_blueprint(user_identi, url_prefix='/')
app.register_blueprint(primary, url_prefix='/')
login_man = LoginManager()
login_man.login_view = 'user_identi.login' # Directing here if the user is not log-in
login_man.init_app(app) # telling Login manager what app will use
from .database import User, UserData
create_database(app)
@login_man.user_loader
def load_user(id):
"""Load the user"""
return User.query.get(int(id)) # automatically get the Primary Key
return app
def create_database(application):
db_filename = f'ST_website/{DB_NAME}'
if not os.path.exists(db_filename):
db.create_all(app=application)
print('Created Database!')database.py
from . import db
from flask_login import UserMixin
from sqlalchemy.sql import func
class User(db.Model, UserMixin):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(150), unique=True)
name = db.Column(db.String(150))
password = db.Column(db.String(150))
user_data = db.relationship('UserData')
class UserData(db.Model):
id = db.Column(db.Integer, primary_key=True)
email = db.Column(db.String(150))
filename = db.Column(db.String(150))
filepath = db.Column(db.String(150))
date_of_upload = db.Column(db.DateTime(timezone=True), default=func.now())
user_id = db.Column(db.Integer, db.ForeignKey('user.id'))run.py
from ST_website import create_app
app = create_app()
print(app)
if __name__ == '__main__':
app.run(host='0.0.0.0', debug=True)终端
D:\Project\venv\lib\site-packages\flask_sqlalchemy\__init__.py:851: UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:".
warnings.warn(
D:\Project\venv\lib\site-packages\flask_sqlalchemy\__init__.py:872:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
warnings.warn(FSADeprecationWarning(
Created Database!
<Flask 'ST_website'>
* Serving Flask app 'ST_website' (lazy loading)
* Environment: production
WARNING: This is a development server. Do not use it in a production deployment.
Use a production WSGI server instead.
* Debug mode: on
* Restarting with stat
D:\Project\venv\lib\site-packages\flask_sqlalchemy\__init__.py:851: UserWarning: Neither SQLALCHEMY_DATABASE_URI nor SQLALCHEMY_BINDS is set. Defaulting SQLALCHEMY_DATABASE_URI to "sqlite:///:memory:".
warnings.warn(
D:\Project\venv\lib\site-packages\flask_sqlalchemy\__init__.py:872:FSADeprecationWarning: SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and will be disabled by default in the future. Set it to True or False to suppress this warning.
warnings.warn(FSADeprecationWarning(
* Debugger is active!
* Debugger PIN: 412-312-823
Created Database!
<Flask 'ST_website'>
* Running on all addresses.
WARNING: This is a development server. Do not use it in a production deployment.
* Running on http://192.168.1.10:5000/ (Press CTRL+C to quit)发布于 2022-01-10 17:03:21
在init.py文件中,请更改这一行。
app.config['SQLALCHEMY_DATABASE_URL'] = 'sqlite:///ST_database.db'到,
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///ST_database.db'https://stackoverflow.com/questions/68449242
复制相似问题