展开
拓展 关闭
订阅号推广码
GitHub
视频
公告栏 关闭

flask连接mysql

  • 安装依赖
(C:\ProgramData\Anaconda3) C:\Users\ychen>pip install pymysql
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Requirement already satisfied: pymysql in c:\programdata\anaconda3\lib\site-packages (1.0.2)

(C:\ProgramData\Anaconda3) C:\Users\ychen>pip install flask-sqlalchemy
Looking in indexes: https://mirrors.aliyun.com/pypi/simple/
Collecting flask-sqlalchemy
  Downloading https://mirrors.aliyun.com/pypi/packages/26/2c/9088b6bd95bca539230bbe9ad446737ed391aab9a83aff403e18dded3e75/Flask_SQLAlchemy-2.5.1-py2.py3-none-any.whl (17 kB)
Requirement already satisfied: SQLAlchemy>=0.8.0 in c:\programdata\anaconda3\lib\site-packages (from flask-sqlalchemy) (1.1.13)
Requirement already satisfied: Flask>=0.10 in c:\programdata\anaconda3\lib\site-packages (from flask-sqlalchemy) (2.0.1)
Requirement already satisfied: click>=7.1.2 in c:\programdata\anaconda3\lib\site-packages (from Flask>=0.10->flask-sqlalchemy) (8.0.4)
Requirement already satisfied: itsdangerous>=2.0 in c:\programdata\anaconda3\lib\site-packages (from Flask>=0.10->flask-sqlalchemy) (2.0.1)
Requirement already satisfied: Jinja2>=3.0 in c:\programdata\anaconda3\lib\site-packages (from Flask>=0.10->flask-sqlalchemy) (3.0.3)
Requirement already satisfied: Werkzeug>=2.0 in c:\programdata\anaconda3\lib\site-packages (from Flask>=0.10->flask-sqlalchemy) (2.0.3)
Requirement already satisfied: colorama in c:\programdata\anaconda3\lib\site-packages (from click>=7.1.2->Flask>=0.10->flask-sqlalchemy) (0.4.5)
Requirement already satisfied: importlib-metadata in c:\programdata\anaconda3\lib\site-packages (from click>=7.1.2->Flask>=0.10->flask-sqlalchemy) (4.8.3)
Requirement already satisfied: MarkupSafe>=2.0 in c:\programdata\anaconda3\lib\site-packages (from Jinja2>=3.0->Flask>=0.10->flask-sqlalchemy) (2.0.1)
Requirement already satisfied: dataclasses in c:\programdata\anaconda3\lib\site-packages (from Werkzeug>=2.0->Flask>=0.10->flask-sqlalchemy) (0.8)
Requirement already satisfied: typing-extensions>=3.6.4 in c:\programdata\anaconda3\lib\site-packages (from importlib-metadata->click>=7.1.2->Flask>=0.10->flask-sqlalchemy) (4.1.1)
Requirement already satisfied: zipp>=0.5 in c:\programdata\anaconda3\lib\site-packages (from importlib-metadata->click>=7.1.2->Flask>=0.10->flask-sqlalchemy) (3.6.0)
Installing collected packages: flask-sqlalchemy
Successfully installed flask-sqlalchemy-2.5.1
  • app.py
from flask import Flask, render_template
from flask_sqlalchemy import SQLAlchemy

app = Flask(__name__)

HOSTNAME = "127.0.0.1"
PORT = 3306
USERNAME = "root"
PASSWORD = "123456"
DATABASE = "db_test"
app.config['SQLALCHEMY_DATABASE_URI'] = f"mysql+pymysql://{USERNAME}:{PASSWORD}@{HOSTNAME}:{PORT}/{DATABASE}?charset=utf8mb4"

db = SQLAlchemy(app)

with app.app_context():
    with db.engine.connect() as conn:
        rs = conn.execute("select 1")
        print(rs.fetchone())

@app.route('/')
def hello_world():
    return 'Hello World!'

if __name__ == '__main__':
    app.run()
  • 执行报错
sqlalchemy.exc.OperationalError: (pymysql.err.OperationalError) (1193, "Unknown system variable 'tx_isolation'")
  • 解决方案:升级sqlalchemy
(C:\ProgramData\Anaconda3) C:\Users\ychen>pip install -i https://pypi.tuna.tsinghua.edu.cn/simple --upgrade sqlalchemy --ignore-installed
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting sqlalchemy
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/52/e5/4b607c8f8a0eb3c0b3678f41d78e905ea9008a4ebbf13f53eff29047835b/SQLAlchemy-1.4.52-cp36-cp36m-win_amd64.whl (1.6 MB)
     |████████████████████████████████| 1.6 MB 930 kB/s
Collecting greenlet!=0.4.17
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a3/6c/dde49c63ab2f12d2ce401620dbe1a80830109f5f310bdd2f96d2e259de37/greenlet-2.0.2-cp36-cp36m-win_amd64.whl (197 kB)
     |████████████████████████████████| 197 kB 2.2 MB/s
Collecting importlib-metadata
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/a0/a1/b153a0a4caf7a7e3f15c2cd56c7702e2cf3d89b1b359d1f1c5e59d68f4ce/importlib_metadata-4.8.3-py3-none-any.whl (17 kB)
Collecting typing-extensions>=3.6.4
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/45/6b/44f7f8f1e110027cf88956b59f2fad776cca7e1704396d043f89effd3a0e/typing_extensions-4.1.1-py3-none-any.whl (26 kB)
Collecting zipp>=0.5
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/bd/df/d4a4974a3e3957fd1c1fa3082366d7fff6e428ddb55f074bf64876f8e8ad/zipp-3.6.0-py3-none-any.whl (5.3 kB)
Installing collected packages: zipp, typing-extensions, importlib-metadata, greenlet, sqlalchemy
ERROR: pip's dependency resolver does not currently take into account all the packages that are installed. This behaviour is the source of the following dependency conflicts.
tensorflow 2.6.2 requires typing-extensions~=3.7.4, but you have typing-extensions 4.1.1 which is incompatible.
Successfully installed greenlet-2.0.2 importlib-metadata-4.8.3 sqlalchemy-1.4.52 typing-extensions-4.1.1 zipp-3.6.0
  • 执行成功,控制台输出
C:\ProgramData\Anaconda3\lib\site-packages\flask_sqlalchemy\__init__.py:873: 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.
  'SQLALCHEMY_TRACK_MODIFICATIONS adds significant overhead and '
(1,)
posted @ 2024-03-20 09:22  DogLeftover  阅读(67)  评论(0编辑  收藏  举报