简体   繁体   English

试图将Flask-mysql与python结合使用

[英]Trying to use Flask-mysql with python

I'm trying to get Flask to connect to a local MySQL but this keeps failing. 我正在尝试让Flask连接到本地MySQL,但这一直失败。 I have tried multiple combinations based on other forums 我已经根据其他论坛尝试了多种组合

from flask.ext.mysql import MySQL
from flask_mysql import MySQL
from flaskext.mysql import MySQL

etc. 等等

the error I get is the following 我得到的错误是以下

ImportError: No module named flask.ext.mysql

Running Windows 10 & Python3.5 I have pip installed flask-MySQL 运行Windows 10和Python3.5我已经安装了pip flask-MySQL

********** EDIT *********** ********** 编辑 ***********

With the help of the user @metmirr (Thank you!!!) the fix was to use a virtual environment, couldn't get it to work any other way! 在用户@metmirr的帮助下(谢谢!!),解决方法是使用虚拟环境,无法使其以任何其他方式工作! The answer and comments have been removed somehow 答案和评论已被删除

Use mysql.connector for accessing MySQL from Flask. 使用mysql.connector从Flask访问MySQL。 For more details visit the link. 有关更多详细信息,请访问链接。

Import MySQL Connector 导入MySQL连接器

import mysql.connector

Connect to local database 连接到本地数据库

def getMysqlConnection():
    return mysql.connector.connect(host='localhost',database='test',user='root',password='root')

To access the database 访问数据库

db = getMysqlConnection()
sqlstr = "select * from test_table"
cur = db.cursor()
cur.execute(sqlstr)
output_json = cur.fetchall()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM