-
Notifications
You must be signed in to change notification settings - Fork 1
/
connect.py
29 lines (26 loc) · 935 Bytes
/
connect.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
import sqlite3
import pandas as pd
import urllib.request
from IPython.display import display, Markdown
urllib.request.urlretrieve("https://github.com/tschuegge/SqlKurs/raw/master/database.db", "database.db")
conn = sqlite3.connect("database.db")
conn.execute("PRAGMA foreign_keys = ON")
def sql(query: str):
cursor = conn.cursor()
returnvalue = None
try:
cursor.execute(query)
result = cursor.fetchall()
if len(result) > 0:
returnvalue = pd.DataFrame(result)
returnvalue.columns = list(map(lambda elem: elem[0], cursor.description))
else:
returnvalue = display(Markdown("✅ **Befehl korrekt ausgeführt, kein Resultat erhalten**"))
cursor.close()
return returnvalue
except sqlite3.Error as error:
returnvalue = display(Markdown("🛑 **SQL Error**: " + str(error)))
finally:
cursor.close()
return returnvalue
print("😊 Verbunden mit Sqlite " + sqlite3.sqlite_version)