Connectivity
Connectivity
AKPandey, PGT CS
MySQL connectivity with Python
AKPandey, PGT CS
Installing my-sql-connector
AKPandey, PGT CS
Establishing connection
import mysql.connector
mydb=mysql.connector.connect(host="localhost",\
user="root",\
passwd="kv2tbm")
print(mydb)
It should result as follows:
<mysql.connector.connection.MySQLConnection object
at 0x0203A0E8>
AKPandey, PGT CS
To create database
import mysql.connector
mydb=mysql.connector.connect(host="localhost",\
user="root",\
passwd="kv2tbm")
mycursor=mydb.cursor()
mycursor.execute("create database student")
AKPandey, PGT CS
Cursor
• The MySQLCursor of mysql-connector-python is used to
execute statements to communicate with the MySQL
database.
• Using the methods of it you can execute SQL statements,
fetch data from the result sets, call procedures.
• We may want to access data one row at a time, but query
processing cannot happens as one row at a time, so cursor
help us in performing this task. Cursor stores all the data as
a temporary container of returned data and we can fetch
data one row at a time from Cursor.
AKPandey, PGT CS
Following are the various methods
provided by the Cursor class/object.
• execute()
This method accepts a MySQL query as a parameter and
executes the given query.
• fetchall()
This method retrieves all the rows in the result set of a
query and returns them as list of tuples. (If we execute
this after retrieving few rows it returns the remaining
ones)
• fetchone()
This method fetches the next row in the result of a query
and returns it as a tuple.
AKPandey, PGT CS
Creating cursor object
The next step is for interacting with MySql through Python is to
create a cursor object.
• To Create Cursor
mycursor=mydb.cursor()
• Cursor stores all the data as a temporary container of
returned data and we can fetch data one row at a time from
Cursor.
AKPandey, PGT CS
Show Databases
import mysql.connector
mydb=mysql.connector.connect(host="localhost",\
user="root",\
passwd="kv2tbm")
mycursor=mydb.cursor()
mycursor.execute("show databases")
for x in mycursor:
print(x)
AKPandey, PGT CS
Create table
import mysql.connector
mydb=mysql.connector.connect(host="localhost",\
user="root",\
passwd="kv2tbm",\
database="student")
mycursor=mydb.cursor()
mycursor.execute("create table record(rno int(3)
primary key,\ name varchar(15), age int(3))")
AKPandey, PGT CS
To show tables
import mysql.connector
mydb=mysql.connector.connect(host="localhost",\
user="root",\
passwd="kv2tbm",\
database="student")
mycursor=mydb.cursor()
mycursor.execute("show tables")
for x in mycursor:
print(x)
AKPandey, PGT CS
Alter table
import mysql.connector
mydb=mysql.connector.connect(host="localhost",\
user="root",\
passwd="kv2tbm",\
database="student")
mycursor=mydb.cursor()
mycursor.execute("alter table record add(marks
int(3))")
AKPandey, PGT CS
Describe Table
import mysql.connector
mydb=mysql.connector.connect(host="localhost",\
user="root",\
passwd="kv2tbm",\
database="student")
mycursor=mydb.cursor()
mycursor.execute("desc record")
for x in mycursor:
print(x)
AKPandey, PGT CS
Inserting record in table
import mysql.connector
mydb=mysql.connector.connect(host="localhost",\
user="root",\
passwd="kv2tbm",\
database="student")
mycursor=mydb.cursor()
mycursor.execute("insert into record values (1,'john',16,79)")
mydb.commit()
AKPandey, PGT CS
Inserting record in table by asking values
from user during run time
import mysql.connector
mydb=mysql.connector.connect(host="localhost",\
user="root",\
passwd="root",\
database="test")
mycursor=mydb.cursor()
rno=int(input("Enter rno:"))
name=input("Enter name:")
age=int(input("Enter age:"))
value=(rno,name,age)
AKPandey, PGT CS
Inserting multiple records in table
import mysql.connector
mydb=mysql.connector.connect(host="localhost",\
user="root",\
passwd="kv2tbm",\
database="student")
mycursor=mydb.cursor()
mycursor.execute("insert into record values(2,'moha',16,70)")
mycursor.execute("insert into record values(3,'dona',16,89)")
mycursor.execute("insert into record values(4,'yakshi',16,77)")
mydb.commit()
AKPandey, PGT CS
To display all records from table
import mysql.connector
mydb=mysql.connector.connect(host="localhost",\
user="root",\
passwd="kv2tbm",\
database="student")
mycursor=mydb.cursor()
mycursor.execute("select * from record")
records=mycursor.fetchall()
for x in records:
print(x)
AKPandey, PGT CS
To display one record
import mysql.connector
mydb=mysql.connector.connect(host="localhost",\
user="root",\
passwd="kv2tbm",\
database="student")
mycursor=mydb.cursor()
mycursor.execute("select * from record")
records=mycursor.fetchone()
print(records)
AKPandey, PGT CS
fetchall()
fetchall() fetches all the rows of a query
result. It returns all the rows as a list of
tuples. An empty list is returned if there is no
record to fetch. cursor. ... When called
repeatedly this method fetches the next set
of rows of a query result and returns a list of
tuples.
AKPandey, PGT CS
import mysql.connector
mydb = mysql.connector.connect(
host = "localhost",
user = "yourusername",
password = "yourpass",
database = "yourdatabase"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM CUSTOMERS")
# This SQL statement selects all data from the CUSTOMER
table.
result = mycursor.fetchall()
# Printing all records or rows from the table.
# It returns a result set.
for all in result:
print(all)
AKPandey, PGT CS
fetchone()
Fetchone(): Fetchone() method is used when there is a
need to retrieve only the first row from the table. The
method only returns the first row from the defined table.
The method is also used when we want to use the cursor()
method for the iteration. This method increments the
position of the cursor by 1 and after which it returns the
next row.
• Steps for using fetchone() in Mysql using Python:
• First. import MySQL connector
• Now, create a connection with the MySQL connector using connect() method
• Next, create a cursor object with the cursor() method
• Now create and execute the query using “SELECT *” statement with execute()
method to retrieve the data
• Use fetchone() method on the result variable.
• print the result
AKPandey, PGT CS
Example:
Suppose, there’s a table named “CUSTOMERS” and want to retrieve
only the first row from it so, we have to run the below code.
import mysql.connector
mydb = mysql.connector.connect(
host = "localhost",
user = "yourusername",
password = "yourpass",
database = "yourdatabase"
)
mycursor = mydb.cursor()
mycursor.execute("SELECT * FROM CUSTOMERS")
result = mycursor.fetchone()
print(result)
AKPandey, PGT CS
AKPandey, PGT CS
Deleting records from table
import mysql.connector
mydb=mysql.connector.connect(host="localhost",\
user="root",\
passwd="kv2tbm",\
database="student")
mycursor=mydb.cursor()
mycursor.execute("delete from record where rno=1")
mydb.commit()
AKPandey, PGT CS
Update record in table
import mysql.connector
mydb=mysql.connector.connect(host="localhost",\
user="root",\
passwd="kv2tbm",\
database="student")
mycursor=mydb.cursor()
mycursor.execute("update record set marks=marks+2
where rno=2")
mydb.commit()
AKPandey, PGT CS
INSERT and UPDATE operation are executed in the same way
we execute SELECT query using execute() but one thing to
remember, after executing insert or update query we must
commit our query using connection object with commit().
def testdelete():
r=int(input("Enter the Roll no. to delete:-"))
rl=(r,)
sql="delete from class_info where rno=%s"
mycursor.execute(sql,rl)
mydb.commit()
for x in res:
print(x)
AKPandey, PGT CS
AKPandey, PGT CS
Click here to download complete program
https://drive.google.com/file/d/1xaeCgQAs3l-DzZeXseRLDWYSuw5OA4X7/view?usp=sharing
AKPandey, PGT CS