Project

General

Profile

Statistics
| Revision:

root / trunk / swipe / query.py @ 300

History | View | Annotate | Download (1.94 KB)

1
"""
2
  This file is part of Tooltron.
3
 
4
  Tooltron is free software: you can redistribute it and/or modify
5
  it under the terms of the Lesser GNU General Public License as published by
6
  the Free Software Foundation, either version 3 of the License, or
7
  (at your option) any later version.
8
 
9
  Tooltron is distributed in the hope that it will be useful,
10
  but WITHOUT ANY WARRANTY; without even the implied warranty of
11
  MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
12
  Lesser GNU General Public License for more details.
13
  You should have received a copy of the Lesser GNU General Public License
14
  along with Tooltron.  If not, see <http://www.gnu.org/licenses/>.
15

16
  Copyright 2009 Bradford Neuman <bneuman@andrew.cmu.edu>
17

18
"""
19
#!/usr/bin/python
20

    
21
import MySQLdb
22
import re
23
import getpass
24

    
25
tools = {
26
   'Bandsaw':'1',
27
   'DrillPress':'2',
28
   'Mill':'3',
29
   'Lathe':'4',
30
   'Welder':'5',
31
   'CircSaw':'6',
32
   'ChopMiterSaw':'7',
33
   'JigSaw':'8',
34
   'Router':'9'}
35

    
36
pw = getpass.getpass("mysql password: ")
37

    
38
db = MySQLdb.connect(host="roboclub8.frc.ri.cmu.edu", user="tooltron", passwd=pw, db="civicrm")
39

    
40
print "connected, now accepting input"
41

    
42
#TODO: errors!
43

    
44
cursor = db.cursor()
45

    
46
qry = "SELECT tools_6 FROM civicrm_value_roboclub_info_2 WHERE card_number_1 = "
47

    
48

    
49
while True:
50
    #    id_s = raw_input("ID# ")
51
    s = raw_input()
52
    id = re.search('%([0-9]*)=.*', s)
53

    
54
    if id != None:
55
        id_s= id.group(1)
56

    
57
        try:
58
            int(id_s)
59
        except ValueError: #make sure it's a number to avoid XKCD #327
60
            print "Error, could not convert '", id_s , "' to an integer"
61
            continue
62

    
63
        cursor.execute(qry + id_s)
64

    
65
        result = cursor.fetchall()
66

    
67
        for r in result:
68
            tls = r[0].split("\x01")
69
            for t in tls:
70
                if t != '':
71
                    try:
72
                        print t, "=", tools[t]
73
                    except KeyError:
74
                        print "could not find key!"
75

    
76
print db