Connect to MSSQL Database using Flask-SQLAlchemy

Connect to MSSQL Database using Flask-SQLAlchemy,第1张

Connect to MSSQL Database using Flask-SQLAlchemy

所以我遇到了一个非常类似的问题,通过做
跟随。
遵循[SQL炼金术]
[文件](http://docs.sqlalchemy.org/en/latest/dialogs/mssql.html#模块-
sqlalchemy.dialones.mssql语言.pyodbc)我发现我可以使用我的pyodbc连接
字符串如下:

# Python 2.ximport urllibparams = urllib.quote_plus("DRIVER={SQL Server Native Client 10.0};SERVER=dagger;DATAbase=test;UID=user;PWD=password")engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)# Python 3.ximport urllibparams = urllib.parse.quote_plus("DRIVER={SQL Server Native Client 10.0};SERVER=dagger;DATAbase=test;UID=user;PWD=password")engine = create_engine("mssql+pyodbc:///?odbc_connect=%s" % params)# using the above logic I just did the followingparams = urllib.parse.quote_plus('DRIVER={SQL Server};SERVER=HARRISONS-THINK;DATAbase=LendApp;Trusted_Connection=yes;')app.config['SQLALCHEMY_DATAbase_URI'] = "mssql+pyodbc:///?odbc_connect=%s" % params

This then caused an additional error because I was also using Flask-Migrate
and apparently it doesn’t like % in the connection URI. So I did some more
digging and found this post.
I then changed the following line in my

./migrations/env.py
file

From:

from flask import current_appconfig.set_main_option('sqlalchemy.url',        current_app.config.get('SQLALCHEMY_DATAbase_URI'))

To:

from flask import current_appdb_url_escaped = current_app.config.get('SQLALCHEMY_DATAbase_URI').replace('%', '%%')config.set_main_option('sqlalchemy.url', db_url_escaped)

在做了所有这些之后,我能够进行迁移,一切看起来都像
它现在工作正常。



欢迎分享,转载请注明来源:内存溢出

原文地址: http://outofmemory.cn/zaji/5630618.html

(0)
打赏 微信扫一扫 微信扫一扫 支付宝扫一扫 支付宝扫一扫
上一篇 2022-12-15
下一篇 2022-12-16

发表评论

登录后才能评论

评论列表(0条)

保存