新手求教 python3 用 flask-sqlalchemy pymysql 连接 mysql

\u5728python3\u4e0b\u600e\u6837\u7528flask-sqlalchemy\u5bf9mysql\u6570\u636e\u5e93\u64cd\u4f5c

\u8fd9\u4e2a\u95ee\u9898\u7ecf\u5e38\u96be\u9053\u65b0\u624b\u4e00\u4e0b\uff0c\u56e0\u4e3a\u5927\u90e8\u5206\u6559\u7a0b\u91cc\uff08\u5305\u62ec\u7ecf\u5178\u7684\u300aFlask
Web\u5f00\u53d1\u300b\u4e00\u4e66\uff09\uff0c\u544a\u8bc9\u4e86\u6211\u4eec\u5982\u4f55\u4f7f\u7528flask-sqlalchemy\u64cd\u4f5csqlite\uff0c\u4f46\u5728\u751f\u4ea7\u73af\u5883\uff08\u7ebf\u4e0a\u7f51\u7ad9\uff09\u4e0a\uff0c\u6211\u4eec\u80af\u5b9a\u662f\u4f7f\u7528MySQL\u6216\u5176
\u4ed6\uff0c\u800c\u5927\u90e8\u5206\u7684\u6559\u7a0b\u91cc\uff0c\u53c8\u544a\u8bc9\u6211\u4eecflask-sqlalchemy\u4f7f\u7528MySQL\u7684\u65b9\u5f0f\u662f\uff1a
mysql://username:password@server/db

\u7ed3\u679c\u6211\u4eec\u7167\u846b\u82a6\u753b\u74e2\u7684\u6765\u4e00\u4e0b\uff0c\u53d1\u73b0\u538b\u6839\u4e0d\u884c\uff0c\u5199\u597d\u7684\u7f51\u7ad9\u4e00\u8ddf\u6570\u636e\u5e93\u6cbe\u8fb9\u5c31\u62a5\u9519\u3002

Python\u548cMySQL\u662f\u300c\u4e24\u4e2a\u56fd\u5bb6\u7684\u4eba\u300d\uff0c\u4ed6\u4eec\u4e92\u4e0d\u76f8\u901a\uff0c\u56e0\u800c\u9700\u8981\u4e00\u4e2a\u4e2d\u95f4\u4ee3\u7406\uff0c\u8ba9\u53cc\u65b9\u4e92\u901a\u6709\u65e0\uff0c\u8ddf\u7ffb\u8bd1\u4e00\u6837\uff08\u8fd9\u6bd4\u55bb\u4e0d\u51c6\u786e\uff0c\u4f46\u8db3\u591f\u4f60\u660e\u767d\u610f\u601d\u5c31\u884c\uff09\u3002\u7ffb\u8bd1\u53c8\u6709\u5f88\u591a\u9009\u62e9\uff0c\u4e0d\u540c\u7684\u7ffb\u8bd1\u5404\u6709\u7279\u8272\u3002
\u9898
\u4e3b\u89e3\u51b3\u95ee\u9898\u9009\u62e9\u7684\u7ffb\u8bd1\u662f\u300cflask-mysqldb\u300d\uff0c\u5176\u80cc\u540e\u7684\u4e3b\u5b50\u662f\u300cMySQL-python\u300d\u3002\u6069\uff0c\u8bf4\u5230\u8fd9\u91cc\u4f60\u5e94\u8be5\u77e5\u9053\uff0c\u300cflask-xxx\u300d
\u8fd9\u6837\u7684\u5305\u90fd\u662f\u5bf9\u80cc\u540e\u4e3b\u5b50\u8fdb\u884c\u4e86\u9002\u5408Flask\u5c01\u88c5\u7684\u63d2\u4ef6\uff0c\u8ddf\u5305\u5b50\u76ae\u4e00\u6837\uff0c\u91cc\u9762\u7684\u9985\u624d\u662f\u91cd\u70b9\uff0c\u300cflask-mysqldb\u300d\u7684\u9985\u662f\u300cMySQL-
python\u300d\u3002

\u800c\u6211\u8981\u63a8\u8350\u7684\u662f\u53e6\u4e00\u4e2a\u7ffb\u8bd1\uff1aPyMySQL\uff0c\u8fd9\u73a9\u610f\u7684\u597d\u5904\u662f\u53ef\u4ee5\u505a\u5f02\u6b65\uff08\u300cMySQL-python\u300d\u4e5f\u53ef\u4ee5\uff0c\u4e2a\u4eba\u53e3\u5473\u7f62\u4e86\uff09\uff0c\u7b80\u800c\u8a00\u4e4b\uff0c\u7f51\u7ad9\u8bbf\u95ee\u91cf\u5927\u4e86\u5c31\u9700\u8981\u8003\u8651\u5f02\u6b65\uff0c\u73b0\u5728\u522b\u7ba1\u8fd9\u662f\u5565\u5b50\u3002\u8fd9\u73a9\u610f\u7684\u5b89\u88c5\u65b9\u5f0f\u662f\uff1a
pip install PyMySQL
\u4e4b\u540e\uff0c\u6570\u636e\u5e93\u8fde\u63a5\u7531\uff1a
mysql://username:password@server/db

\u6539\u4e3a
mysql+pymysql://username:password@server/db

\u5c31\u53ef\u4ee5\u4e86\u3002

\u8fd9\u4e2a\u95ee\u9898\u7ecf\u5e38\u96be\u9053\u65b0\u624b\u4e00\u4e0b\uff0c\u56e0\u4e3a\u5927\u90e8\u5206\u6559\u7a0b\u91cc\uff08\u5305\u62ec\u7ecf\u5178\u7684\u300aFlask Web\u5f00\u53d1\u300b\u4e00\u4e66\uff09\uff0c\u544a\u8bc9\u4e86\u6211\u4eec\u5982\u4f55\u4f7f\u7528flask-sqlalchemy\u64cd\u4f5csqlite\uff0c\u4f46\u5728\u751f\u4ea7\u73af\u5883\uff08\u7ebf\u4e0a\u7f51\u7ad9\uff09\u4e0a\uff0c\u6211\u4eec\u80af\u5b9a\u662f\u4f7f\u7528MySQL\u6216\u5176\u4ed6\uff0c\u800c\u5927\u90e8\u5206\u7684\u6559\u7a0b\u91cc\uff0c\u53c8\u544a\u8bc9\u6211\u4eecflask-sqlalchemy\u4f7f\u7528MySQL\u7684\u65b9\u5f0f\u662f\uff1a
mysql://username:password@server/db
\u7ed3\u679c\u6211\u4eec\u7167\u846b\u82a6\u753b\u74e2\u7684\u6765\u4e00\u4e0b\uff0c\u53d1\u73b0\u538b\u6839\u4e0d\u884c\uff0c\u5199\u597d\u7684\u7f51\u7ad9\u4e00\u8ddf\u6570\u636e\u5e93\u6cbe\u8fb9\u5c31\u62a5\u9519\u3002

Python\u548cMySQL\u662f\u300c\u4e24\u4e2a\u56fd\u5bb6\u7684\u4eba\u300d\uff0c\u4ed6\u4eec\u4e92\u4e0d\u76f8\u901a\uff0c\u56e0\u800c\u9700\u8981\u4e00\u4e2a\u4e2d\u95f4\u4ee3\u7406\uff0c\u8ba9\u53cc\u65b9\u4e92\u901a\u6709\u65e0\uff0c\u8ddf\u7ffb\u8bd1\u4e00\u6837\uff08\u8fd9\u6bd4\u55bb\u4e0d\u51c6\u786e\uff0c\u4f46\u8db3\u591f\u4f60\u660e\u767d\u610f\u601d\u5c31\u884c\uff09\u3002\u7ffb\u8bd1\u53c8\u6709\u5f88\u591a\u9009\u62e9\uff0c\u4e0d\u540c\u7684\u7ffb\u8bd1\u5404\u6709\u7279\u8272\u3002
\u9898\u4e3b\u89e3\u51b3\u95ee\u9898\u9009\u62e9\u7684\u7ffb\u8bd1\u662f\u300cflask-mysqldb\u300d\uff0c\u5176\u80cc\u540e\u7684\u4e3b\u5b50\u662f\u300cMySQL-python\u300d\u3002\u6069\uff0c\u8bf4\u5230\u8fd9\u91cc\u4f60\u5e94\u8be5\u77e5\u9053\uff0c\u300cflask-xxx\u300d\u8fd9\u6837\u7684\u5305\u90fd\u662f\u5bf9\u80cc\u540e\u4e3b\u5b50\u8fdb\u884c\u4e86\u9002\u5408Flask\u5c01\u88c5\u7684\u63d2\u4ef6\uff0c\u8ddf\u5305\u5b50\u76ae\u4e00\u6837\uff0c\u91cc\u9762\u7684\u9985\u624d\u662f\u91cd\u70b9\uff0c\u300cflask-mysqldb\u300d\u7684\u9985\u662f\u300cMySQL-python\u300d\u3002

\u800c\u6211\u8981\u63a8\u8350\u7684\u662f\u53e6\u4e00\u4e2a\u7ffb\u8bd1\uff1aPyMySQL\uff0c\u8fd9\u73a9\u610f\u7684\u597d\u5904\u662f\u53ef\u4ee5\u505a\u5f02\u6b65\uff08\u300cMySQL-python\u300d\u4e5f\u53ef\u4ee5\uff0c\u4e2a\u4eba\u53e3\u5473\u7f62\u4e86\uff09\uff0c\u7b80\u800c\u8a00\u4e4b\uff0c\u7f51\u7ad9\u8bbf\u95ee\u91cf\u5927\u4e86\u5c31\u9700\u8981\u8003\u8651\u5f02\u6b65\uff0c\u73b0\u5728\u522b\u7ba1\u8fd9\u662f\u5565\u5b50\u3002\u8fd9\u73a9\u610f\u7684\u5b89\u88c5\u65b9\u5f0f\u662f\uff1a
pip install PyMySQL
\u4e4b\u540e\uff0c\u6570\u636e\u5e93\u8fde\u63a5\u7531\uff1a
mysql://username:password@server/db
\u6539\u4e3a
mysql+pymysql://username:password@server/db
\u5c31\u53ef\u4ee5\u4e86\u3002

python3 用 flask-sqlalchemy pymysql 连接 mysql
这个问题经常难道新手一下,因为大部分教程里(包括经典的《Flask
Web开发》一书),告诉了我们如何使用flask-sqlalchemy操作sqlite,但在生产环境(线上网站)上,我们肯定是使用MySQL或其
他,而大部分的教程里,又告诉我们flask-sqlalchemy使用MySQL的方式是:
mysql://username:password@server/db

结果我们照葫芦画瓢的来一下,发现压根不行,写好的网站一跟数据库沾边就报错。

Python和MySQL是「两个国家的人」,他们互不相通,因而需要一个中间代理,让双方互通有无,跟翻译一样(这比喻不准确,但足够你明白意思就行)。翻译又有很多选择,不同的翻译各有特色。

主解决问题选择的翻译是「flask-mysqldb」,其背后的主子是「MySQL-python」。恩,说到这里你应该知道,「flask-xxx」
这样的包都是对背后主子进行了适合Flask封装的插件,跟包子皮一样,里面的馅才是重点,「flask-mysqldb」的馅是「MySQL-
python」。

而我要推荐的是另一个翻译:PyMySQL,这玩意的好处是可以做异步(「MySQL-python」也可以,个人口味罢了),简而言之,网站访问量大了就需要考虑异步,现在别管这是啥子。这玩意的安装方式是:
pip install PyMySQL
之后,数据库连接由:
mysql://username:password@server/db

改为
mysql+pymysql://username:password@server/db

就可以了。

扩展阅读:python3.5安装配置 ... 按住锁车键10秒自动升降窗 ... 新手求教猎镰猛蚁饲养 ... 学了python再学c++好学吗 ... python基础代码大全 ... 锁芯不归位钥匙拔不出 ... python 3.11 发布时间 ... 新手求教多肉怎么养? ... python3.8安装配置教程 ...

本站交流只代表网友个人观点,与本站立场无关
欢迎反馈与建议,请联系电邮
2024© 车视网