Adding Entries to a MySQL Database : MySQL « Database « Python Tutorial






import MySQLdb

cities = ["Dallas", "Los Angeles", "New York"]
flights = ["1144", "1045", "1520"]
times = ["230pm", "320pm", "420pm"]

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

cHandler = myDB.cursor()

x = 0
for city in cities:
    sqlCommand = "INSERT INTO Arrivals VALUES('%s', '%s', '%s')" % (city, flights[x], times[x])
    cHandler.execute(sqlCommand)
    x += 1

sqlCommand = "SELECT cities, flights, times FROM Arrivals"
cHandler.execute(sqlCommand)
results = cHandler.fetchall()
print results

myDB.commit()

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