# Datenbank-Connector direkt von MySQL
# http://dev.mysql.com/downloads/connector/python/
import mysql.connector

Servername = 'localhost'
Benutzer   = 'webterra'
Passwort   = 'pwd'
Datenbank  = 'terra'

# Verbindung aufbauen
con = mysql.connector.connect(
            host     = Servername,
            user     = Benutzer,
            password = Passwort,
            database = Datenbank )

cursor = con.cursor()

print("Erste Version: ")
SQLBefehl = """
  SELECT ort.Name, ort.Einwohner, kontinent.Name AS Kontinentname 
    FROM ort, land, kontinent 
   WHERE ort.LNR  = land.LNR 
     AND land.KNR = kontinent.KNR
"""   
cursor.execute(SQLBefehl)

# Durchlaufen der Ergebnisse
row = cursor.fetchone()
while row != None:
  if row[1]>1000000 and row[2]=='Europa':
     print("%s (%d)" % (row[0],row[1]) )
  row = cursor.fetchone()


print("---------------")
print("---------------")
print("Zweite Version: ")
SQLBefehl = """
  SELECT ort.Name, ort.Einwohner 
    FROM ort, land, kontinent 
   WHERE ort.LNR  = land.LNR 
     AND land.KNR = kontinent.KNR 
     AND ort.Einwohner > 1000000 
     AND kontinent.Name = 'Europa'
"""
cursor.execute(SQLBefehl)

row = cursor.fetchone()
while row != None:
  print("%s (%d)" % (row[0],row[1]) )
  row = cursor.fetchone()

cursor.close()

con.disconnect()
