Skip to main content

GRADE XII - MYSQL CONNECTIVITY WITH PYTHON

 GRADE XII - MYSQL CONNECTIVITY WITH PYTHON


SOURCE CODE :

#ADD TO DATABASE

import mysql.connector

mydb = mysql.connector.connect(

host="localhost",

user="root",

passwd="root",

database="school" )

mycursor=mydb.cursor()

v1=input("enter GROCERY ID:")

v2=input("enter GROCERY NAME:")

v3=int(input("enter PRICE:"))

v4=int(input("enter QUANTITY:"))

sql='insert into GRO values("%s","%s","%d","%d")'%(v1,v2,v3,v4)

 #print(sql)

mycursor.execute(sql)

mydb.commit()

print("*********Grocery Added To Database Successfully*****")


#TO VIEW DATA

sql="select * from gro"

mycursor.execute(sql)

res=mycursor.fetchall()

print("The AVAILABLE GROCERY details are as follows : ")

print("G_ID    NAME     PRICE   QUANTITY")

print("\n##*************************************** ************ ##")

for x in res:

        print(x)

print("\n##***************************************************  ##")


#DELETION FROM DATABASE

 print("*******************DELETE  FORM **************************")

    en = input("Enter Grocery Id to be deleted :")

    query="select * from gro where ID="+en

    mycursor.execute(query)

    results = mycursor.fetchall()

    if mycursor.rowcount<=0:

        print("\## SORRY! NO MATCHING DETAILS AVAILABLE ##")

    else:

       

        print("**************************************************")

        print('%5s'%"NAME",'%15s'%'PRICE','%12s'%'QUANTITY')

        print("**************************************************")

        for row in results:

            print('%5s' % row[1],'%15s'%row[2],'%15s'%row[3])

    print("-"*50)

    ans = input("Are you sure to delete ? (y/n)")

    if ans=="y" or ans=="Y":

        query="delete from GRO where ID="+en

        mycursor.execute(query)

        mydb.commit()

        print("\n##***************** RECORD DELETED**********************  ##")

        print("\n##***************** ************************************  ##")

    

_____________________________________________________________________________

GRADE - XII

MYSQL-PYTHON CONNECTIVITY

PRACTICE QUESTIONS

1. Write the code to check whether the database has been created or not using Python interface.

2. Write a code to create a table ‘student’ inside the database ‘school’ using Python as the interface.

3. Write a code to check for the created table using Python.

4. How can we add a new column ‘marks’ in the student table?

5. Write the code to insert multiple records into the table student through Python interface.

6. How can we display all the records of a student table in Python?

7. Write the code to display all the records along with the total number of records from the student table using Python shell.

8. Kabir wants to write a program in Python to insert the following record in the table named Student in MYSQL database, SCHOOL:

· rno(Roll number ) - integer

· name(Name) - string

· DOB (Date of birth) – Date

· Fee – float

Note the following to establish connectivity between Python and MySQL:

· Username - root

· Password - tiger

· Host - localhost

a)     The values of fields rno, name, DOB and fee has to be accepted from the user. Help Kabir to write the program in Python.

b)    He also wants to display the records of students whose fee is more than 5000. Help him to write the program in Python.

 

9. A table, named STATIONERY, in ITEMDB database, has the following structure:

Field

Type

itemNo

int(11)

itemName

Varchar(15)

Price

Float

qty

int(11)

 

Write the following Python function to perform the specified operation:

AddAndDisplay(): To input details of an item and store it in the table STATIONERY. The function should then retrieve and display all records from the STATIONERY table where the Price is greater than 120.

Assume the following for Python-Database connectivity:

Host: localhost, User: root, Password: Pencil

 

 

ANSWERS

 

1. import mysql.connector
mydb = mysql.connector.connect(host="localhost", user="root", password="123456")
print("Yes, its connected")
mycursor=mydb.cursor()
mycursor.execute("Show Databases")
for i in mycursor:
    print(i)

2. import mysql.connector
mydb = mysql.connector.connect(host="localhost", user="root", password="123456", database=”school”)
mycursor=mydb.cursor()
mycursor.execute("Create table student (RollNo int (5) primary key, Name varchar (15), age int, city varchar (20)")

3. import mysql.connector
mydb = mysql.connector.connect(host="localhost", user="root", password="123456", database=”school”)
mycursor=mydb.cursor()

mycursor.execute("Show Tables")
for i in mycursor:
    print(i)

4. import mysql.connector
mydb = mysql.connector.connect(host="localhost", user="root", password="123456", database=”school”)
mycursor=mydb.cursor()
mycursor.execute("alter table student add (marks int (3)")

 

5. import mysql.connector
mydb = mysql.connector.connect(host="localhost", user="root", password="123456", database=”school”)
mycursor=mydb.cursor()
mycursor.execute("insert into student values (1, ‘Pooja’, 21, ‘Ggn’, 99)")

mycursor.execute("insert into student values (2, ‘Viraj’, 21, ‘Ggn’, 99)")

mycursor.execute("insert into student values (3, ‘Myra’, 21, ‘Ggn’, 99)")

mycursor.execute("insert into student values (4, ‘Ankit’, 21, ‘Ggn’, 99)")

mycursor.execute("insert into student values (5, ‘Raman’, 21, ‘Ggn’, 99)")

mydb.commit()

6. import mysql.connector
mydb = mysql.connector.connect(host="localhost", user="root", password="123456", database=”school”)
mycursor=mydb.cursor()

mycursor.execute("Select * from student")

myrecords = mycursor.fetchall()
for i in myrecords:
    print(i)

 

7. import mysql.connector
mydb = mysql.connector.connect(host="localhost", user="root", password="123456", database=”school”)
mycursor=mydb.cursor()

mycursor.execute("Select * from student")

myrecords = mycursor.fetchall()

no_rec=mycursor.rowcount

print(“Total no. of records found are:”, no_rec)
for i in mycursor:
    print(i)

 

8 a)

8. b)

9. def Add_Item():

    import mysql.connector as mycon

    mydb=mycon.connect(host="localhost",user="root", password="Pencil",database="ITEMDB")

    mycur=mydb.cursor()

    no=input("Enter Item Number: ")

    nm=input("Enter Item Name: ")

    pr=input("Enter price: ")

    qty=input("Enter qty: ")                                

    query="INSERT INTO stationery VALUES (“%s”, “%s”, “%s”, “%s”)”% (no, nm, pr, qty)

   print(query)

    mycur.execute(query)

    mydb.commit()

    mycur.execute("select * from stationery where price>120")

    for i in mycur:

        print(i)

 


Comments

Popular posts from this blog

GRADE XII - Python Connectivity with MySQL

  Python Connectivity with MySQL In real-life applications, the data inputted by the user and processed by the application needs to be saved permanently, so that it can be used for future manipulation. Usually, input and output data is displayed during execution but not stored, as it resides in main memory , which is temporary — i.e., it gets erased once the application is closed. This limitation can be overcome by sending the data to be stored in a database , which is made accessible to the user through a Front-End interface . Key Concepts Database A database is an organized collection of data that is stored and accessed electronically from a computer system. DBMS (Database Management System) A DBMS is software that interacts with end-users, applications, and the database to capture and analyze data. Front-End The Front-End is the user interface of the application, responsible for input/output interaction with the user. Back-End The Back-End refe...

GRADE XI | DESIGN THINKING & INNOVATION (848) | INTRO | CH-1

  DESIGN THINKING &  INNOVATION (848)  INTRODUCTION LINK : INTRODUCTION PPT PPT LINK Notes: Design Thinking and Innovation Grade XI Introduction Section   I. What is Design? (0.1.1) Definitions from Experts: John Maeda: "Design is solution to a problem". Saul Bass: "Design is thinking made visual". Charles Eames:   “Design is a plan for arranging elements in such a way as best to accomplish a particular purpose.” Steve Jobs:  "Design is not just what it looks like and feels like. Design is how it works". Prof. Sudhakar Nadkarni:  "Essentials of design are— purity, precision, details". Design is a way of understanding needs, identifying problems, and creating appropriate and innovative solutions. It is not only about appearance, but also about usefulness and sustainability. Design is explained as something that helps solve problems and make a positive difference. Key ...