'A', 'F', . , ' - - ') ( , 'VARUN', 'B', 'M', . , ' - - ') ( , 'PRIYA', 'A', 'F', . , ' - - ') ( , 'TARUN', 'D', 'M', .
, ' - - ') 12th Computer Chapter - - Data Manipulation Through SQL . . . Displaying Specified number of records using fetchmany(n) Displaying specified number of records is done by using fetchmany(n).
This method returns the number of rows of the result set. Example . . .
- : Program to display the content of tuples using fetchmany() import sqlite3 connection = sqlite3.connect("Academy.db") cursor = connection.cursor() cursor.execute("SELECT * FROM student") print("fetching first records:") result = cursor.fetchmany( ) print(result) OUTPUT fetching first records: [( , 'Akshay', 'B', 'M', . , ' - - '), ( , 'Aravind', 'A', 'M', . , ' - - '), ( , 'BASKAR', 'C', 'M', . , ' - - ')] Example .
. . - : Program to display the content of tuples in newline without using loops import sqlite3 connection = sqlite3.connect("Academy.db") cursor = connection.cursor() cursor.execute("SELECT * FROM student") print("fetching first records:") result = cursor.fetchmany( ) print(*result,sep="\n") # * is used for unpacking a tuple. OUTPUT fetching first records: ( , 'Akshay', 'B', 'M', .
, ' - - ') ( , 'Aravind', 'A', 'M', . , ' - - ') ( , 'BASKAR', 'C', 'M', . , ' - - ') * symbol is used to print the list of all elements in a single line with space. To print all elements in new lines or separated by space use sep= "\n" or sep= "," respectively.
Note 12th Computer Chapter - - . . CLAUSES IN SQL SQL provides various clauses that can be used in the SELECT statements. This clauses can be called through python script.
Almost all clauses will work with SQLite. The following frequently used clauses are discussed here • DISTINCT • WHERE • GROUP BY • ORDER BY. • HAVING . .
. SQL DISTINCT Keyword The distinct keyword is helpful when there is need of avoiding the duplicate values present in any specific columns/table. When we use distinct keyword only the unique values are fetched. In this example we are going to display the different grades scored by students from “student table”.
Example . . . - import sqlite3 connection = sqlite3.connect("Academy.db") cursor = connection.cursor() cursor.execute("SELECT DISTINCT (Grade) FROM student") result = cursor.fetchall() print(result) OUTPUT