Using MySQLdb

mySQLdb - Library has to be pip'ed' before and now to be imported


In [4]:
import MySQLdb

Database Connection Properties and Connect to the database


In [46]:
#Enter the values for you database connection
dsn_database = "verein"   # e.g. "MySQLdbtest"
dsn_hostname = "localhost"       # e.g.: "mydbinstance.xyz.us-east-1.rds.amazonaws.com"
dsn_port = 3306                        # e.g. 3306 
dsn_uid = "steinam"             # e.g. "user1"
dsn_pwd = "steinam"            # e.g. "Password123"

conn = MySQLdb.connect(host=dsn_hostname, port=dsn_port, user=dsn_uid, passwd=dsn_pwd, db=dsn_database)

Let's create a sample table and insert some data into it.


In [6]:
conn.query("""DROP TABLE IF EXISTS Cars""")
conn.query("""CREATE TABLE Cars(Id INTEGER PRIMARY KEY, Name VARCHAR(20), Price INT)""")
conn.query("""INSERT INTO Cars VALUES(1,'Audi',52642)""")
conn.query("""INSERT INTO Cars VALUES(2,'Mercedes',57127)""")
conn.query("""INSERT INTO Cars VALUES(3,'Skoda',9000)""")
conn.query("""INSERT INTO Cars VALUES(4,'Volvo',29000)""")
conn.query("""INSERT INTO Cars VALUES(5,'Bentley',350000)""")
conn.query("""INSERT INTO Cars VALUES(6,'Citroen',21000)""")
conn.query("""INSERT INTO Cars VALUES(7,'Hummer',41400)""")
conn.query("""INSERT INTO Cars VALUES(8,'Volkswagen',21600)""")

conn.cursor will return a cursor object, you can use this cursor to perform queries.


In [53]:
cursor=conn.cursor()
cursor.execute("""SELECT * FROM Cars""")
cursor.fetchone()

Now you can print out the result set using pretty print: You should see the following results:


In [48]:
print("\nShow me the records:\n")
rows = cursor.fetchall()
import pprint
pprint.pprint(rows)


Show me the records:

()

Am Ende schließen wir die Verbindung


In [41]:
conn.close()

Using ext sql

'from: https://github.com/catherinedevlin/ipython-sql pip install ipython-sql has to be done


In [ ]:
%load_ext sql

In [54]:
%sql mysql://steinam:steinam@localhost/verein


Out[54]:
'Connected: steinam@verein'

In [57]:
%sql select * from spieler;


14 rows affected.
Out[57]:
SPIELERNR NAME VORNAME TITEL GEBOREN GESCHLECHT BEITRITT STRASSE HAUSNR PLZ ORT TELEFON
2 Elfers Rainer None 1981-12-09 00:00:00 M 1969-05-01 00:00:00 Stadtring 43 40348 Düsseldorf 0211-237893
6 Peters Robert None 1981-05-04 00:00:00 M 1992-05-03 00:00:00 Hafenallee 80 40234 Düsseldorf 0211-476537
7 Wiegand Günther None 1971-08-26 00:00:00 M 1975-12-05 00:00:00 Erasmusweg 39 40648 Düsseldorf 0211-347689
8 Neuhaus Berta None 1979-09-05 00:00:00 W 1996-06-01 00:00:00 Sporenallee 4 40304 Ratingen 02102-45845
27 Kohl Dagmar None 1962-11-14 00:00:00 W 1980-08-01 00:00:00 Luisenpfad 84 40051 Meerbusch 02105-23485
28 Kohl Claudia None 1958-05-01 00:00:00 W 1979-03-01 00:00:00 Domplatz 10 40402 Neuss 02101-65959
39 Bischof Dennis None 1959-01-09 00:00:00 M 1975-11-01 00:00:00 Erikaplatz 78 40876 Düsseldorf 0211-393435
44 Bürger Egon Dr. 1980-04-03 00:00:00 M 1989-04-01 00:00:00 Lichtenstra? 23 40323 Ratingen 02102-36875
57 Bürgern Manfred von 1984-12-19 00:00:00 M 1993-09-01 00:00:00 Erasmusweg 16 40989 Düsseldorf 0211-473458
83 Hofmann Philipp None 1973-04-03 00:00:00 M 1990-02-01 00:00:00 Marienufer 16 40127 Düsseldorf 0211-353548
95 Müller Paul None 1976-07-09 00:00:00 M 1995-02-01 00:00:00 Hauptweg 33 40101 Hilden 02103-86745
100 Peters Franz None 1973-05-03 00:00:00 M 1995-02-01 00:00:00 Hafenallee 80 40756 Düsseldorf 02103-86756
104 Maurer Doris None 1980-09-03 00:00:00 W 1988-11-01 00:00:00 Stutenallee 65 40053 Meerbusch 02105-98757
112 Bauer Irene von 1980-12-19 00:00:00 W 1996-06-01 00:00:00 Fuchsweg 8 41501 Krefeld 02151-54874

In [16]:
result = _

In [19]:
print(result[3])


(8, 'Neuhaus', 'Berta', None, datetime.datetime(1979, 9, 5, 0, 0), 'W', datetime.datetime(1996, 6, 1, 0, 0), 'Sporenallee', '4', '40304', 'Ratingen', '02102-45845')

In [29]:
%sql describe strafen;


4 rows affected.
Out[29]:
Field Type Null Key Default Extra
ZAHLUNGSNR int(11) YES None
SPIELERNR int(11) NO MUL 0
DATUM datetime YES None
BETRAG decimal(18,4) YES None

In [35]:
result = %sql SELECT Betrag, spielernr from strafen


8 rows affected.

In [36]:
%matplotlib inline

In [ ]:
result.bar()

Markdown und SQL

Man kann auch in Markdown-Zellen mit Syntax-Highlightning arbeiten

SELECT *
FROM table_a AS a
LIMIT 10;

New heading