Kernel: Python 2 (Ubuntu Linux)
DB Zugang
Zugang zu einer MSSQL Datenbank. Bei Amazon AWS Trail gehostet.
In [3]:
ImportErrorTraceback (most recent call last)
<ipython-input-3-5a0e93ffdcbc> in <module>()
----> 1 import pymssql
2
3 db ='dbatwork.cckoaz5lxapo.eu-central-1.rds.amazonaws.com:1433'
4
5 conn = pymssql.connect(host=db, user='adm', password='helloworld',database='master')
ImportError: No module named pymssql
In [6]:
Available line magics:
%alias %alias_magic %autocall %automagic %autosave %bookmark %cat %cd %clear %colors %config %connect_info %cp %debug %dhist %dirs %doctest_mode %ed %edit %env %gui %hist %history %killbgscripts %ldir %less %lf %lk %ll %load %load_ext %loadpy %logoff %logon %logstart %logstate %logstop %ls %lsmagic %lx %macro %magic %man %matplotlib %mkdir %more %mv %notebook %page %pastebin %pdb %pdef %pdoc %pfile %pinfo %pinfo2 %popd %pprint %precision %profile %prun %psearch %psource %pushd %pwd %pycat %pylab %qtconsole %quickref %recall %rehashx %reload_ext %rep %rerun %reset %reset_selective %rm %rmdir %run %save %sc %set_env %store %sx %system %tb %time %timeit %unalias %unload_ext %who %who_ls %whos %xdel %xmode
Available cell magics:
%%! %%HTML %%SVG %%bash %%capture %%debug %%file %%html %%javascript %%js %%latex %%perl %%prun %%pypy %%python %%python2 %%python3 %%ruby %%script %%sh %%svg %%sx %%system %%time %%timeit %%writefile
Automagic is ON, % prefix IS NOT needed for line magics.
In [22]:
ImportErrorTraceback (most recent call last)
<ipython-input-22-a6f2816fa368> in <module>()
1 #faster connection then pymsql to db with less overhead
2 #no cursor() => cant use pandas
----> 3 import _mssql
4
5 def my_msg_handler(msgstate, severity, srvname, procname, line, msgtext):
ImportError: No module named _mssql
In [502]:
DB Erstellung
In [124]:
Die pandas Bibliothek dient hier zur alternativen Datenverarbeitung. Zweiterer Befehl wählt die gewünschte Datenbank aus.
In [11]:
DB Test
Der folgende Code dient zum puren Test der Datenbank.
In [1]:
NameErrorTraceback (most recent call last)
<ipython-input-1-24b28b859ed1> in <module>()
1 #man beachte alter ist nen sql statement und darf kein atributname sein
----> 2 cur.execute(""" IF OBJECT_ID('persons', 'U') IS NOT NULL
3 DROP TABLE persons
4 CREATE TABLE persons (
5 P_Alter INT NOT NULL,
NameError: name 'cur' is not defined
Multi INSERT exec
In [253]:
Get Data
In [522]:
[(80, 'Konrad Adenauer', 1),
(80, 'Ludwig Erhard', 2),
(13, 'Dr. Indiana Jones', 3),
(4, 'Dr. Watson', 4),
(20, 'John Wayne', 5),
(215, 'Bruce Wayne', 6),
(21, 'Mickey Mouse', 7),
(30, 'Popeye der Seemann', 8),
(40, 'Henry Maske', 9)]
In [520]:
P_Alter name id
0 80 Konrad Adenauer 1
1 80 Ludwig Erhard 2
2 13 Dr. Indiana Jones 3
3 4 Dr. Watson 4
4 20 John Wayne 5
5 215 Bruce Wayne 6
6 21 Mickey Mouse 7
7 30 Popeye der Seemann 8
8 40 Henry Maske 9
View test
In [238]:
In [249]:
name
0 Konrad Adenauer
1 Ludwig Erhard
2 Dr. Jones
3 Dr. Watson
4 John Wayne
5 Bruce Wayne
6 Popeye der Seemann
7 Henry Maske
Test procedures
In [262]:
In [263]:
P_Alter | name | id | |
---|---|---|---|
0 | 4 | Dr. Watson | 4 |
In [416]:
In [427]:
[('Dr. Watson',)]
In [529]:
[]
[]
Bash example
In [195]:
Linux compute7-us 4.2.0-42-generic #49-Ubuntu SMP Tue Jun 28 21:26:26 UTC 2016 x86_64 x86_64 x86_64 GNU/Linux
hallo welt ich mag züge :P
DB Zugriff Beispiel mit pandas:
In [160]:
id | name | |
---|---|---|
0 | 0 | Dr. Jones |
1 | 1 | Dr. Watson |
DB Medienverleih Implementierung
DROP CONSTRAINTS
In [757]:
"\nIF OBJECT_ID('FK_zugestellt_rechnung') IS NOT NULL \nALTER TABLE zugestellt DROP FK_zugestellt_rechnung\nIF OBJECT_ID('FK_zugestellt_kunde') IS NOT NULL \nALTER TABLE zugestellt DROP FK_zugestellt_kunde\n"
Create Tables
In [758]:
FERTSCH
In [0]:
In [142]:
{"output_type":"execute_result"}
Views & Methoden
Sandkasten
In [157]:
(1, 'Test', 1, '2017-03-02')
In [0]:
6.4.1. Kunde
In [0]:
In [738]:
Test users :
In [742]:
---------------------------------------------------------------------------
MSSQLDatabaseException Traceback (most recent call last)
pymssql.pyx in pymssql.Cursor.execute (pymssql.c:7184)()
_mssql.pyx in _mssql.MSSQLConnection.execute_query (_mssql.c:11586)()
_mssql.pyx in _mssql.MSSQLConnection.execute_query (_mssql.c:11466)()
_mssql.pyx in _mssql.MSSQLConnection.format_and_run_query (_mssql.c:12746)()
_mssql.pyx in _mssql.check_cancel_and_raise (_mssql.c:16910)()
_mssql.pyx in _mssql.maybe_raise_MSSQLDatabaseException (_mssql.c:17524)()
MSSQLDatabaseException: (15025, b"The server principal '@KundenName' already exists.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20018, severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n")
During handling of the above exception, another exception occurred:
OperationalError Traceback (most recent call last)
<ipython-input-742-0cd7d4bff378> in <module>()
1 cur.executemany(
2 """EXEC personen_anlegen @PID=%d,@KundenName=%s, @KundenVorname =%s,@Strasse=%s,@Hausnummer=%s, @PLZ=%s,@Ort=%s,@EmailAdresse=%s,@TelefonNr=%s;""",
----> 3 [( 0,'Wayne','John','Augustusbruecke','0','011097','Dresden','none','none')])
4
5 #,
pymssql.pyx in pymssql.Cursor.executemany (pymssql.c:7841)()
pymssql.pyx in pymssql.Cursor.execute (pymssql.c:7561)()
OperationalError: (15025, b"The server principal '@KundenName' already exists.DB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20018, severity 14:\nGeneral SQL Server error: Check messages from the SQL Server\nDB-Lib error message 20018, severity 16:\nGeneral SQL Server error: Check messages from the SQL Server\n")
In [709]:
In [740]:
In [745]:
PID | KundenName | KundenVorname | EmailAdresse | TelefonNr | Adr_ID |
---|
In [0]:
In [743]:
('public', 0, 0)
('dbo', 0, 1)
('guest', 0, 2)
('INFORMATION_SCHEMA', 0, 3)
('sys', 0, 4)
('adm', 0, 5)
('@KundenName', 0, 6)
('db_owner', 0, 16384)
('db_accessadmin', 0, 16385)
('db_securityadmin', 0, 16386)
('db_ddladmin', 0, 16387)
('db_backupoperator', 0, 16389)
('db_datareader', 0, 16390)
('db_datawriter', 0, 16391)
('db_denydatareader', 0, 16392)
('db_denydatawriter', 0, 16393)
In [0]:
In [0]:
In [748]:
In [0]:
Exemplar 4.6.2.
In [4]:
In [5]:
NameErrorTraceback (most recent call last)
<ipython-input-5-5d2ab254bd9e> in <module>()
1 # 4.6.2.2. Exemplar ausleihen
----> 2 cur.execute(""" IF OBJECT_ID('exemplar_ausleihen') IS NOT NULL
3 DROP PROCEDURE exemplar_ausleihen
4 """)
5
NameError: name 'cur' is not defined
In [722]:
In [626]:
In [629]:
In [8]:
Vorbestellung 4.6.3.
In [0]:
In [713]:
In [2]:
Traceback (most recent call last):
File "<stdin>", line 2, in <module>
NameError: name 'cur' is not defined
Medien Verwalten (4.6.4.)
Film aufnehmen 4.6.4.1
In [564]:
EBook aufnehmen 4.6.4.2
In [565]:
Spiel aufnehmen 4.6.4.3
In [566]:
Titel suchen 4.6.4.4
Medium suchen 4.6.4.5
In [585]:
Medium löschen 4.6.4.6
In [6]:
NameErrorTraceback (most recent call last)
<ipython-input-6-d7522ef25306> in <module>()
1 # 4.6.4.6.
2 #aussondern TODO: umbennenen
----> 3 cur.execute("""
4 IF OBJECT_ID('medium_loeschen') IS NOT NULL
5 DROP PROCEDURE medium_loeschen
NameError: name 'cur' is not defined
In [0]: