/+junk/Dataanalys

To get this branch, use:
bzr branch http://gegoxaren.bato24.eu/bzr/%2Bjunk/Dataanalys

« back to all changes in this revision

Viewing changes to awnsers_and_db_edit.py

  • Committer: Gustav Hartvigsson
  • Date: 2010-12-21 13:12:47 UTC
  • Revision ID: gustav.hartvigsson@gmail.com-20101221131247-h0fgwud7k3slm1r3
Made some changes and added the initial work on the different functions...
and changed the enviorment to 3.1....



----------------his line and the following will be ignored --------------

modified:
  awnsers_and_db_edit.py
  createDB.sql

Show diffs side-by-side

added added

removed removed

Lines of Context:
1
 
#!/usr/bin/env python
 
1
#!/usr/bin/env python3.1
2
2
import sys
3
3
import sqlite3
4
4
 
5
5
 
 
6
#======================= connection to DB ===========================
 
7
 
 
8
conn = sqlite3.connect("./awnsers.sqlite") #makes the connection with the db file.
 
9
db = conn.cursor()
 
10
 
 
11
##create table from query from file: create table if not exists
 
12
 
 
13
f = open("./createDB.sql")
 
14
query = f.read()
 
15
db.execute(query)
 
16
f.close()
 
17
 
 
18
#===================== end DB connection ==========================
 
19
 
 
20
 
 
21
#===================== Futction section ===========================
 
22
 
6
23
## menu
7
24
def menu():
8
25
        ## menu options
9
26
        menuOptions="""1. show DB
10
27
2. edit DB
11
28
3. add to DB
12
 
4. exit"""
 
29
4. exit
 
30
"""
13
31
 
14
32
        print("select an option")
15
33
        print(menuOptions)
16
34
        menuOptionsInput = input(">>>>")
17
35
 
18
36
        #ifs and thens
19
 
        if(menuOptionsInput == 1):
 
37
        if(menuOptionsInput == 1 or menuOptionsInput.lower() == "s" ):
20
38
                dbShow()
21
39
                
22
 
        elif(menuOptionsInput == 2):
23
 
                #run function 2
24
 
                print("test 2")
25
 
        elif(menuOptionsInput == 3):
26
 
                #run function 3
27
 
                print("test 3")
28
 
        elif(menuOptionsInput == 4):
 
40
        elif(menuOptionsInput == 2 or menuOptionsInput.lower() =="e" ):
 
41
                dbEdit()
 
42
        
 
43
        elif(menuOptionsInput == 3 or menuOptionsInput.lower() == "a" ):
 
44
                dbAdd()
 
45
        
 
46
        elif(menuOptionsInput == 4 or menuOptionsInput.lower() == "q" ):
29
47
                exit()
 
48
        
30
49
        else:
 
50
                print("You need to select an option! in the list")
31
51
                menu()
32
52
        
33
53
 
34
54
 
35
 
## make connectiot to DB:
36
 
def dbConn():
37
 
        """ This functon connects to the DB and you gain access to its
38
 
        sql cammands via db.*
39
 
        IE: db.execute("insert into knights values('nii', 'sir') ") 
40
 
        the DB file is located in ./awnsers.sqlite"""
41
 
        conn = sqlite3.connect("./awnsers.sqlite") #makes the connection with the db file.
42
 
        db = conn.cursor()
43
 
        
44
 
        ##check if table exists:
45
 
        checkDB = db.execute(""" select 1 from sqlite_master where type='table' and name='awnsers' ;""", )
46
 
        if(checkDB < 1):
47
 
                f = open("./createDB.sql")
48
 
                query = f.read()
49
 
                db.execute(query)
50
 
                f.close()
51
 
        ## debug code
52
 
        #else:
53
 
                #print("db allready exists! conituing.")
54
 
        
55
 
        
 
55
 
56
56
 
57
57
#Show DB Function
58
58
def dbShow():
59
 
        dbConn() ##connect to DB
 
59
        print("DEBUG : Running dbShow()")
 
60
        #TODO: make it print counts of occurunsus of different true (integer 1) values
 
61
        
60
62
        getQuery =""" select * from awnsers; """
61
63
        db.execute(getQuery)
62
64
        
63
65
        for row in db:
64
 
                print row
 
66
                print( row )
65
67
        
66
68
        
67
69
        if(getQuery == None):
69
71
        
70
72
        menu()
71
73
 
72
 
#main call section
 
74
def dbEdit():
 
75
        print("DEBUG: running dbEdit()")
 
76
        #TODO: make it 
 
77
        menu()
 
78
 
 
79
 
 
80
def dbAdd():
 
81
        print("DEBUG: running dbAdd()")
 
82
        #TODO: make a function with all the questions and such.
 
83
        menu()
 
84
 
 
85
 
 
86
#==================== end function setion ==================
 
87
 
 
88
 
 
89
#==================== main call section ====================
73
90
menu()
74
91