简体   繁体   中英

how use mysql.connector in python-flask

I am trying to connect mysql database with python-flask app. My app.py looks like this

app = Flask(__name__, template_folder='templates', static_folder='static')

#config Database
connection = mysql.connector.connect(
        user = 'localhost',
        password = '',
        host='127.0.0.1',
        port = 3306,
        database = 'test'
)

@app.route('/', methods=['GET', 'POST'])
def index():
    cursor = connection.cursor()
    cursor.execute("SELECT name, rating, number, type, image from place")
    data = cursor.fetchall()
    cursor.close()
    connection.close()
    return render_template('index.html', data = data)

if __name__ == '__main__':
    app.run(host='0.0.0.0')

But when I browse it "Unable to connect". MySql sever is on. php 我的管理浏览器,带有测试数据库和位置表

In the terminal it shows

  • Serving Flask app "app" (lazy loading)
  • Environment: production
  • WARNING: This is a development server. Do not use it in a production deployment. Use a production WSGI server instead.
  • Debug mode: off
  • Running on http://0.0.0.0:5000/ (Press CTRL+C to quit)

You are running "Pyhton app.py" in terminal. It needs to run "Flask run" in terminal

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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