St. Britto Hr. Sec. School - Madurai
12th Computer Science Monthly Test - 2 ( Data manipulation through SQL )-Aug 2020
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
import sqlite3
connection = sqlite3.connect("Academy.db")
cursor = connection.cursor()
cursor.execute("SELECT GENDER,COUNT(GENDER) FROM Student GROUP BY GENDER HAVING COUNT(GENDER)>3")
result = cursor.fetchall()
co = [i[0] for i in cursor.description]
print(co)
print(result) -
import sqlite3
connection = sqlite3.connect("Academy.db")
cursor = connection.cursor()
cursor.execute("SELECT * FROM student where grade<>'A' and Grade<>'B'")
result = cursor.fetchall()
print(*result,sep="\n") -
Read the following details.Based on that write a python script to display department wise records
database name :- organization.db
Table name :- Employee
Columns in the table :- Eno, EmpName, Esal, Dept -
-
In this example we are going to count the number of records(rows)
import sqlite3
connection = sqlite3.connect("Academy.db")
cursor = connection.cursor()
cursor.execute("SELECT COUNT(*) FROM student ")
result = cursor.fetchall()
print(result) -
Read the following details.Based on that write a python script to display records in desending order of
Eno
database name :- organization.db
Table name :- Employee
Columns in the table :- Eno, EmpName, Esal, Dept
-
-
What is the use of Where Clause.Give a python statement Using the where clause.
-
What is the purpose of using
(i) COUNT()
(ii) AVG()
(iii) SUM()
(iv) MAX()
(v) MIN(). -
Explain how a connect to be made to a database (Academy through python SQlite3.
-
Consider the following table Supplier and item .Write a python script for (i) to (ii)
SUPPLIER Suppno Name City Icode SuppQty S001 Prasad Delhi 1008 100 S002 Anu Bangalore 1010 200 S003 Shahid Bangalore 1008 175 S004 Akila Hydrabad 1005 195 S005 Girish Hydrabad 1003 25 S006 Shylaja Chennai 1008 180 S007 Lavanya Mumbai 1005 325 i) Display Name, City and Itemname of suppliers who do not reside in Delhi.
ii) Increment the SuppQty of Akila by 40 -
Write a Python script to create a table called ITEM with following specification.
Add one record to the table.
Name of the database :- ABC
Name of the table :- Item
Column name and specification :-Icode :- integer and act as primary key Item Name :- Character with length 25 Rate :- Integer Record to be added :- 1008, Monitor,15000 -
What is the use of HAVING clause. Give an example python script
-
In this example we are going to count the number of records by specifying a column
import sqlite3
connection = sqlite3.connect("Academy.db")
cursor = connection.cursor()
cursor.execute("SELECT COUNT(AVERAGE) FROM student ")
result = cursor.fetchall()
print(result) -
-
import sqlite3
connection = sqlite3.connect("Organization.db")
cursor = connection.cursor()
print("Displaying the name of the Highest Average")
cursor.execute("SELECT sname,max(AVERAGE) FROM student ")
result = cursor.fetchall()
print(result)
print("Displaying the name of the Least Average")
cursor.execute("SELECT sname,min(AVERAGE) FROM student ")
result = cursor.fetchall()
print(result) -
import sqlite3
import io # to access replace()
import csv
# CREATING CSV FILE
d=open('c:/pyprg/sql.csv','w')
c=csv.writer(d)
connection = sqlite3.connect("Academy.db")
cursor = connection.cursor()
# a=Connection.cursor()
cursor.execute("SELECT * FROM student ORDER BY GENDER DESC,SNAME")
# WRITING THE COLUMN HEADING
co = [i[0] for i in cursor.description]
c.writerow(co)
data=cursor.fetchall()
for item in data:
c.writerow(item)
d.close()
# Reading the CSV File
# replace() is used to eliminate the newline character at the end of each row
with open('c:/pyprg/sql.csv', "r", newline=None) as fd:
# r = csv.reader(fd)
for line in fd:
line = line.replace("\n", "")
print(line)
cursor.close()
connection.close()
-