Posts

Showing posts with the label MySQL

USER MANAGEMENT USING FLASK FRAMEWORK AND MySQL DATABASE IN PYTHON

Image
Package Installation :   pip install flask pip install flask-mysqldb Program : # User Management in python using Flask framework and MySQL Database from flask import Flask , render_template , url_for , request , redirect , flash from flask_mysqldb import MySQL app = Flask(__name__) # This takes the filename as a parameter # MySQL Connection app.config[ "MYSQL_HOST" ] = "localhost" app.config[ "MYSQL_USER" ] = "root" # Database Username app.config[ "MYSQL_PASSWORD" ] = "Enter the password" # Database Password app.config[ "MYSQL_DB" ] = "crud" # Database Name app.config[ "MYSQL_CURSORCLASS" ] = "DictCursor" mysql = MySQL(app) # Loading Home Page @app.route ( "/" ) # Home Page def home (): cursor = mysql.connection.cursor() # SELECT Query query = "SELECT * FROM users" cursor.execute(query) result = cursor.fetchall() return render_template...

MYSQL DATABASE CONNECTIVITY IN PYTHON

Image
MySQL Database Connectivity :  First you need to setup Apache , phpMyAdmin and MySQL.  To know the setup procedure  click here . As a example here I am using  Users  for table name and  Id ,  Name ,  Age ,  City   for field names. The below program shows  CRUD  operation in MySQL using python. C - Create (INSERT) R - Read (SELECT) U - Update (UPDATE) D - Delete (DELETE) Program :  # MySQL DATABASE CONNECTIVITY import mysql.connector # presenting output in tabular view from tabulate import tabulate # connecting the database file to MySQL connection = mysql.connector.connect( host = "localhost" , user = "enter username" , password = "enter password" , database = "enter database file name" ) cursor = connection.cursor() # function to INSERT data def insert_data (name , age , city): # INSERT query query = "INSERT INTO Users (name, age, city) values (%s, %s, %s)" # to execute the query cursor.execute(query , (n...