Run MySQL and MariaDB database connection workflows directly inside Google Colab. Practice connecting via MySQL Connector and SQLAlchemy with automated database setup and error handling.
View on GitHubimport mysql.connector
my_connect = mysql.connector.connect(
host="localhost",
user="userid",
passwd="password",
database="database_name"
)
####### end of connection ####
my_conn = my_connect.cursor()
import mysql.connector
try:
my_connect = mysql.connector.connect(
host="localhost",
user="root",
passwd="test",
database="my_tutorial"
)
except mysql.connector.Error as my_error:
print(my_error.msg) # Error message
print(my_error) # With error number
####### end of connection ####
my_conn = my_connect.cursor()
####### Showing records #####
my_conn.execute("SELECT * FROM student") # SQL to execute
my_result = my_conn.fetchall()
for row in my_result:
print(row)
from sqlalchemy import create_engine
my_conn = create_engine("mysql+mysqldb://userid:password@localhost/database_name")
If you are getting this error. ( for recent drivers ) from sqlalchemy import create_engine, text
my_conn = create_engine("mysql+mysqldb://id:pw@localhost/db_name").connect()
r_set = my_conn.execute(text("SELECT * FROM student"))
for row in r_set:
print(row)
We will be using the connection variable my_conn in our scripts here. r_set = my_conn.execute(text("SHOW TABLES"))
for row in r_set:
print(row)
r_set = my_conn.execute(text("SELECT * FROM student"))
for row in r_set:
print(row)
from sqlalchemy import create_engine
my_conn = create_engine("sqlite:///my_db.db")
u_id:User id of MySQL database pw: Password for user idloclhost: Address of MySQL server database: Database of MySQL to connect
from sqlalchemy import create_engine
from sqlalchemy.exc import SQLAlchemyError
try:
my_conn = create_engine("mysql+mysqldb://u_id:pw@localhost/my_tutorial")
my_conn.connect() # error is generated at this stage
except SQLAlchemyError as e:
#print(e)
error = str(e.__dict__['orig'])
print(error)
## connection is over ####
try:
query = "SELECT * FROM student LIMIT 0, 2"
r_set = my_conn.execute(text(query))
for row in r_set:
print(row)
print('Data displayed ..')
except SQLAlchemyError as e:
#print(e)
error = str(e.__dict__['orig'])
print(error)
In modern SQLAlchemy 2.0+, you will frequently see connections managed using Python context managers (such as with engine.connect() as conn: or with engine.begin() as conn:) rather than manually calling and storing my_conn = create_engine(...).connect().
The context manager approach automatically handles closing the connection and returning it to the pool as soon as the block exits, preventing connection leaks. For write operations (like INSERT, UPDATE, and DELETE), using engine.begin() automatically commits transactions for you:
from sqlalchemy import create_engine, text
engine = create_engine("mysql+mysqldb://userid:password@localhost/database_name")
# Automatically manages connection lifecycle and commits transactions
with engine.begin() as conn:
r_set = conn.execute(text("SELECT * FROM student"))
for row in r_set:
print(row)
Author & Instructor at plus2net
I write and maintain practical tutorials on Python, PHP, SQL, JavaScript, HTML, jQuery, and web development at plus2net. The tutorials focus on clear explanations, working examples, and code that readers can test and adapt while learning.