Course Content

If you are facing any issue in playing video please use Firefox browser.

Course Content

Description

Python MySQL Querying Data

 

In this video we will see that,

How to insert, update dynamic data in database ?

How to work with select query in Python ?

 

Now we will take input from user and we'll put that data in database :

 

import mysql.connector

def getConnection():

conn=mysql.connector.connect(host="localhost",database="School",user="root",password="root")

return conn;

def insert_student(sname,semail):

conn=getConnection()

cur=conn.cursor()

query="insert into student(sname,semail) values(%s,%s)

args=(sname,semail)

cur.execute(query,args)

conn.commit()

conn.close()

 

So now this is an method for insert. This module was name 'DatabaseOperation'. In this we have made a method named 'insert_student'. Now we will create a new module and in that we will call this method.

 

from DatabaseOperation import*

sname=input("Enter Student Name : ")

semail=input("Enter Student Email : ")

insert_student(sname,semail)

cur.execute(query,args)

conn.commit()

conn.close()

print("Student data inserted")

 

To update data of our database :

 

def update_student(sname,semail,sid):

conn=getConnection()

cur=conn.cursor()

query="update student set sname=%s,semail=%s where sid=%s";

args=(sname,semail,sid)

cur.execute(query,args)

conn.commit()

conn.close()

print ("Student Data updated")

 

from DatabaseOperation import*

sname=input("Enter Student Name : ")

semail=input("Enter Student Email : ")

sid=input("Enter Student Roll no : ")

sid=int(sid)

update_student(sname,semail,sid)

cur.execute(query,args)

conn.commit()

conn.close()

print("Student Data updated")

Output :

Enter Student Name : Payal

Enter Student Email : payal@gmail.com

Enter Student Roll no : 4

Student Data Updated

 

To delete data from our database :

 

def delete_student(sid):

conn=getConnection()

cur=conn.cursor()

query="delete from student where sid=%s";

args=(sid,)

cur.execute(query,args)

conn.commit()

conn.close()

print ("Student Data deleted")

 

from DatabaseOperation import*

sid=input("Enter Student Roll no : ")

sid=int(sid)

delete_student(sname,semail,sid)

cur.execute(query,args)

conn.commit()

conn.close()

print("Student Data deleted")

Output :

Enter Student Roll no : 4

Student Data deleted

 

Select query :

 

def select_all_students():

conn=getConnection()

cur=conn.cursor()

query="select * from student"

cur.execute(query)

rows=cur.fetchall()

conn.commit()

conn.close()

return rows

 

from DatabaseOperation import*

students=select_all_students()

for student in students:

print("Roll No : ",student[0])

print("Name : ",student[1])

print("Email : ",student[2])

print ()

 

When we will execute it, it will run successfully and will give us all the data which has been stored in database.

 

 

FAQs

Share With Friend

Have a friend to whom you would want to share this course?

Download The Apps

Code Scan or Download the app
Google Play Store
297K+ Downloads
4.5 Rating
10K+ Reviews
  • Learn anywhere on the go
  • Get regular updates about your enrolled or new courses
  • Share content with your friends
  • Evaluate your progress through practice tests
  • No internet connection needed
  • Enroll for the webinar and join at the time of the webinar from anywhere