comp progs
comp progs
import pickle
# Create table
mycursor.execute('''
CREATE TABLE IF NOT EXISTS EMPLOYEE (
Empid INT PRIMARY KEY,
EName VARCHAR(50),
CompName VARCHAR(50),
City VARCHAR(50),
SEX CHAR(1)
)
''')
print("Table Created")
con.commit()
print('Records Inserted')
# Query b: Display name and city of employees whose Empid is 30 and above
mycursor.execute('SELECT EName, City FROM EMPLOYEE WHERE Empid >= 30')
y=mycursor.fetchall():
for emp in y:
print(emp)
# Close connection
con.close()
-----------------------------------------------------------------------------------
------------------------------------------
2
def count_lines():
count = 0
file=(student.txt, 'r')
x=file.readlines()
for i in x:
if i[0]=='A'or'E'or'I':
count += 1
print("Number of lines starting with A, E, or I: ",count)
count_lines()
-----------------------------------------------------------------------------------
--------------------------------------
import mysql.connector
# Table creation
mycursor.execute('''
CREATE TABLE IF NOT EXISTS SALES (
SalesId INT PRIMARY KEY,
Salesperson VARCHAR(50),
Item VARCHAR(50),
Units INT,
UnitCost INT,
Totalcost INT
)
''')
print("table is created")
# Clear previous records for simplicity
mycursor.execute('DELETE FROM SALES')
# Record insertion
mycursor.execute("INSERT INTO SALES VALUES (100, 'Jones', 'Pen', 45, 10, 450)")
mycursor.execute("INSERT INTO SALES VALUES (102, 'Kivell', 'Binder', 50, 19, 950)")
mycursor.execute("INSERT INTO SALES VALUES (105, 'Jardine', 'Pencil', 36, 16,
576)")
mycursor.execute("INSERT INTO SALES VALUES (106, 'Gill', 'Pen', 27, 19, 513)")
mycursor.execute("INSERT INTO SALES VALUES (115, 'Andrews', 'Pen', 75, 10, 750)")
mycursor.execute("INSERT INTO SALES VALUES (119, 'Jardine', 'Pencil', 90, 11,
990)")
# Commit changes
con.commit()
print(mycursor.rowcount, 'Records Inserted')
# Query a: Search records for item 'Pen' with Totalcost more than 600
mycursor.execute("SELECT * FROM SALES WHERE Item = 'Pen' AND Totalcost > 600")
x=mycursor.fetchall()
for record in x:
print(record)
# Query b: List items with UnitCost more than 15, sorted by Units
import csv
# Writing to CSV file in 'w' mode (overwrite the file each time)
file = open('marks.csv', mode='a', newline='') # Changed to 'a' for
appending new records
writer = csv.writer(file)
writer.writerow(student_data)
file.close()
more_records = input("Do you want to add another student record? (y/n): ")
import mysql.connector
# Query a. List the name of Male students sorted by highest to lowest marks
mycursor.execute('''
SELECT SName FROM STUDENT
WHERE Gender = "Male"
ORDER BY Marks DESC
''')
male_students = mycursor.fetchall()
# Query b. Search minimum Marks from student table for United States
mycursor.execute('''
SELECT MIN(Marks) FROM STUDENT
WHERE Country = "United States"
''')
min_marks_us = mycursor.fetchone()
print("\nMinimum Marks for students from United States:", min_marks_us[0])
-----------------------------------------------------------------------------------
------------------------------------------------------------
import pickle
# Use pickle to serialize the student record and write it to the file
pickle.dump(student_record, file)
# Check if marks are greater than 350 and display the record
if student_record['Marks'] > 350:
print(f"ID: {student_record['SID']}, Name:
{student_record['Sname']}, Stream: {student_record['Stream']}, Marks:
{student_record['Marks']}")
except EOFError:
break
# Main execution
# Create student records in the binary file
CreateStd()
-----------------------------------------------------------------------------
5
# Function to create a list of 10 elements and push numbers greater than 50 into a
stack
def create_and_push_to_stack():
# Creating a list with 10 elements (random numbers for example purposes)
numbers = [12, 75, 44, 56, 85, 22, 91, 37, 62, 50]
stack = [] # Empty stack
for num in numbers:
if num > 50:
stack.append(num)
return stack
-----------------------------------------------------------------------------------
---------------------------------
import mysql.connector
# a. List the names of students who are in class 12, sorted by average marks
mycursor.execute('''
SELECT Name
FROM STUDENT
WHERE Class LIKE '12%'
ORDER BY AvgMark DESC
''')