0% found this document useful (0 votes)
15 views5 pages

Dbms Exp 9

Uploaded by

vunhsiv47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
15 views5 pages

Dbms Exp 9

Uploaded by

vunhsiv47
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

710722205301 ANBU SELVAN

Ex.No.9
Open Database Connectivity

Aim
To establish connection between SQL database and python using ODBC(Open Database Connection).

Definition:
 ODBC provides a standard way for programmers to create, access and manage databases regardless
of the type of database or the platform which it is running.
 ODBC is a low-level, high-performance interface that is designs specifically for relational data
stores. It aligns with the ISO/IEC for database APIs.
 ODBC accomplishes DBMS independence by using an ODBC driver as a translation layer between
the application and the DBMS. Users simply add database drivers to link the application to their
choice of DBMS.

STEPS:-
STEP1:Install Required Libraries

pip install mysql-connector-python

pip install mysql-connector-python

Collecting mysql-connector-python
Downloading mysql_connector_python-8.4.0-cp310-cp310-manylinux_2_17_x86_64.whl (19.4 MB)
━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 19.4/19.4 MB 65.6 MB/s eta 0:00:00
ysql-connector-python
Successfully installed mysql-connector-python-8.4.0

import mysql.connector

STEP2:Connect to MySQL Database

mydb = mysql.connector.connect(

22UCS406/ DATABASE MANAGEMENT SYSTEMS LABORATORY


710722205301 ANBU SELVAN
host="sql12.freemysqlhosting.net", username="sql12712200", password="hNDbVanr5f",
database="sql12712200"
)

STEP3:Creating a cursor object

mycursor = mydb.cursor()

STEP4:Creating New Database

Show Database

mycursor.execute("SHOW DATABASES")

for db in mycursor:
print(db)

('information_schema',)
('sql12712200',)

STEP5:Create a Table

create_table_query = """
CREATE TABLE IF NOT EXISTS users (
id INT AUTO_INCREMENT PRIMARY KEY,
username VARCHAR(50) NOT NULL,
email VARCHAR(100) NOT NULL
)
"""
mycursor.execute(create_table_query)

STEP6:Insert Data

22UCS406/ DATABASE MANAGEMENT SYSTEMS LABORATORY


710722205301 ANBU SELVAN
insert_query = """
INSERT INTO users (username, email) VALUES (%s, %s)
"""

user_data = [
("john_doe", "[email protected]"),
("jane_smith", "[email protected]"),
("alice", "[email protected]")
]

mycursor.executemany(insert_query, user_data)
mydb.commit()

STEP7:Read the Data

select_query = """
SELECT * FROM users
"""

mycursor.execute(select_query)
users = mycursor.fetchall()

for user in users:


print(user)

(1, 'john_doe', '[email protected]')


(2, 'jane_smith', '[email protected]')
(4, 'john_doe', '[email protected]')
(5, 'jane_smith', '[email protected]')
(6, 'alice', '[email protected]')

STEP8:Update Data

22UCS406/ DATABASE MANAGEMENT SYSTEMS LABORATORY


710722205301 ANBU SELVAN
update_query = """
UPDATE users SET email = %s WHERE username = %s
"""

new_email = "[email protected]"
username_to_update = "john_doe"

mycursor.execute(update_query, (new_email, username_to_update))


mydb.commit()

STEP9:Show data

select_query = """
SELECT * FROM users
"""

mycursor.execute(select_query)
users = mycursor.fetchall()

for user in users:


print(user)

(1, 'john_doe', '[email protected]')


(2, 'jane_smith', '[email protected]')
(4, 'john_doe', '[email protected]')
(5, 'jane_smith', '[email protected]')
(6, 'alice', '[email protected]')

STEP10:Delete Data

delete_query = """
DELETE FROM users WHERE username = %s

22UCS406/ DATABASE MANAGEMENT SYSTEMS LABORATORY


710722205301 ANBU SELVAN
"""

username_to_delete = "alice"

mycursor.execute(delete_query, (username_to_delete,))
mydb.commit()

STEP11:Show data

select_query = """
SELECT * FROM users
"""

mycursor.execute(select_query)
users = mycursor.fetchall()

for user in users:


print(user)

(1, 'john_doe', '[email protected]')


(2, 'jane_smith', '[email protected]')
(4, 'john_doe', '[email protected]')
(5, 'jane_smith', '[email protected]')

STEP12:Close Connection

# Closing cursor and connection


mycursor.close()
mydb.close()

Result:
Thus, connection between SQL database and python are executed and verified successfully.

22UCS406/ DATABASE MANAGEMENT SYSTEMS LABORATORY

You might also like