Computer Science_MY SQL
Computer Science_MY SQL
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:
(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.
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.
OUTPUT:
########################################
EMPLOYEE SEARCHING FORM
########################################
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