**# PYTHON to SQL CONNECTION
# Connection Type 1**
# install these 2 libraries
!pip install sqlalchemy
!pip install PyMySQL
# import the essencials libraries
import pymysql
from sqlalchemy import create_engine
import pandas as pd
import getpass
# Password library to encrypt your password
password = getpass.getpass()
# Creating Variable for connection (EXAMPLE from SQL Schema 'bank')
connection_string = 'mysql+pymysql://root:' + password + '@localhost/bank'
engine = create_engine(connection_string)
data = pd.read_sql_query('SELECT * FROM loan', engine)
**# Connection Type 2**
# install these library
!pip install mysql-connector-python
# import library
import mysql.connector
# Setting up as a variable the connection setup
cnx = mysql.connector.connect( user = 'root',
password = password,
host = '127.0.0.1')
# This string is to show true or false if the connection works
cnx.is_connected()
# A diferent type of pushing the output like connection type 1
cursor = cnx.cursor()
cursor.execute('SELECT * FROM bank.loan')
result = pd.Dataframe(cursor.fetchall())
result.columns = [head[0] for head in cursor.description]
result.head()
Python / SQL CREATE
Python / SQL INSERT