我正在尝试将我的 Flask 应用程序与 MySQL 数据库连接。这是 MySQL 数据
mysql> select user();
+----------------+
| user() |
+----------------+
| root@localhost |
+----------------+
1 row in set (0.01 sec)
mysql> SHOW DATABASES;
+--------------------+
| Database |
+--------------------+
| alchemy |
| information_schema |
| mysql |
| performance_schema |
| sys |
+--------------------+
5 rows in set (0.00 sec)
mysql> SHOW GLOBAL VARIABLES LIKE 'PORT';
+---------------+-------+
| Variable_name | Value |
+---------------+-------+
| port | 3306 |
+---------------+-------+
1 row in set (0.01 sec)
这是init.py
from flask import Flask
from config import Config
from flask_sqlalchemy import SQLAlchemy
from flask_migrate import Migrate
app = Flask(__name__)
app.config.from_object(Config)
db = SQLAlchemy(app)
migrate = Migrate(app, db)
from app import routes, models
这是config.py
import os
basedir = os.path.abspath(os.path.dirname(__file__))
class Config(object):
SECRET_KEY = os.environ.get('SECRET_KEY') or 'test-key-motherfucker'
SQLALCHEMY_DATABASE_URI = 'mysql://root@127.0.0.1:3360/alchemy'
SQLALCHEMY_TRACK_MODIFICATIONS = False
和 models.py
from app import db
class Test(db.Model):
id = db.Column(db.Integer, primary_key=True)
name = db.Column(db.String(64), index=True, unique=True)
def __repr__(self):
return '<Test {}>'.format(self.name)
当我尝试初始化数据库时
flask db init
flask db upgrade
我收到
sqlalchemy.exc.NoSuchModuleError: Can't load plugin: sqlalchemy.dialects:mysql
我尝试使用 pymysql SQLALCHEMY_DATABASE_URI = 'mysql+pymysql://rootl@127.0.0.1:3360/alchemy'
如何将mysql与sqlalchemy连接?
aluckdog
相关分类