python with mysql code example

Example 1: how to install mysql python

pip install mysql-connector

Example 2: connect python to mysql

import mysql.connector
mydb = mysql.connector.connect(
  host="localhost",
  user="yourusername",
  password="yourpassword",
  port = 8888, #for Mamp users
  database='whatever db you want'
)
print(mydb)

Example 3: python mysql

C:\Users\Your Name\AppData\Local\Programs\Python\Python36-32\Scripts>python -m pip install 
  mysql-connector-python

Example 4: mysql connection python

from sqlalchemy import types, create_engine
import pymysql

try:
	conn = create_engine('mysql+pymysql://user:pass@IP/database_name')
  	print("MySQL Connection Sucessfull!!!!!!!!!!!")

except Exception as err:

	print("MySQL Connection Failed !!!!!!!!!!!")
	print(err)

Tags: