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
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
|
#!/usr/bin/env python
import sys
import sqlite3
## menu
def menu():
## menu options
menuOptions="""1. show DB
2. edit DB
3. add to DB
4. exit"""
print("select an option")
print(menuOptions)
menuOptionsInput = input(">>>>")
#ifs and thens
if(menuOptionsInput == 1):
dbShow()
elif(menuOptionsInput == 2):
#run function 2
print("test 2")
elif(menuOptionsInput == 3):
#run function 3
print("test 3")
elif(menuOptionsInput == 4):
exit()
else:
menu()
## make connectiot to DB:
def dbConn():
""" This functon connects to the DB and you gain access to its
sql cammands via db.*
IE: db.execute("insert into knights values('nii', 'sir') ")
the DB file is located in ./awnsers.sqlite"""
conn = sqlite3.connect("./awnsers.sqlite") #makes the connection with the db file.
db = conn.cursor()
##check if table exists:
checkDB = db.execute(""" select 1 from sqlite_master where type='table' and name='awnsers' ;""", )
if(checkDB < 1):
f = open("./createDB.sql")
query = f.read()
db.execute(query)
f.close()
## debug code
#else:
#print("db allready exists! conituing.")
#Show DB Function
def dbShow():
dbConn() ##connect to DB
getQuery =""" select * from awnsers; """
db.execute(getQuery)
for row in db:
print row
if(getQuery == None):
print("No data in table.")
menu()
#main call section
menu()
|