0% found this document useful (0 votes)
39 views

notes mysql and python

Uploaded by

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

notes mysql and python

Uploaded by

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

CHAPTER: 15 “INTERFACE PYTHON WITH MYSQL”

Connecting MySQL with Python

# importing required libraries


import mysql.connector
dataBase = mysql.connector.connect(
host ="localhost",
user ="admin",
passwd ="admin"
)
print(dataBase)
# Disconnecting from the server
dataBase.close()

Creating Databases

# importing required libraries


import mysql.connector

dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password"
)

# preparing a cursor object


cursorObject = dataBase.cursor()

# creating database
cursorObject.execute("CREATE DATABASE gfg")

Creating Tables

Creating MySQL table using Python


# importing required libraries
import mysql.connector

dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)
# preparing a cursor object
cursorObject = dataBase.cursor()

# creating table
studentRecord = """CREATE TABLE STUDENT (
NAME VARCHAR(20) NOT NULL,
BRANCH VARCHAR(50),
ROLL INT NOT NULL,
SECTION VARCHAR(5),
AGE INT
)"""

# table created
cursorObject.execute(studentRecord)

# disconnecting from server


dataBase.close()

Insert Data into Tables


Example 1: Inserting Single Row

# importing required libraries


import mysql.connector

dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)

# preparing a cursor object


cursorObject = dataBase.cursor()

sql = "INSERT INTO STUDENT (NAME, BRANCH, ROLL, SECTION, AGE)\


VALUES (%s, %s, %s, %s, %s)"
val = ("Ram", "CSE", "85", "B", "19")

cursorObject.execute(sql, val)
dataBase.commit()

# disconnecting from server


dataBase.close()
Example 2: Inserting Multiple Rows

# importing required libraries


import mysql.connector

dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)

# preparing a cursor object


cursorObject = dataBase.cursor()

sql = "INSERT INTO STUDENT (NAME, BRANCH, ROLL, SECTION, AGE)\


VALUES (%s, %s, %s, %s, %s)"
val = [("Nikhil", "CSE", "98", "A", "18"),
("Nisha", "CSE", "99", "A", "18"),
("Rohan", "MAE", "43", "B", "20"),
("Amit", "ECE", "24", "A", "21"),
("Anil", "MAE", "45", "B", "20"),
("Megha", "ECE", "55", "A", "22"),
("Sita", "CSE", "95", "A", "19")]

cursorObject.executemany(sql, val)
dataBase.commit()

# disconnecting from server


dataBase.close()

Fetching Data

 In order to select particular attribute columns from a table, we write the attribute names.

SELECT attr1, attr2 FROM table_name

 In order to select all the attribute columns from a table, we use the asterisk ‘*’ symbol.

SELECT * FROM table_name


Example: Select data from MySQL table using Python

# importing required libraries


import mysql.connector

dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)

# preparing a cursor object


cursorObject = dataBase.cursor()

query = "SELECT NAME, ROLL FROM STUDENT"


cursorObject.execute(query)

myresult = cursorObject.fetchall()

for x in myresult:
print(x)

# disconnecting from server


dataBase.close()

Where Clause

Example: Where clause in MySQL using Python

# importing required libraries


import mysql.connector

dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)

# preparing a cursor object


cursorObject = dataBase.cursor()

query = "SELECT * FROM STUDENT where AGE >=20"


cursorObject.execute(query)

myresult = cursorObject.fetchall()

for x in myresult:
print(x)

# disconnecting from server


dataBase.close()

Order By Clause
Example: Order By clause in MySQL using Python

# importing required libraries


import mysql.connector

dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)

# preparing a cursor object


cursorObject = dataBase.cursor()

query = "SELECT * FROM STUDENT ORDER BY NAME DESC"


cursorObject.execute(query)

myresult = cursorObject.fetchall()

for x in myresult:
print(x)

# disconnecting from server


dataBase.close()
Limit Clause

# importing required libraries


import mysql.connector

dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)

# preparing a cursor object


cursorObject = dataBase.cursor()

query = "SELECT * FROM STUDENT LIMIT 2 OFFSET 1"


cursorObject.execute(query)
myresult = cursorObject.fetchall()

for x in myresult:
print(x)

# disconnecting from server


dataBase.close()

Update Data
Syntax:
UPDATE tablename
SET ="new value"
WHERE ="old value";

# importing required libraries


import mysql.connector

dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)

# preparing a cursor object


cursorObject = dataBase.cursor()

query = "UPDATE STUDENT SET AGE = 23 WHERE Name ='Ram'"


cursorObject.execute(query)
dataBase.commit()
# disconnecting from server
dataBase.close()

Delete Data from Table

Syntax:
DELETE FROM TABLE_NAME WHERE ATTRIBUTE_NAME = ATTRIBUTE_VALUE

Example: Delete Data from MySQL table using Python

# importing required libraries


import mysql.connector

dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)

# preparing a cursor object


cursorObject = dataBase.cursor()

query = "DELETE FROM STUDENT WHERE NAME = 'Ram'"


cursorObject.execute(query)
dataBase.commit()

# disconnecting from server


dataBase.close()

Drop Tables

Syntax:
DROP TABLE tablename;
DROP TABLE IF EXISTS tablename;

Example 1: Drop Table in MySQL using Python

# importing required libraries


import mysql.connector

dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)

# preparing a cursor object


cursorObject = dataBase.cursor()

query ="DROP TABLE Student;"

cursorObject.execute(query)
dataBase.commit()

# disconnecting from server


dataBase.close()

Example 2: Drop Table if exists

# importing required libraries


import mysql.connector

dataBase = mysql.connector.connect(
host ="localhost",
user ="user",
passwd ="password",
database = "gfg"
)

# preparing a cursor object


cursorObject = dataBase.cursor()

query ="Drop Table if exists Employee;"

cursorObject.execute(query)
dataBase.commit()

# disconnecting from server


dataBase.close()

***** End *****

Mr. M. BARNA BASS M.Sc., M.PhiL. PGDCSA.

PGT COMPUTER SCIENCE


ADHARSH VIDHYALAYA PUBLIC SCHOOL-CBSE
PARUVACHI, ANTHIYUR

You might also like