/CRUD_Flask_MySQL

Basic CRUD with Python Flask & MySQL Database

Primary LanguagePython

simplinnovation

Basic CRUD: Flask & MySQL

  1. Activate MySQL server:

    $ cd C:\Program Files\MySQL\MySQL Server 8.0\bin
    $ mysql.exe -u <username> -p<password>
    $ mysql -u <username> -p

    On my case:

    $ mysql.exe -u lintang -p12345
    $ mysql -u lintang -p   

  1. Create a database & table on MySQL. I'll use a database called "lin_flask" & a table called "users":

    mysql>  CREATE DATABASE lin_flask;
    mysql>  USE lin_flask;
    mysql>  CREATE TABLE users (
                id int auto_increment,
                name varchar(100) not null,
                age tinyint,
                primary key (id)
            );

  1. Clone this repo. Edit database.yaml file according to your database configuration, then install all the packages needed. In this project I'm using flask, flask_cors & flask_mysqldb:

    $ git clone https://github.com/LintangWisesa/CRUD_Flask_MySQL.git
    $ cd CRUD_Flask_MySQL

    Install dependencies:

    $ pip install flask flask_cors flask_mysqldb
    $ py -m pip install flask flask_cors flask_mysqldb

  1. Run the server file. Make sure your MySQL server is still running. Your application server will run locally at http://localhost:5000/ :
    $ py app.py
    $ python app.py
    $ python3 app.py

  1. Give a request to the server. You can use Postman app:

    See the opening screen (home.html)

    GET /

    Post a data to database:

    POST /data
    body request: {name:"x", age:"y"}

    Get all data & specific data by id:

    GET /data
    GET /data/{:id}

    Update a data by id:

    PUT /data/{:id}
    body request: {name:"x", age:"y"}

    Delete a data by id:

    DELETE /data/{:id}

  1. Enjoy your code! 😎👌

See also:

Lintang Wisesa 💌 lintangwisesa@ymail.com

Facebook | Twitter | Google+ | Youtube | :octocat: GitHub | Hackster