Python:如何在Windows计算机上运行flask mysqldb? [英] Python: How to run flask mysqldb on Windows machine?

查看:235
本文介绍了Python:如何在Windows计算机上运行flask mysqldb?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我已经在Windows machine上安装了带有pip软件包管理系统的flask-mysqldb模块,但我不知道如何运行它.

I've installed the flask-mysqldb module with pip package management system on my Windows machine and I don't know how to run it.

我试图在系统属性中添加到MySQLdb的路径,但仍然没有.

I have tried to add the path to the MySQLdb in System properties and still nothing.

推荐答案

在Windows中使用Flask和MySQL时,我使用

When I work with Flask and MySQL in Windows, I use XAMPP that includes MariaDB, PHP, and Perl. I run the XAMPP server and start MySQL service.

我使用 Flask-MySQL ,可以使用pip install Flask-MySQL安装.

I use Flask-MySQL which can be installed using pip install Flask-MySQL.

然后我从XAMPP附带的phpMyAdmin创建数据库.在Flask应用中,我声明了连接并操作了数据库.

Then I create database from phpMyAdmin which comes with XAMPP. In Flask app, I declare the connection and manipulate the database.

以下是此Python文件中有关如何在MySQL中使用Flask的演示:

Here is a demonstration of how to use Flask with MySQL in this Python file:

from flask import Flask
from flask import request
from flask import render_template
from flaskext.mysql import MySQL

app = Flask(__name__)

mysql = MySQL()
app.config['MYSQL_DATABASE_USER'] = 'root'
app.config['MYSQL_DATABASE_PASSWORD'] = ''
app.config['MYSQL_DATABASE_DB'] = 'matrimony'
app.config['MYSQL_DATABASE_HOST'] = 'localhost'
mysql.init_app(app)

@app.route('/')
def index():
    return render_template('index.html')

@app.route('/create_table', methods=['POST'])
def create_table():
    if request.method=="POST":
        try:
            table_name = request.form.get('table_name')
            field_name_list = request.form.getlist('fields[]')
            field_list = []
            for field in field_name_list:
                field_list.append(field+ " VARCHAR(50) DEFAULT NULL")
            field_query = " ( " + ", ".join(field_list) + " ) "
            create_table_query = 'CREATE TABLE `'+table_name+'`' + field_query
            conn = mysql.connect()
            cursor = conn.cursor()
            cursor.execute(create_table_query)
            return "Table: "+table_name+" created successfully"
        except Exception as e:
            return str(e)

if __name__ == '__main__':
    app.run(debug = True) 

在此示例中,我从用户输入创建了带有用户提到的列名的MySQL表.

In this example, I created MySQL table from user input with user mentioned column names.

这篇关于Python:如何在Windows计算机上运行flask mysqldb?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆