In this tutorial, how to create a python flask with MySQL database. so let's get started.


Python Flask with Mysql Database
 

SQL 


SQL is the access and manipulates databases system used to require RDMS like MySQL, Microsoft SQL Server, PostgreSQL, etc. 

It's used for all users and easy to learn and the perform various tasks such as: 

  • creating tables 
  • CRUD operations (Create,Read,Update,Delete) 

Here, I will work with PHPMyAdmin. Xampp software is the familiar to use in Cpanel most and provides a web interface. 

Just download from google or click here to download 

After the installation of Xampp.we need two things to start: Apache and MySQL .one thing you have to know is MySQL port is 3306, some time python is not allowed to run Xampp.
Before the coding on the Xampp server and know to create the database in PHPMyAdmin
here, I already created the database and named it the PHP tutorial. 
 
Next, Let's install the flask_mysqldb connector to use MySQL to run the package :


pip install flask-mysqldb


Flask Header:



from flask import Flask, render_template, request
from flask_mysqldb import MySQL


setting up a Flask MySQL database:



app = Flask(__name__)

app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'phptutorial'

mysql = MySQL(app)



Implement code to fetch data from Database:



@app.route('/')
def home_page():
    table = "select * from student"
    cursor = mysql.connection.cursor()
    cursor.execute(table)
    result = cursor.fetchall()
    return render_template('home.html', result=result)
    mysql.connection.commit()
    cursor.close()


Request Method To INSERT Data:




@app.route('/add', methods=['POST', 'GET'])
def add():
    if request.method == 'POST':
        name = request.form['name']
        dept = request.form['dept']
        cursor = mysql.connection.cursor()
        result = cursor.execute(''' INSERT INTO student (student_name,dept_name) 
        VALUES (%s,%s)''', (name, dept))
        mysql.connection.commit()
        cursor.close()
        return "insert successfully"


Home.html



<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8">
    <title>Title</title>
</head>
<body>
<form action="/add" method = "POST">
   <p>Student Name <input type = "text" name = "name" /></p>
   <p>Department Name <input type = "text" name = "dept" /></p>
   <p><input type = "submit" value = "Submit" /></p>
</form>
<table border="1">

    <th>Id</th>
    <th>Student Name</th>
    <th>Department Name</th>
    {% for i in result %}
    <tr>
        <td>{{i[0]}}</td>
        <td>{{i[1]}}</td>
        <td>{{i[2]}}</td>
    </tr>
    {% endfor %}
</table>
</body>
</html>


Python Flask with Mysql Database


Flask Full Coding:




from flask import Flask, render_template, request
from flask_mysqldb import MySQL

app = Flask(__name__)

app.config['MYSQL_HOST'] = 'localhost'
app.config['MYSQL_USER'] = 'root'
app.config['MYSQL_PASSWORD'] = ''
app.config['MYSQL_DB'] = 'phptutorial'

mysql = MySQL(app)


@app.route('/')
def home_page():
    table = "select * from student"
    cursor = mysql.connection.cursor()
    cursor.execute(table)
    result = cursor.fetchall()
    return render_template('home.html', result=result)
    mysql.connection.commit()
    cursor.close()


@app.route('/add', methods=['POST', 'GET'])
def add():
    if request.method == 'POST':
        name = request.form['name']
        dept = request.form['dept']
        cursor = mysql.connection.cursor()
        result = cursor.execute(''' INSERT INTO student (student_name,dept_name) 
        VALUES (%s,%s)''', (name, dept))
        mysql.connection.commit()
        cursor.close()
        return "insert successfully"




Python Flask with Mysql Database

 Read also: Python Flask tutorial Setting up a Flask and Sql Database

1 Comments

  1. This comment has been removed by a blog administrator.

    ReplyDelete

Post a Comment

Previous Post Next Post