sqlite1.py 1.12 KB
Newer Older
1
2
3
4
5
6
7
8
#!/usr/bin/python3
import sqlite3 
import yaml

connection = sqlite3.connect("test.db")
cursor = connection.cursor()

#Ab hier: ABSTRAKTION
9
10
11
12
13
14
15
16
17
18
19
20
cursor.execute("DROP TABLE IF EXISTS benutzer;")
sql_command = """
CREATE TABLE benutzer (
b_nr INTEGER PRIMARY KEY,
vname VARCHAR(40),
nname VARCHAR(40),
username VARCHAR(40),
aktiv INTEGER,
mail VARCHAR(60),
beitritt DATE
); """
cursor.execute(sql_command)
21
22
23

#Benutzer aus YAML-Datei lesen und in die Tabelle einfügen
with open('users.yaml') as f:
24
    global users 
25
26
27
28
29
30
31
32
33
    users = yaml.safe_load(f)

#print(users)

def addPerson(vname, nname, username, aktiv, mail, beitritt):
    cursor.execute("INSERT INTO benutzer(vname, nname, username, aktiv, mail, beitritt) VALUES (:vn, :nn, :un, :akt, :mail, :bei)", {'vn': vname, 'nn': nname, 'un': username, 'akt': aktiv, 'mail': mail, 'bei': beitritt})

for x in range (0, 3):
    addPerson(users[x] ['vname'], users[x] ['nname'], users[x] ['username'], users[x] ['aktiv'], users[x] ['mail'], users[x] ['beitritt'])
34
35
connection.commit()
connection.close()
36
37
38
39
40
41
42

#sql_command = """
#SELECT *
#FROM benutzer;
#"""
#cursor.execute(sql_command)
#print(cursor.fetchall())