首页
学习
活动
专区
圈层
工具
发布
社区首页 >问答首页 >为什么我不能在我的工作目录(Flask)中看到创建的数据库文件?

为什么我不能在我的工作目录(Flask)中看到创建的数据库文件?
EN

Stack Overflow用户
提问于 2021-07-20 03:31:46
回答 1查看 453关注 0票数 1

问题:我刚开始在烧瓶上创建一个网站,我的代码运行顺利,没有任何错误,但是我无法在我的工作目录或我的项目中看到数据库文件,我不知道为什么数据库文件没有在我的目录中显示或创建。有人能帮我吗?

我的目录

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
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

代码语言:javascript
复制
from ST_website import create_app
app = create_app()
print(app)

if __name__ == '__main__':
    app.run(host='0.0.0.0', debug=True)

终端

代码语言:javascript
复制
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)
EN

回答 1

Stack Overflow用户

发布于 2022-01-10 17:03:21

init.py文件中,请更改这一行。

代码语言:javascript
复制
app.config['SQLALCHEMY_DATABASE_URL'] = 'sqlite:///ST_database.db'

到,

代码语言:javascript
复制
app.config['SQLALCHEMY_DATABASE_URI'] = 'sqlite:///ST_database.db'
票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/68449242

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档