CBSE Class 12 Computer Science Question 44 of 46

Interface Python with MySQL — Question 1

Back to all questions
1
Question

Question 1

Design a Python application that fetches all the records from Pet table of menagerie database.

Answer
import mysql.connector

db_con = mysql.connector.connect(host = "localhost", 
                                user = "root",
                                passwd = "lion",
                                database = "menagerie")
cursor = db_con.cursor()

cursor.execute("SELECT * FROM Pet")

records = cursor.fetchall()
for record in records:
    print(record)

db_con.close()
Output
('Fluffy', 'Harold', 'cat', 'f', datetime.date(1993, 2, 4), None)
('Claws', 'Gwen', 'cat', 'm', datetime.date(1994, 3, 17), None)
('Buffy', 'Harold', 'dog', 'f', datetime.date(1989, 5, 13), None)
('Fang', 'Benny', 'dog', 'm', datetime.date(1990, 8, 27), None)
('Bowser', 'Diane', 'dog', 'm', datetime.date(1979, 8, 31), datetime.date(1995, 7, 29))
('Chirpy', 'Gwen', 'bird', 'f', datetime.date(1998, 9, 11), None)
('Whistler', 'Gwen', 'bird', None, datetime.date(1997, 12, 9), None)
('Slim', 'Benny', 'snake', 'm', datetime.date(1996, 4, 29), None)