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

Python+MySQL

Uploaded by

jsudha896
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
32 views

Python+MySQL

Uploaded by

jsudha896
Copyright
© © All Rights Reserved
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 22

Interface of Python with a MySQL Database

Steps to Use MySQL Connector:


1.Download Python Application(3.7) .exe file and Install.
(https://www.python.org/downloads/release/python-370/)
2. Download MySQL Applicaton(5.5.62).exe file and Install.
(https://downloads.mysql.com/archives/community/)
3. Download MySQL Connector
(https://dev.mysql.com/downloads/connector/python/)
4.To Install MySQL Connector (Open Command Prompt and Execute the
Following Command).
>pip install mysql-connector
5.Write Python Statement in Python Shell.
>>>import mysql.connector
If no error message is occurred means, we can take it MySQL Connector working
properly.
6.Now we can able to Connect Python with MySQL Server.
Python Application
MySQL Database
MySQL Connector
Interface of Python with an MySQL Database
A Database Cursor is a Special Control Structure that
facilitates the row by row processing of records in the resultset. i.e., the
set of records retrieved as per query.
The ResultSet refers to logical set of records that are fetched
from the database by executing an SQL query and made available to the
application program.
How to Create Cursor Object and Use it: (Create Databases, View)

import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="SQLDB")
mycursor=mydb.cursor()
mycursor.execute("create database newdb") #mycursor=cursor object
mycursor.execute("show databases") #mydb=connection object
for x in mycursor:
print(x)
Interface of Python with an MySQL Database
Interface of Python with an MySQL Database
(Use Database and View Tables)
import mysql.connector as sql
mydb=sql.connect(host="localhost",user="root",passwd="SQLDB“, database=“sqldb2”)
mycursor=mydb.cursor()
#mycursor.execute("use sqldb2")
mycursor.execute("show tables")
for x in mycursor:
print(x)
Create Table & View its Structure
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="SQLDB")
mycursor=mydb.cursor()
mycursor.execute("use sqldb2")
mycursor.execute("CREATE TABLE Emp(EmpID INT AUTO_INCREMENT
PRIMARY KEY, EmpName VARCHAR(55), Address VARCHAR(500))")
Alter Table (add, drop, modify column)
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="SQLDB")
mycursor=mydb.cursor()
mycursor.execute("use sqldb2")
mycursor.execute("alter table emp add(PhoneNumber int(10))")
mycursor.execute("alter table emp drop Address")
mycursor.execute("alter table emp modify column EmpName text(50)")
mydb.commit()
Alter Table Drop, Truncate Table.
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="SQLDB")
mycursor=mydb.cursor()
mycursor.execute("use sqldb2")
mycursor.execute("truncate table ft")
#mycursor.execute("drop table ft")
mydb.commit()
Alter Table Rename
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="SQLDB")
mycursor=mydb.cursor()
mycursor.execute("use sqldb2")
mycursor.execute("alter table fromexittable rename to fet")
#mycursor.execute("alter table fet change City Cities Varchar(40)")
mydb.commit()
Insert Values into Table
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="SQLDB")
mycursor=mydb.cursor()
mycursor.execute("use sqldb2")
mycursor.execute("insert into emp values(3,'Rahul',987654321)") # row by row insert
#mycursor.execute("insert into emp values(7,'MSD',987456321)")
#mycursor.execute("insert into emp values(18,'VK',987321654)")
mydb.commit()
print(mycursor.rowcount, "record inserted.")
Insert Multiple Values into Table
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="SQLDB")
mycursor=mydb.cursor()
mycursor.execute("use sqldb2")
mycursor = mydb.cursor()
sql= "INSERT INTO emp (EmpID, EmpName, PhoneNumber) VALUES (%s,%s,%s)"
val=[(333,'Peter',44987456),(10,'Amy',44123456),(888,'Hannah',44654321),
(27,'Michael',44978645),(36,'Sandy',44123789),(45,'Betty',44236547), 99,'Richard',44987455),]
mycursor.executemany(sql, val)
mydb.commit()
print(mycursor.rowcount, "was inserted.")
Update & Delete Values into Table
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="SQLDB",
database="sqldb2")
mycursor=mydb.cursor()
#mycursor.execute("use sqldb2")
sql = "UPDATE emp SET EmpName= 'Muralitharan' WHERE EmpID = 888"
mycursor.execute(sql)
mycursor.execute("DELETE FROM emp WHERE EmpID=27")
mydb.commit()
Update Values into Table
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="SQLDB",data
base="sqldb2")
mycursor=mydb.cursor()
sql = "UPDATE emp SET EmpID={} WHERE EmpID={}".format(303,333)
mycursor.execute(sql)
mydb.commit()
Select All the Fields & Values from Table
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="SQLDB",
database="sqldb2")
mycursor=mydb.cursor()
#mycursor.execute("use sqldb2")
mycursor.execute("select * from emp")
for x in mycursor:
print(x)
Select Particular Fields & Values from Table
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="SQLDB",data
base="sqldb2")
mycursor=mydb.cursor()
mycursor.execute("select PhoneNumber from emp")
for x in mycursor:
print(x)
#mycursor.execute("select EmpName from emp where EmpID=18 OR EmpID=888")
#for x in mycursor:
# print(x)
Display Data by Using Fetchall, Rowcount
Fetchall: This Method will return all the rows from the resultset in the form of a
tuple containing the records.
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="SQLDB",
database="sqldb2")
mycursor=mydb.cursor()
mycursor.execute("SELECT * FROM emp")
fchall=mycursor.fetchall()
print(fchall)
print(mycursor.rowcount, "Record(s) Fetched from the Table EMP")
mydb.commit()
Display Data by Using Fetchone, Rowcount
Fetchone: This Method will return only one row from the resultset in the form of
a tuple containing the records.
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="SQLDB",
database="sqldb2")
mycursor=mydb.cursor()
mycursor.execute("SELECT * FROM emp")
fchone=mycursor.fetchone()
print(fchone)

Rowcount: This Method that always returns how many records have been
retrieved so far using any of the fetch() methods.
Display Data by Using Fetchmany, Rowcount
Fetchmany: This Method will return only the ”N” number of row from the
resultset in the form of a tuple containing the records.
import mysql.connector
mydb=mysql.connector.connect(host="localhost",user="root",passwd="SQLDB",
database="sqldb2")
mycursor=mydb.cursor()
mycursor.execute("SELECT * FROM emp")
fmany=mycursor.fetchmany(5)
print(fmany)

# fmany=mycursor.fetchmany(2)
THANK YOU

You might also like