Skip to content

Commit 292765d

Browse files
committed
better sqlite3 sample file
1 parent 614e0de commit 292765d

File tree

2 files changed

+43
-16
lines changed

2 files changed

+43
-16
lines changed

.gitignore

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -33,3 +33,7 @@ nosetests.xml
3333
.mr.developer.cfg
3434
.project
3535
.pydevproject
36+
37+
38+
beginnerscorner/tests/sqlite/test1.db
39+
.idea

beginnerscorner/tests/sqlite/test1.py

Lines changed: 39 additions & 16 deletions
Original file line numberDiff line numberDiff line change
@@ -13,32 +13,55 @@
1313
cursor = connection.cursor()
1414

1515
# erste Tabelle anlegen
16-
cursor.execute("""CREATE TABLE user (id INTEGER, name TEXT, vorname TEXT, pass TEXT)""")
17-
cursor.execute("""CREATE TABLE chanel (id INTEGER, name TEXT, pass TEXT, typ INTEGER)""")
16+
cursor.execute("CREATE TABLE IF NOT EXISTS user (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, vorname TEXT, pass TEXT, personalNummer INTEGER)")
17+
cursor.execute("CREATE TABLE IF NOT EXISTS chanel (id INTEGER PRIMARY KEY AUTOINCREMENT, name TEXT, pass TEXT, typ INTEGER)")
1818

19-
# Daten in die Tabelle fuellen
20-
cursor.execute("""INSERT INTO user VALUES ( 1, 'oerb', 'the Real', 'hallo')""")
21-
cursor.execute("""INSERT INTO user VALUES ( 2, 'bison', 'The Warhammer', 'hallo1')""")
22-
connection.commit() # solange commit nicht ausgeführt, sind daten eventuell noch nicht geschrieben
19+
# Daten in die Tabelle fuellen (is aber kacke so)
20+
cursor.execute("INSERT INTO user VALUES (NULL, 'oerb', 'the Real', 'hallo', 1337);")
21+
cursor.execute("INSERT INTO user VALUES (NULL, 'bison', 'The Warhammer', 'hallo1', 23);")
22+
connection.commit() # solange commit nicht ausgefuehrt, sind daten eventuell noch nicht geschrieben
2323

2424
# Sichere Datenuebergabe mit einem Dictionary
25-
werte = { "id" : "3", "name" : "00", "vorname" : "M", "pass" : "hallo3" }
26-
sql = "INSERT INTO user VALUES (:id, :name, :vorname, :pass)"
25+
werte = { "id" : None, "name" : "00", "vorname" : "M", "pass" : "hallo3", "personalNummer": 01 }
26+
sql = "INSERT INTO user VALUES (:id, :name, :vorname, :pass, :personalNummer)"
27+
cursor.execute(sql, werte)
28+
29+
connection.commit()
30+
31+
32+
werte = {"id": None, "name" : "01", "vorname" : "M", "pass" : "hallo3", "personalNummer": 02 }
33+
sql = "INSERT INTO user VALUES (:id, :name, :vorname, :pass, :personalNummer)"
2734
cursor.execute(sql, werte)
2835

2936
connection.commit()
3037

3138
# alternativ geht auch folgendes
32-
for row in ((4, "superhero", "Hero", "karabanga"),
33-
(4, "Nemo", "Captain", "Nautilus")):
34-
cursor.execute("INSERT INTO user VALUES(?,?,?,?)",row)
39+
for row in ((None, "superhero", "Hero", "karabanga", 77),
40+
(None, "Nemo", "Captain", "Nautilus", 88)):
41+
cursor.execute("INSERT INTO user VALUES(?,?,?,?,?)", row)
3542

3643
connection.commit()
3744

38-
# ne kleine selectabfrage
45+
# ne kleine selectabfrage (in kacke)
46+
cursor.execute("SELECT vorname, name FROM user WHERE personalNummer>2")
47+
dataRows = cursor.fetchall()
48+
49+
for data in dataRows:
50+
print(data)
51+
52+
print dataRows[0]
53+
54+
print '--------------------'
55+
56+
# ne kleine selectabfrage (in "sicher")
57+
cursor.execute("SELECT vorname, name FROM user WHERE personalNummer>:personalNummer",
58+
{"personalNummer": 2})
59+
dataRows = cursor.fetchall()
60+
61+
for data in dataRows:
62+
print(data)
63+
64+
print dataRows[0]
3965

40-
cursor.execute("SELECT vorname, name FROM user WHERE id=1")
41-
data = cursor.fetchall()
4266

43-
print(data[0])
44-
connection.close()
67+
connection.close()

0 commit comments

Comments
 (0)