LIVE Course for free

Rated by 1 million+ students
Get app now
JEE MAIN 2024
JEE MAIN 2025 Foundation Course
NEET 2024 Crash Course
NEET 2025 Foundation Course
CLASS 12 FOUNDATION COURSE
CLASS 10 FOUNDATION COURSE
CLASS 9 FOUNDATION COURSE
CLASS 8 FOUNDATION COURSE
0 votes
1.7k views
in Data Manipulation Through SQL by (53.1k points)
closed by

Write python program to accept 5 students names, their ages and ids during run time and display all the records from the table?

1 Answer

+1 vote
by (57.9k points)
selected by
 
Best answer

In this example we are going to accept data using Python input() command during runtime and then going to write in the Table called “Person”

Example

# code for executing query using input data

import sqlite3

#creates a database in RAM

con =sqlite3.connect(“Academy,db”)

cur =con.cursor( )

cur.execute(“DROP Table person”) 

cur.execute(“create table person (name, age, id)”) 

print(“Enter 5 students names:”)

who =[input( ) for i in range(5)]

print(“Enter their ages respectively:”)

age =[int(input()) for i in range(5)]

print(“Enter their ids respectively:”)

p_d =[int(input( ))for i in range(5)]

n =len(who)

for i in range(n):

#This is the q-mark style:

cur.execute(“insert into person values(?,?,?)” , (who[i], age[i], p_id[i]))

#And this is the named style:

cur.execute(“select *from person”)

#Fetches all entries from table

print(“Displaying All the Records From Person Table”)

print (*cur.fetchall(), sep= ’\n’)

OUTPUT

Enter 5 students names:

RAM

KEERTHANA

KRISHNA

HARISH

GIRISH

Enter their ages respectively:

28

12

21

18

16

Enter their ids respectively:

1

2

3

4

5

Displaying All the Records From Person Table 

(‘RAM’ , 28, 1)

(‘KEERTHANA’ , 12, 2)

(‘KRISHNA’ , 21, 3)

(‘HARISH’ , 18,4)

(‘GIRISH’ , 16, 5)

Welcome to Sarthaks eConnect: A unique platform where students can interact with teachers/experts/students to get solutions to their queries. Students (upto class 10+2) preparing for All Government Exams, CBSE Board Exam, ICSE Board Exam, State Board Exam, JEE (Mains+Advance) and NEET can ask questions from any subject and get quick answers by subject teachers/ experts/mentors/students.

...