【发布时间】:2020-11-10 23:55:00
【问题描述】:
我目前正在使用 Flask 创建一个 Web 应用程序。目前我的主要问题是理解连接到数据库的概念,因为网上有很多资源让我在建立与数据库的可靠连接方面感到困惑。据我所知,SQL 的语法不是问题。
我选择使用 SQLite 方言的 SQLAlchemy 而不是 MySQL、PostgresSQL 等。 我的第一个问题是:在使用 SQLAlchemy 时选择方言是否必要?我们可以不按原样使用 SQLAlchemy 吗?
第二个问题:我在网上看到了许多示例和教程,使用“phpMyAdmin”或类似的东西,在他们的localhost 浏览器中以可视化和交互式的方式处理他们的数据库(关系)。是否需要在为任何类型的项目创建任何类型的数据库连接之前进行设置?
第二个问题(扩展):设置pypMyAdmin,有教程如“https://www.youtube.com/watch?v=hVHFPzjp064&t=238s”表示激活apache,激活PHP,并下载MySQL 以使用工作台。如第二个问题所述,这些步骤是强制性的吗?因为许多教程似乎没有说明如何设置。
第三个问题:由于我的项目增长缓慢,我正在使用“关注点分离”的概念。我的文件树如下:
经过研究,我认为我应该在__init__.py 文件中包含与数据库相关的代码?另外,当然要使用必要的配置更新配置文件?我不明白的是,用于连接数据库的语法。以下代码将在上述两个文件中显示我的代码:
__init__.py
# This class will ultimately bring our entire application together.
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
# Creating Flask app.
app = Flask(__name__)
# Creating a database object which represents the database.
# Created a migration object which represents the migration engine.
db = SQLAlchemy(app)
migrate = Migrate(app, db)
# TODO Explain reasons for using this method:
# Using method to determine Flask environment from the following link:
# https://www.youtube.com/watch?v=GW_2O9CrnSU&t=366s
if app.config["ENV"] == "production":
app.config.from_object("config.ProductionConfig")
elif app.config["ENV"] == "testing":
app.config.from_object("config.TestingConfig")
else:
app.config.from_object("config.DevelopmentConfig")
# Importing views file to avoid circular import.
from app import views
from app import admin_views
from app import routes, models
config.py
# This class contains important information regarding the conifgurations for this application.
# It is good practice to keep configurations of the application in a seperate file. This enforces the
# practice of 'seperation of concerns'.
# There is a main class "Config" which has subclasses as illustrated below. The configuration settings
# are defined as class variables within the 'Config' class. As the application grows, we can create subclasses.
import os
basedir = os.path.abspath(os.path.dirname(__file__))
# The SECRET_KEY is important as it...
class Config(object):
DEBUG = False
TESTING = False
SECRET_KEY = '\xb6"\xc5\xce\xc2D\xd1*\x0c\x06\x83 \xbc\xdbM\x97\xe2\xf4OZ\xdc\x16Jv'
# The SQLAlchemy extension is connecting the location of the database from the URI variable.
# The fallback value if the value is not defined is given below as the URL.
SQLALCHEMY_DATABASE_URI = os.environ.get('DATABASE_URL') or \
'sqlite:///' + os.path.join(basedir, 'app.db')
# The 'modifications' config option is set to false as it prevents a signal from appearing whenever
# there is a change made within the database.
SQLALCHEMY_TRACK_MODIFICATIONS = False
class ProductionConfig(Config):
pass
class DevelopmentConfig(Config):
DEBUG = True
class TestingConfig(Config):
TESTING = True
如果我的问题似乎无处不在,我深表歉意。我研究得越多,我就越对能否成功连接到数据库感到困惑。
如果有人能以“易于理解”的方式回答我的问题,我将不胜感激。
【问题讨论】:
-
您好,欢迎来到 StackOverflow。我觉得你的问题太笼统了,请阅读stackoverflow.com/help/how-to-ask
-
好吧,假设我有以下代码:
from sqlalchemy import create_engineengine = create_engine('sqlite:///etc-etc-etc-etc)如何在引擎函数中确定连接到数据库的连接流?
标签: database flask flask-sqlalchemy