Retrieving Entries from a MySQL Database : MySQL « Database « Python Tutorial






import MySQLdb

myDB = MySQLdb.connect(host="127.0.0.1", port=3306, db="schedule")

cHandler = myDB.cursor()

sqlCommand = "SELECT * FROM Arrivals WHERE city = 'Dallas'"
cHandler.execute(sqlCommand)

results = cHandler.fetchall()
print results

sqlCommand = "SELECT * FROM Arrivals"
cHandler.execute(sqlCommand)

results = cHandler.fetchall()
print results

cityList = []
flightList = []
timeList = []
for row in results:
    cityList.append(row[0])
    flightList.append(row[1])
    timeList.append(row[2])

x = 0
for flight in flightList:
    print ("Flight %s arrives from %s at %s" % (flight, cityList[x],  timeList[x]))
    x+=1

myDB.close()








15.3.MySQL
15.3.1.Basic connection to MySQL with mysqldb
15.3.2.Connecting to a MySQL Database Server
15.3.3.Creating a MySQL Database
15.3.4.Adding Entries to a MySQL Database
15.3.5.Retrieving Entries from a MySQL Database
15.3.6.Create table, insert data, update data and select data