Python: How to run flask mysqldb on Windows machine?

2024/11/18 23:37:58

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.

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

Answer

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.

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

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

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 MySQLapp = 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_queryconn = 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) 

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

https://en.xdnf.cn/q/118616.html

Related Q&A

Match a pattern and save to variable using python

I have an output file containing thousands of lines of information. Every so often I find in the output file information of the following formInput Orientation: ... content ... Distance matrix (angstro…

Sharing a Queue instance between different modules

I am new to Python and I would like to create what is a global static variable, my thread-safe and process-safe queue, between threads/processes created in different modules. I read from the doc that t…

Square a number with functions in python [duplicate]

This question already has answers here:What does it mean when the parentheses are omitted from a function or method call?(6 answers)Closed last year.This is an extremely easy question for Python. Its…

Changing the cell name

I have a file that contains the following:NameABCD0145ABCD1445ABCD0998And Im trying to write a cod that read every row and change the name to the following format:NameABCD_145ABCD_1445ABCD_998keeping i…

Procfile Heroku

I tried to deploy my first Telegram chatbot (done with Chatterbot library) on Heroku. The files of my chatbot are: requirements (txt file) Procfile (worker: python magghybot.py) botusers (csv file) Mag…

How do i loop a code until a certain number is created?

This task is to determine the difference between two attributes, strength and skill, from game characters. The process for this is:Determining the difference between the strength attributes. The differ…

Finding the longest list in given list that contains only positive numbers in Python [closed]

Closed. This question needs to be more focused. It is not currently accepting answers.Want to improve this question? Update the question so it focuses on one problem only by editing this post.Closed l…

How to create multiple VideoCapture Objects

I wanted to create multiple VideoCapture Objects for stitching video from multiple cameras to a single video mashup.for example: I have path for three videos that I wanted to be read using Video Captur…

How to read Data from Url in python using Pandas?

I am trying to read the text data from the Url mentioned in the code. But it throws an error:ParserError: Error tokenizing data. C error: Expected 1 fields in line 4, saw 2url="https://cdn.upgrad.…

Testing multiple string in conditions in list comprehension [duplicate]

This question already has answers here:How to test multiple variables for equality against a single value?(31 answers)Closed 6 years ago.I am trying to add multiple or clauses to a python if statement…