0% found this document useful (0 votes)
18 views5 pages

Set 3

The document contains sample code to demonstrate basic operations in MySQL like creating a database and table, inserting records, selecting records, and filtering records based on conditions. It shows: 1) Creating a database called SCHOOL and table called STU to store student records 2) Inserting 8 records into the STU table 3) Selecting all records from the STU table 4) Additional SELECT queries to retrieve particular columns and filter on conditions 5) Python code to connect to MySQL database and query records where department is 'HISTORY'

Uploaded by

Rashvandh
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)
18 views5 pages

Set 3

The document contains sample code to demonstrate basic operations in MySQL like creating a database and table, inserting records, selecting records, and filtering records based on conditions. It shows: 1) Creating a database called SCHOOL and table called STU to store student records 2) Inserting 8 records into the STU table 3) Selecting all records from the STU table 4) Additional SELECT queries to retrieve particular columns and filter on conditions 5) Python code to connect to MySQL database and query records where department is 'HISTORY'

Uploaded by

Rashvandh
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/ 5

SET – 3

Q-1 a) Write a program to count a total number of lines and count the total number of lines
starting with 'A', 'B', and 'C' from the file myfile.txt. (8)
Example: If File Contents as given below:
Python is super and trending language.
Allows to store the output in the files.
A text file stores textual data. Binary files can handle binary data.
Binary files use pickle module to store data.
CSV files can handle tabular data.
CSV files can be read easily using CSV reader object.
SOURCE CODE:
def count():
with open("myfile.txt","r") as f1:
data=f1.readlines()
cnt_lines=0
cnt_A=0
cnt_B=0
cnt_C=0
for lines in data:
cnt_lines+=1
if lines[0]=='A':
cnt_A+=1
if lines[0]=='B':
cnt_B+=1
if lines[0]=='C':
cnt_C+=1
print("Total Number of lines are:",cnt_lines)
print("Total Number of lines strating with A are:",cnt_A)
print("Total Number of lines strating with B are:",cnt_B)
print("Total Number of lines strating with C are:",cnt_C)
f1.close()
count()
OUTPUT:

1. b
mysql> CREATE DATABASE SCHOOL;
Query OK, 1 row affected (0.20 sec)

mysql> USE SCHOOL;


Database changed
mysql> CREATE TABLE STU
-> (Rollno INTEGER,
-> Name varchar(15),
-> Gender varchar(2),
-> Age int(3),
-> Dept varchar(15),
-> DOA date,
-> Fees integer(5) );
Query OK, 0 rows affected, 2 warnings (0.32 sec)

mysql> INSERT INTO STU


-> VALUES(1,'Arun','M',24,'COMPUTER','1997-01-10',120);
Query OK, 1 row affected (0.11 sec)

mysql> INSERT INTO STU


-> VALUES(2,'Ankit','M',21,'HISTORY','1998-03-24',200);
Query OK, 1 row affected (0.10 sec)

mysql> INSERT INTO STU


-> VALUES(3,'Anu','F',20,'HINDI','1996-12-12',300);
Query OK, 1 row affected (0.04 sec)

mysql> INSERT INTO STU VALUES(4,'Bala','M',19,NULL,'1997-07-01',400);


Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO STU VALUES(5,'Cheran','M',18,'HINDI','1997-09-05',250);


Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO STU


-> VALUES(6,'Deepa','F',19,'HISTORY','1997-06-27',300);
Query OK, 1 row affected (0.06 sec)

mysql> INSERT INTO STU


-> VALUES(7,'Dinesh','M',22,'COMPUTER','1997-02-25',210);
Query OK, 1 row affected (0.05 sec)

mysql> INSERT INTO STU


-> VALUES(8,'Usha','F',23,NULL,'1997-07-31',200);
Query OK, 1 row affected (0.06 sec)

mysql> SELECT * FROM STU;


+--------+--------+--------+------+----------+------------+------+
| Rollno | Name | Gender | Age | Dept | DOA | Fees |
+--------+--------+--------+------+----------+------------+------+
| 1 | Arun | M | 24 | COMPUTER | 1997-01-10 | 120 |
| 2 | Ankit | M | 21 | HISTORY | 1998-03-24 | 200 |
| 3 | Anu | F | 20 | HINDI | 1996-12-12 | 300 |
| 4 | Bala | M | 19 | NULL | 1997-07-01 | 400 |
| 5 | Cheran | M | 18 | HINDI | 1997-09-05 | 250 |
| 6 | Deepa | F | 19 | HISTORY | 1997-06-27 | 300 |
| 7 | Dinesh | M | 22 | COMPUTER | 1997-02-25 | 210 |
| 8 | Usha | F | 23 | NULL | 1997-07-31 | 200 |
+--------+--------+--------+------+----------+------------+------+
8 rows in set (0.00 sec)

a)mysql> create table info


-> (Rollno INTEGER,
-> Name varchar(15),
-> Gender varchar(2),
-> Age int(3),
-> Dept varchar(15),
-> DOA date,
-> Fees int(5));
mysql> insert into info(Rollno,Name,Gender,Age,Dept,DOA,Fees)
select Rollno,Name,Gender,Age,Dept,DOA,Fees
mysql> select * from info;
+--------+--------+--------+------+----------+------------+------+
| Rollno | Name | Gender | Age | Dept | DOA | Fees |
+--------+--------+--------+------+----------+------------+------+
| 1 | Arun | M | 24 | COMPUTER | 1997-01-10 | 170 |
| 2 | Ankit | M | 21 | HISTORY | 1998-03-24 | 200 |
| 3 | Anu | F | 20 | HINDI | 1996-12-12 | 300 |
| 4 | Bala | M | 19 | NULL | 1999-07-01 | 400 |
| 5 | Cheran | M | 18 | HINDI | 1997-09-05 | 250 |
| 6 | Deepa | F | 19 | HISTORY | 1997-06-27 | 300 |
| 7 | Dinesh | M | 22 | COMPUTER | 1997-02-25 | 210 |
+--------+--------+--------+------+----------+------------+------+
7 rows in set (0.00 sec)

b)mysql>SELECT * FROM STU;


+--------+--------+--------+------+----------+------------+------+
| Rollno | Name | Gender | Age | Dept | DOA | Fees |
+--------+--------+--------+------+----------+------------+------+
| 1 | Arun | M | 24 | COMPUTER | 1997-01-10 | 120 |
| 2 | Ankit | M | 21 | HISTORY | 1998-03-24 | 200 |
| 3 | Anu | F | 20 | HINDI | 1996-12-12 | 300 |
| 4 | Bala | M | 19 | NULL | 1999-07-01 | 400 |
| 5 | Cheran | M | 18 | HINDI | 1997-09-05 | 250 |
| 6 | Deepa | F | 19 | HISTORY | 1997-06-27 | 300 |
| 7 | Dinesh | M | 22 | COMPUTER | 1997-02-25 | 210 |
| 8 | Usha | F | 23 | NULL | 1997-07-31 | 200 |
+--------+--------+--------+------+----------+------------+------+
8 rows in set (0.00 sec)
c)mysql>select Rollno,Name,Dept from STU;
mysql> select Rollno,Name,Dept from stu;
+--------+--------+----------+
| Rollno | Name | Dept |
+--------+--------+----------+
| 1 | Arun | COMPUTER |
| 2 | Ankit | HISTORY |
| 3 | Anu | HINDI |
| 4 | Bala | NULL |
| 5 | Cheran | HINDI |
| 6 | Deepa | HISTORY |
| 7 | Dinesh | COMPUTER |
+--------+--------+----------+
7 rows in set (0.00 sec)

d)
IN PYTHON
import mysql.connector
db=mysql.connector.connect(host='localhost',user='root',passwd=
'Sriram@123 ',database='school')
cursor=db.cursor()
cursor.execute("SELECT * FROM STU WHERE DEPT,'HISTORY'")
cursor.fetchall()
db.close()
OUTPUT:
[(2, 'Ankit', 'M', 21, 'HISTORY', datetime.date(1998, 3, 24), 200, None), (6, 'Deepa', 'F', 19,
'HISTORY', datetime.date(1997, 6, 27), 300, None)]

You might also like