Project

General

Profile

Statistics
| Revision:

root / trunk / swipe / query.py @ 300

History | View | Annotate | Download (1.94 KB)

1 139 kwoo
"""
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 101 rc99
#!/usr/bin/python
20
21
import MySQLdb
22 102 rc99
import re
23 104 rc99
import getpass
24 101 rc99
25 103 rc99
tools = {
26 113 bneuman
   '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 103 rc99
36 104 rc99
pw = getpass.getpass("mysql password: ")
37 101 rc99
38 113 bneuman
db = MySQLdb.connect(host="roboclub8.frc.ri.cmu.edu", user="tooltron", passwd=pw, db="civicrm")
39 104 rc99
40 105 rc99
print "connected, now accepting input"
41
42 101 rc99
#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 105 rc99
49 101 rc99
while True:
50 102 rc99
    #    id_s = raw_input("ID# ")
51
    s = raw_input()
52
    id = re.search('%([0-9]*)=.*', s)
53 103 rc99
54 102 rc99
    if id != None:
55
        id_s= id.group(1)
56 101 rc99
57 102 rc99
        try:
58
            int(id_s)
59 103 rc99
        except ValueError: #make sure it's a number to avoid XKCD #327
60 102 rc99
            print "Error, could not convert '", id_s , "' to an integer"
61
            continue
62 101 rc99
63 102 rc99
        cursor.execute(qry + id_s)
64 101 rc99
65 102 rc99
        result = cursor.fetchall()
66 101 rc99
67 102 rc99
        for r in result:
68 113 bneuman
            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 101 rc99
76
print db