mySQL
Installation
pip install mysql-connector-python
            
Selection
import mysql.connector

# connect to database
cnx = mysql.connector.connect(user='root', password='root',
                              host='127.0.0.1',
                              port=8889,
                              database='University')

# create a cursor
cursor = cnx.cursor(buffered=True)

# create query
query = (
    "SELECT * \
    from Department")

# execute query
cursor.execute(query)

# fetch result
result = cursor.fetchall() # list of tuple

for record in result:
    print(record) # tuple

# convert result to pandas DataFrame
df = pd.DataFrame.from_records(result)
print(df)

# close connection
cnx.close()
            
Join
import mysql.connector
import pandas as pd

# connect to database
cnx = mysql.connector.connect(user='root', password='root',
                              host='127.0.0.1',
                              port=8889,
                              database='University')

# create a cursor
cursor = cnx.cursor(buffered=True)

# create query
query = ("select Department.Mgr_ssn, Dept_locations.Dlocation \
         from Dept_locations, Department \
         where Dept_locations.Dnumber = Department.Dnumber")

# execute query
cursor.execute(query)

# fetch result
df = pd.DataFrame.from_records(cursor.fetchall(), columns=['Manager_SSN', 'Department_Location'])

print(df)

# close connection
cnx.close()
            
Like
import mysql.connector
import pandas as pd

# connect to database
cnx = mysql.connector.connect(user='root', password='root',
                              host='127.0.0.1',
                              port=8889,
                              database='University')

# create a cursor
cursor = cnx.cursor(buffered=True)

# create query
query = ("SELECT Fname, Lname, Dno\
    from Employee \
    where Employee.Fname like 'J%'")

# execute query
cursor.execute(query)

# fetch result
df = pd.DataFrame.from_records(cursor.fetchall(), columns=['First', 'Last', 'Dno'])

print(df)

# close connection
cnx.close()
            
Query Parameter
import mysql.connector
import pandas as pd

# connect to database
cnx = mysql.connector.connect(user='root', password='root',
                              host='127.0.0.1',
                              port=8889,
                              database='University')

# create a cursor
cursor = cnx.cursor(buffered=True)

# create query
query = ("SELECT Fname, Lname, Dno\
    from Employee \
    where Employee.Dno = %s and Employee.Fname like %s")

# execute query
cursor.execute(query, ('5', 'J%'))

# fetch result
df = pd.DataFrame.from_records(cursor.fetchall(), columns=['First', 'Last', 'Dno'])

print(df)

# close connection
cnx.close()
            
Update
import mysql.connector
import pandas as pd

# connect to database
cnx = mysql.connector.connect(user='root', password='root',
                              host='127.0.0.1',
                              port=8889,
                              database='University')

# create a cursor
cursor = cnx.cursor(buffered=True)

# create query
sql = ("Update Employee \
    Set ssn = '123111789' \
    WHERE Fname = 'Jennifer'")

# execute query
cursor.execute(sql)

# commit
cnx.commit()

# close connection
cnx.close()
            
Reference
  • mysql
  • W3C