Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                
0% found this document useful (0 votes)
32 views

Computer Science_MY SQL

Uploaded by

nitiuser7
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)
32 views

Computer Science_MY SQL

Uploaded by

nitiuser7
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/ 9

TABLE: STOCK

Pno Pname Dcode Qty UnitPrice StockDate


5005 Ball point pen 102 100 10 2021-03-31
5003 Gel pen premium 102 150 15 2021-01-01
5002 Pencil 101 125 4 2021-02-18
5006 Scale 101 200 6 2020-01-01
5001 Eraser 102 210 3 2020-03-19
5004 Sharpner 102 60 5 2020-12-09
5009 Gel pen classic 103 160 8 2022-01-19

TABLE: DEALERS
Dcode Dname
101 Sakthi Stationeries
103 Classic Stationeries
102 Indian Book House

(a) To display the total Unit price of all the products whose Dcode as 102.

Sol:
mysql> SELECT SUM(UNITPRICE) FROM STOCK GROUP BY DCODE
HAVINGDCODE=102;
Output:

(b) To display details of all products in the stock table in descending order of Stock date.

Sol:
mysql> SELECT * FROM STOCK ORDER BY STOCKDATE DESC;

Output:
(c) To display maximum unit price of products for each dealer individually as
per dcodefrom the table Stock.

Sol:

mysql> SELECT DCODE,MAX(UNITPRICE) FROM STOCK


GROUP BY DCODE;
Output:

(d) To display the Pname and Dname from table stock and dealers.

Sol:
mysql> SELECT PNAME,DNAME FROM STOCK S,DEALERS D
WHERE S.DCODE=D.DCODE;

Output:
Python
Database
Connectivity
Program 13: Program to connect with database and store record of employee and display
records.

import mysql.connector as mycon


con = mycon.connect(host='localhost',
user='root',
password="root")
cur = con.cursor()
cur.execute("create database if not exists company")
cur.execute("use company")
cur.execute("create table if not exists employee(empno int, name varchar(20), dept varchar(20),salary int)")
con.commit()
choice=None
while choice!=0:
print("1. ADD RECORD ")
print("2. DISPLAY RECORD ")
print("0. EXIT")
choice = int(input("Enter Choice :"))
if choice == 1:
e = int(input("Enter Employee Number :"))
n = input("Enter Name :")
d = input("Enter Department :")
s = int(input("Enter Salary :"))
query="insert into employee values({},'{}','{}',{})".format(e,n,d,s)
cur.execute(query)
con.commit()
print("## Data Saved ##")
elif choice == 2:
query="select * from employee"
cur.execute(query)
result = cur.fetchall()
print("%10s"%"EMPNO","%20s"%"NAME","%15s"%"DEPARTMENT", "%10s"%"SALARY")
for row in result:
print("%10s"%row[0],"%20s"%row[1],"%15s"%row[2],"%10s"%row[3])
elif choice==0:
con.close()
print("## Bye!! ##")
OUTPUT:

1. ADD RECORD
2. DISPLAY RECORD
0. EXIT
Enter Choice :1
Enter Employee Number :101
Enter Name :RAMESH
Enter Department :IT
Enter Salary :34000
## Data Saved ##
1. ADD RECORD
2. DISPLAY RECORD
0. EXIT
Enter Choice :2
EMPNO NAME DEPARTMENT SALARY
101 RAMESH IT 34000
1. ADD RECORD
2. DISPLAY RECORD
0. EXIT
Enter Choice : 0
Program 14: Program to connect with database and search employee number in table
employee and display record, if empno not found display appropriate message.

import mysql.connector as mycon


con = mycon.connect(host='localhost',
user='root',
password="root",
database="company")
cur = con.cursor()
print("#"*40)
print("EMPLOYEE SEARCHING FORM")
print("#"*40)
print("\n\n")
ans='y'
while ans.lower()=='y':
eno = int(input("ENTER EMPNO TO SEARCH :"))
query="select * from employee where empno={}".format(eno)
cur.execute(query)
result = cur.fetchall()
if cur.rowcount==0:
print("Sorry! Empno not found ")
else:
print("%10s"%"EMPNO", "%20s"%"NAME","%15s"%"DEPARTMENT",
"%10s"%"SALARY")
for row in result:
print("%10s"%row[0],"%20s"%row[1],"%15s"%row[2],"%10s"%row[3])
ans=input("SEARCH MORE (Y) :")

OUTPUT:
########################################
EMPLOYEE SEARCHING FORM
########################################

ENTER EMPNO TO SEARCH :101


EMPNO NAME DEPARTMENT SALARY
101 RAMESH IT 34000
SEARCH MORE (Y) :
Program 15: Perform all the operations (Insert, Update, Delete, Display) with reference to table
‘student’ through MySQL-Python connectivity

import mysql.connector as ms
db=ms.connect(host="localhost",
user="root",
passwd="root",
database="class_xii"
)
#cn=db.cursor()
def insert_rec():
try:
while True:
rn=int(input("Enter roll number:"))
sname=input("Enter name:")
marks=float(input("Enter marks:"))
gr=input("Enter grade:")
cn.execute("insert into student values({},'{}',{},'{}')".format(rn,sname,marks,gr))
db.commit()
ch=input("Want more records? Press (N/n) to stop entry:")
if ch in 'Nn':
print("Record Inserted ")
break
except Exception as e:
print("Error", e)
def update_rec():
try:
rn=int(input("Enter rollno to update:"))
marks=float(input("Enter new marks:"))
gr=input("Enter Grade:")
cn.execute("update student set marks={},gr='{}' where rn={}".format(marks,gr,rn))
db.commit()
print("Record Updated.... ")
except Exception as e:
print("Error",e)
def delete_rec():
try:
rn=int(input("Enter rollno to delete:"))
cn.execute("delete from student where rn={}".format(rn))
db.commit()
print("Record Deleted ")
except Exception as e:
print("Error",e)

def view_rec():
try:
cn.execute("select * from student")
records = cn.fetchall()
for record in records:
print(record)
#db.commit()
#print("Record...")
except Exception as e:
print("Error",e)
db = ms.connect( host="localhost",
user="root",
passwd="root",
database="class_xii"
)
cn = db.cursor()
while True:
print("MENU\n1. Insert Record\n2. Update Record \n3. Delete Record\n4. Display Record \n5.Exit")
ch=int(input("Enter your choice<1-4>="))
if ch==1:
insert_rec()
elif ch==2:
update_rec()
elif ch==3:
delete_rec()
elif ch==4:
view_rec()
elif ch==5:
break
else:
print("Wrong option selected")
OUTPUT:

MENU
1. Insert Record
2. Update Record
3. Delete Record
4. Display Record
5.Exit
Enter your choice<1-4>=1
Enter roll number:101
Enter name:Ramesh
Enter marks:85
Enter grade:A
Want more records? Press (N/n) to stop entry:n
Record Inserted
MENU
1. Insert Record
2. Update Record
3. Delete Record
4. Display Record
5.Exit
Enter your choice<1-4>=4
(101, 'Ramesh', 85.0, 'A')
MENU
1. Insert Record
2. Update Record
3. Delete Record
4. Display Record
5.Exit
Enter your choice<1-4>=2
Enter rollno to update:101
Enter new marks:58
Enter Grade:B
Record Updated....
MENU
1. Insert Record
2. Update Record
3. Delete Record
4. Display Record
5.Exit
Enter your choice<1-4>=4
(101, 'Ramesh', 58.0, 'B')
MENU
1. Insert Record
2. Update Record
3. Delete Record
4. Display Record
5.Exit
Enter your choice<1-4>=5

You might also like