2016-12-29 26 views
0

我試圖讓Flask連接到本地MySQL,但是這一直失敗。我已經基於其他論壇試圖使用Flask-mysql與python

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

錯誤,我得到嘗試多種組合如下

ImportError: No module named flask.ext.mysql 

運行Windows 10 & Python3.5 我已經安裝了點子flask- MySQL

********** 編輯 ***********

在用戶@metmirr(謝謝!!!)的幫助下,解決方案是使用虛擬環境,無法以其他方式使用它!答案和評論已被刪除

回答

0

使用mysql.connector從Flask訪問MySQL。欲瞭解更多details請訪問鏈接。

導入MySQL連接

import mysql.connector 

連接到本地數據庫

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

訪問數據庫

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