📖 generic · 12th TN - English Medium · COMPUTER SCIENCE · Page 284question

DATA MANIPULATION THROUGH SQL · Part 4

Chapter 4: 6 · COMPUTER SCIENCE

of the file, change the path while opening the file. Example . . - #save the file as “ .py” import sqlite3 connection = sqlite3.connect("Academy.db") crsr = connection.cursor() # execute the command to fetch all the data from the table Student crsr.execute("SELECT * FROM Student") # store all the fetched data in the ans variable ans= crsr.fetchall() # loop to print all the data for i in ans: print(i) .

. . Displaying all records using fetchall() The fetchall() method is used to fetch all rows from the database table Example . .

. - import sqlite3 connection = sqlite3.connect("Academy.db") cursor = connection.cursor() cursor.execute("SELECT * FROM student") print("fetchall:") result = cursor.fetchall() for r in result: print(r) OUTPUT fetchall: ( , 'Akshay', 'B', 'M', . , ' - - ') ( , 'Aravind', 'A', 'M', . , ' - - ') ( , 'BASKAR', 'C', 'M', .

, ' - - ') ( , 'SAJINI', 'A', 'F', . , ' - - ') ( , 'VARUN', 'B', 'M', . , ' - - ') ( , 'PRIYA', 'A', 'F', . , ' - - ') ( , 'TARUN', 'D', 'M', .

, ' - - ') 12th Computer Chapter - - cursor.fetchall() -fetchall () method is to fetch all rows from the database table cursor.fetchone() - The fetchone () method returns the next row of a query result set or None in case there is no row left. cursor.fetchmany() method that returns the next number of rows (n) of the result set Note . . .

Displaying A record using fetchone() The fetchone() method returns the next row of a query result set or None in case there is no row left. Example . . .

- import sqlite3 connection = sqlite3.connect("Academy.db") cursor = connection.cursor() cursor.execute("SELECT * FROM student") print("\nfetch one:") res = cursor.fetchone() print(res) OUTPUT fetch one: ( , 'Akshay', 'B', 'M', . , ' - - ') . . .

Displaying all records using fetchone() Using while loop and fetchone() method we can display all the records from a table. Example . . .

- import sqlite3 connection = sqlite3.connect("Academy.db") cursor = connection.cursor() cursor.execute("SELECT * FROM student") print("fetching all records one by one:") result = cursor.fetchone() while result is not None: print(result) result = cursor.fetchone() OUTPUT fetching all records one by one: ( , 'Akshay', 'B', 'M', . , ' - - ') ( , 'Aravind', 'A', 'M', . , ' - - ') ( , 'BASKAR', 'C', 'M', . , ' - - ') ( , 'SAJINI',

Related topics

Have a question about this topic?

Get an AI answer grounded in your actual textbook — with the exact page reference.

Ask AI about this topic →