Search code examples
pythonmysqldatabaseraspberry-pirfid

how to make a different conditions with one input (python)


Can anyone help me with this coding?

I want to insert a RFID card data to a MySQL database. When i first tap the tag on RFID Reader, the program will insert the data to Masuk table When i tap the tag again, the program will insert the data to Keluar table.

Am i doing right with this code?

import MFRC522 
import signal
import time
import MySQLdb
import datetime

db = MySQLdb.connect(host='localhost',
                user='root',
                passwd='12345678',
                db='pa')<br>
cursor = db.cursor()

continue_reading = True
MIFAREReader = MFRC522.MFRC522()

cardA = [131,89,173,1,118]

def read ():
    read = 1

def end_read(signal, frame):


global continue_reading
  continue_reading = False
  print "Ctrl+C captured, ending read."
  MIFAREReader.GPIO_CLEEN()

signal.signal(signal.SIGINT, end_read)

while continue_reading:
  (status,TagType) = MIFAREReader.MFRC522_Request(MIFAREReader.PICC_REQIDL)
  if status == MIFAREReader.MI_OK:
    print "Card detected"
  (status,backData) = MIFAREReader.MFRC522_Anticoll()
  if status == MIFAREReader.MI_OK:
    print "Card read UID: "+str(backData[0])+""+str(backData[1])+""+str(backData$
    if backData == cardA:
        print "Selamat Datang Dheny"
        if (read == True):
            sql = """ INSERT INTO Masuk(Nama, No_ID, datetime) VALUES ('Dheny', $
        try:
            cursor.execute(sql)
            db.commit()
        except:
            db.rollback()
        read = False
        if (read == False):
                sql = """ INSERT INTO Keluar(Nama, No_ID, datetime) VALUES ('Dhe$
        try:
            cursor.execute(sql)
            db.commit()
        except:
            db.rollback()
        read = True

Solution

  • see the code to access and modify the global variable

    global count 
    
    
    def modi():
        global count # required to modify the global copy
        count=10
    
    def printc():
        if (count==10): # read it as a normal variable 
            print("yes")
    
    
    modi()
    printc()