St. Britto Hr. Sec. School - Madurai
12th Computer Science Monthly Test - 1 ( Data manipulation through SQL )-Aug 2020
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
-
Mention the users who uses the Database.
-
Write the placeholders which supported by SQlite3 module Execute.
-
Which method is used to fetch all rows from the database table?
-
How the cursor object is created?
-
import sqlite3
connection = sqlite3.connect("Academy.db")
cursor = connection.cursor()
cursor.execute("SELECT * FROM student")
print("fetching first 3 records:")
result = cursor.fetchmany(3)
print(result) -
import sqlite3
connection = sqlite3.connect("Academy.db")
cursor = connection.cursor()
cursor.execute("SELECT DISTINCT (Grade) FROM student")
result = cursor.fetchall()
print(result) -
Which method is used to connect a database? Give an example.
-
What is the reason behind defining a SQL command with triple quotes?
-
What is the use of AND, OR operators combined ·with WHERE clause?
-
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) -
-
import sqlite3
connection = sqlite3.connect("Academy.db")
cursor = connection.cursor()
cursor.execute("SELECT Rollno,sname FROM student
WHERE(Birth_date>='2001-01-01' ANDBirth_date<='2001-12-01')")
result = cursor.fetchall()
print(*result,sep="\n") -
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)
-
-
Explain how the SELECT statement can be used along with GROUP BY class.
-
import sqlite3
connection = sqlite3.connect("Academy.db")
cursor = connection.cursor()
cursor.execute("SELECT DISTINCT (Grade) FROM student where gender='M'")
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 -
What is SQLite?What is it advantage?
-
List the classes used in the SQL SELECT statement.
-
Write the sqlite steps to connect the database.
-
What is the use of aggregate functions used along with SELECT statement? What are they?
-
What is the use of Where Clause.Give a python statement Using the where clause.
-
Write in brief about SQLite and the steps used to use it.
-
-
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() -
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
-
-
import sqlite3
connection = sqlite3.connect("Academy.db")
cursor = connection.cursor()
cursor.execute("SELECT AVG(AVERAGE) FROM student ")
result = cursor.fetchall()
print(result) -
Write the Python script to display all the records of the following table using fetchmany()
Icode ItemName Rate 1003 Scanner 10500 1004 Speaker 3000 1005 Printer 8000 1008 Monitor 15000 1010 Mouse 700 -
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)