Advertisements
Advertisements
Question
Sumit wants to write a code in Python to display all the details of the passengers from the table flight
in MySQL database, Travel
. The table contains the following attributes:
F_code:
Flightcode(String) F_name:
Name of flight (String) Source:
Departure city of flight (String) Destination:
Destination city of flight (String)
Consider the following to establish connectivity between Python and MySQL.
- Username:
root
- Password:
airplane
- Host:
localhost
Code Writing
Solution
import mysql. connector as m
try:
con=m.connect(host='localhost',user='root',pa
sswd='airplane',database='travel')
mycursor=con.cursor()
mycursor.execute("Select * from flight")
data=mycursor.fetchall()
for fdata in data:
print("Code of flight":, fdata [0])
print("Flight Name":, fdata [1])
print("Source of flight":, fdata [2])
print("Flight destination":, fdata[3])
except:
con.close()
shaalaa.com
Is there an error in this question or solution?