Click here to Skip to main content
15,884,939 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
Hello I write code read RFID tag using python,check the existence of the ID in the database.
It successfully, right ID on database led is ON, but command insert to database not work. Python no error message, and I do not understand what's wrong with it.
How can I fix it? Please help me. Thank you very much
Here is my code:
Python
import serial
import time
import RPi.GPIO as GPIO
import MySQLdb
from datetime import datetime
from binascii import hexlify
serial=serial.Serial("/dev/ttyAMA0",                    
		      baudrate=9600,                     
                      parity=serial.PARITY_NONE,                    
                      stopbits=serial.STOPBITS_ONE,                    
                      bytesize=serial.EIGHTBITS,
                      timeout=0.1) 
GPIO.setmode(GPIO.BOARD) 
GPIO.setup(12, GPIO.OUT)
chuoi= "\xAA\x00\x03\x25\x26\x00\x00\xBB"
db = MySQLdb.connect(host='localhost', user='root', passwd='root', db='ex_1')
cur = db.cursor(MySQLdb.cursors.DictCursor)
while 1:   
	serial.write(chuoi)      
        data =serial.readline()    
        tach = data[5:8]    
        hex = hexlify(tach)   
        num = int(hex,16)    
        print(num)      
        cur.execute("SELECT * FROM thong_tin WHERE Card_ID = '%d'" %num)    
        rows = cur.fetchall()    
        for row in rows:	
		ID = row['Card_ID']	 	
                if( num == ID):		
			GPIO.output(12, True)
                        cur.execute("INSERT INTO data (Card_ID,Time_out,Time_in) VALUES ('%d',NOW(),NOW())" %num) #-> it does not add to database	
                        time.sleep(3)		
                        GPIO.output(12, False);   	
                else:		
                        GPIO.output(12, False);
Posted

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900