YoungJules

New db part

Nov 7th, 2012
83
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 3.63 KB | None | 0 0
  1. #!/usr/bin/env python
  2.  
  3. import MySQLdb
  4. import MySQLdb.cursors
  5.  
  6. # Information about the database      
  7.  
  8. database = "ENTS"
  9. table = "members"
  10. hostaddr = "localhost"
  11. username = "mastercontrol"
  12. # TODO: This is insecure as it can be read by anyone with access to this script, needs to be handled in another way
  13. password = "pass"
  14.  
  15. def connect():
  16.     conn = MySQLdb.connect ( host = hostaddr, user = username, passwd = password, db = database, cursorclass=MySQLdb.cursors.DictCursor )
  17.         cursor = conn.cursor()
  18.     return conn, cursor
  19.  
  20. def close(conn, cursor):
  21.     cursor.close()
  22.     conn.close()
  23.  
  24. # Go to the database and get the last unlock event for the given door number
  25. # We match FobNumber on the members table with CardNum on the DoorLog table and search based on door number
  26. def getLastUnlock( doorNum ):
  27.         print "getLastUnlock connecting to db"
  28.     conn, cursor = connect()
  29.         #conn = MySQLdb.connect ( host = hostaddr, user = username, passwd = password, db = database, cursorclass=MySQLdb.cursors.DictCursor )
  30.         #cursor = conn.cursor()                          #connect to users db    
  31.         print "connection cursor created"
  32.     # We use an inner join because we want to find at least one match on both tables
  33.         cursor.execute ("SELECT * from DoorLog inner join members ON members.FobNumber=DoorLog.CardNum where DoorNum = '"+str(doorNum)+"' order by EventDateTime DESC")
  34.         row = cursor.fetchone()                 #get the record for the current cardnum
  35.     print "getLastUnlock returning row:"        
  36.     print row
  37.     close(conn, cursor)
  38.         return row
  39.  
  40.  
  41. def getMember( fobNumber, fobField ):
  42.         #conn = MySQLdb.connect ( host = hostaddr, user = username, passwd = password, db = database, cursorclass=MySQLdb.cursors.DictCursor )
  43.         #cursor = conn.cursor()                          #connect to users db    
  44.     print "getMember connecting to db"
  45.     conn, cursor = connect()
  46.         cursor.execute ("SELECT * from members where "+fobField+" = '" + fobNumber+"'")
  47.  
  48.         try:
  49.             row = cursor.fetchone()                 #get the record for the current cardnum
  50.         except ValueError:              #Exception:
  51.                 #no match for that user cardNum, send
  52.                 #server.serve_forever()back fail message
  53.                 print "Major Failure \n"
  54.                 #print Exception
  55.  
  56.         #close the mysql connection            
  57.         close(conn, cursor)
  58.     return row
  59.  
  60. def createDoorLogFail( fobNumber, doorNumber, cmd ):
  61.     print "createDoorLogFail connecting to db"
  62.     conn, cursor = connect()
  63.     cursor.execute("insert into DoorLog values ("+fobNumber+
  64.         ",'"+fobNumber+" Not in database: "+cmd+
  65.         "','"+doorNumber+"',now(),NULL)")
  66.     close(conn, cursor)
  67.  
  68. def createDoorLogAdmit( fobNumber, firstName, lastName, doorNumber):
  69.     print "createDoorLogAdmit connecting to db"
  70.     conn, cursor = connect()
  71.         cursor.execute("insert into DoorLog values ("+fobNumber+
  72.         ",'"+firstName+" "+lastName+" unlocked door "+doorNumber+
  73.         "','"+doorNumber+"',now(),NULL)")
  74.     close(conn, cursor)
  75.  
  76. def getSeconds( lastUnlock ):
  77.     print "getSeconds connecting to db"
  78.     conn, cursor = connect()
  79.     cursor.execute("select timestampdiff(second,'"+lastUnlock+"',now()) as seconds")
  80.         seconds = cursor.fetchone()['seconds']
  81.     if (seconds is None):
  82.         seconds = 2000
  83.     close(conn, cursor)
  84.     return seconds
  85.  
  86. def updateMember( fobNumber, fobField, lastUnlockField ):
  87.     print "updateMember connecting to db"
  88.     conn, cursor = connect()
  89.     cursor.execute("update members set "+lastUnlockField+" =  now() where "+fobField+" = '"+fobNumber+"'")                                    
  90.     close(conn, cursor)
Advertisement
Add Comment
Please, Sign In to add comment