Advertisement
Guest User

Untitled

a guest
Nov 7th, 2017
106
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
Python 1.39 KB | None | 0 0
  1. KB
  2. # -*- coding: utf-8 -*-
  3. import serial
  4. import mysql.connector
  5.  
  6. SERIALPORT = "/dev/tty.wchusbserial410" #serial port on mac
  7. SERIALRATE = 9600
  8.  
  9. ser = serial.Serial(SERIALPORT, SERIALRATE)
  10. conn = mysql.connector.connect(host='localhost', database='dbTeste', user='root', password='root')
  11. if conn.is_connected():
  12.     print('Connected to MySQL database')
  13.  
  14. cursor = conn.cursor()
  15. print ("connection OK, cursor OK")
  16.  
  17. while 1:
  18.     serial_line = ser.readline()
  19.     if(serial_line != "GO_BLINK"):
  20.         IDCardReadLine = ser.readline()
  21.         print(IDCardReadLine) #del this line - only show the tag ID
  22.        
  23.         sqlSearchCardID = "SELECT idUser, nameUser, userCardID, isActive FROM Users WHERE userCardID LIKE = %s" %(IDCardReadLine)
  24.         sqlSearchName = "SELECT nameUser FROM Users WHERE userCardID LIKE = %s" %(IDCardReadLine)
  25.  
  26.         if(cursor.execute(sqlSearchCardID) != '' and cursor.execute(sqlSearchName) != ''):
  27.             sqlQueryInsertData = "INSERT INTO Records (idRecord, recordData, recordDateTime, userCardID) VALUES (NULL, 'Registo ok por: %s', CURRENT_TIMESTAMP, '%s')" %(IDCardReadLine, IDCardReadLine)
  28.             cursor.execute(sqlQueryInsertData)
  29.             conn.commit()
  30.         else:
  31.             print "Error"
  32.            
  33.     ser.flush()
  34.  
  35.  
  36. cursor.close()
  37. ser.close()
  38. conn.close();
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement