C:\Users\user>pip install mysql-connector-python
Without this module you will get this error message import mysql.connector
my_conn=mysql.connector.connect(
host='localhost',
user='root',
password = 'your_password',
db='my_database',
)
my_cursor = my_conn.cursor()
my_cursor.execute("SELECT * FROM student")
my_result = my_cursor.fetchall() # we get a tuple
print(my_result)
C:\Users\user>pip install PyMySQL
Here is the sample code to connect and show data from our student table using pymysql connect .
import pymysql
my_conn=pymysql.connect(
host='localhost',
user='root',
password = 'password',
db='my_tutorial',
)
my_cursor = my_conn.cursor()
my_cursor.execute("SELECT * FROM student")
my_result = my_cursor.fetchall() # we get a tuple
print(my_result)
C:\Users\user>pip install mysqlclient
Connection to MySQL using MySQLdb
import MySQLdb
my_conn=MySQLdb.connect(
host='localhost',
user='root',
password = "your_password",
db='db_name',
)
my_cursor = my_conn.cursor()
my_cursor.execute("SELECT * FROM student")
my_result = my_cursor.fetchall() # we get a tuple
print(my_result)
Note the difference in import MySQLdb but we have installed mysqlclient.
pip install pyodbc
In your system check for the driver name import pyodbc
my_conn = pyodbc.connect("DRIVER={MySQL ODBC 8.0 ANSI Driver}; \
SERVER=localhost;DATABASE=my_tutorial; UID=root; PASSWORD=your_password;")
my_cursor = my_conn.cursor()
my_cursor.execute("SELECT * FROM student LIMIT 0,10")
my_result = my_cursor.fetchall() # we get a tuple
print(my_result)
BLOB | Managing MySQL Blob Data type |
connection | Python MySQL connection string |
create | Create table by Query and showing structure |
Storing Password | Securely Store MySQL Database Credentials |
rowcount | Number of rows affected due to Query |
error | Capturing Error in MySQL Query |
Collect records | Select Query to get records from database table |
Add record | Using sqlalchemy to Insert record to database table |
Add record | Insert Query to add record to database table |
Delete Table | Drop table Query to remove table from MySQL database |
Update record | Update Query to update records of a database table |
Delete record | Delete records of a database table |
read_sql | Using MySql records to create Pandas DataFrame |
mysqlclient | Using sqlalchemy and create_engine to manage MySQL |
pickle | How to Pickle MySQL table and restore records. |
Environment
at your left side navigational menu Base root
Open Terminal
conda install -c anaconda mysql-connector-python
This will install your MySQL connector by downloading packages from the internet. Now it is time to check the connection.
from sqlalchemy import create_engine
engine = create_engine("mysql+mysqldb://userid_here:password_here@localhost/db_name")
my_conect = engine.connect()
If you are getting error message like this. pip install mysqlclient
pip show mysqlclient
pip install sqlalchemy
pip show sqlalchemy
pip install mariadb
Using SQLALchemy we can create connection string like this.
my_conn = create_engine("mariadb+mariadbconnector://root:pw@localhost/db_name")
Author
🎥 Join me live on YouTubePassionate about coding and teaching, I publish practical tutorials on PHP, Python, JavaScript, SQL, and web development. My goal is to make learning simple, engaging, and project‑oriented with real examples and source code.