How to fix a skipped for loop

-1

I am trying to run a program that retrieves data from a database and displays it on an LCD screen. The program keeps skipping a FOR loop. Not sure if it is related with an error I am getting regarding: local variable 'ext_id' referenced before assignment

The output for db_val is <sqlite3.Cursor object at 0x763869e0>

I have added print commands verify that it is being skipped.

from pad4pi import rpi_gpio
import sqlite3
import sys
import pyfingerprint
import I2C_LCD_driver
from time import *
import datetime
import hashlib
from pyfingerprint import PyFingerprint
import calendar
import time

###connceting to the lcd driver
mylcd = I2C_LCD_driver.lcd()

pressed_keys =''
pin="12345"
fun = ''
yer = ''
code = ''
cors_type = ''
branch = ''
last = ''
roll_id = ''
r = ''


def finger():
    ## Search for a finger
    ##

    ## Tries to initialize the sensor
    try:
        f = PyFingerprint('/dev/serial0', 57600, 0xFFFFFFFF, 0x00000000)

        if ( f.verifyPassword() == False ):
            raise ValueError('The given fingerprint sensor password is wrong!')
            startChoice()
    except Exception as e:
        print('The fingerprint sensor could not be initialized!')
        print('Exception message: ' + str(e))
        startChoice()

    ## Gets some sensor information
    print('Currently used templates: ' + str(f.getTemplateCount()))

    ## Tries to search the finger and calculate hash
    try:
        mylcd.lcd_clear()
        mylcd.lcd_display_string('Waiting for finger..',2)

        ## Wait that finger is read
        while ( f.readImage() == False ):
            pass

        ## Converts read image to characteristics and stores it in charbuffer 1
        f.convertImage(0x01)

        ## Searchs template
        result = f.searchTemplate()

        positionNumber = result[0]
        accuracyScore = result[1]

        if ( positionNumber == -1 ):
            mylcd.lcd_clear()
            mylcd.lcd_display_string('No match found!')
            sleep(2)
            mylcd.lcd_clear()
            startChoice()
        else:
            mylcd.lcd_clear()
            today_name = datetime.date.today()
            if (calendar.day_name[today_name.weekday()] != 'Sunday') and (calendar.day_name[today_name.weekday()] != 'Saturday'):
                ## Generating TIME VALUES
                now = datetime.datetime.now()
                my_time_string_10 = "11:30:00"
                my_time_string_12 = "13:30:00"
                my_time_string_13 = "14:29:59"
                my_time_string_14 = "15:30:00"
                my_time_string_16 = "17:00:01"
                time_10 = datetime.datetime.strptime(my_time_string_10, "%H:%M:%S")
                time_12 = datetime.datetime.strptime(my_time_string_12, "%H:%M:%S")
                time_13 = datetime.datetime.strptime(my_time_string_13, "%H:%M:%S")
                time_14 = datetime.datetime.strptime(my_time_string_14, "%H:%M:%S")
                time_16 = datetime.datetime.strptime(my_time_string_16, "%H:%M:%S")

                # I am supposing that the date must be the same as now
                time_10 = now.replace(hour=time_10.time().hour, minute=time_10.time().minute, second=time_10.time().second, microsecond=0)
                time_12 = now.replace(hour=time_12.time().hour, minute=time_12.time().minute, second=time_12.time().second, microsecond=0)
                time_13 = now.replace(hour=time_13.time().hour, minute=time_13.time().minute, second=time_13.time().second, microsecond=0)
                time_14 = now.replace(hour=time_14.time().hour, minute=time_14.time().minute, second=time_14.time().second, microsecond=0)
                time_16 = now.replace(hour=time_16.time().hour, minute=time_16.time().minute, second=time_16.time().second, microsecond=0)

                print('Found template at position #' + str(positionNumber))
                mylcd.lcd_display_string('PLEASE WAIT',2,3)
                sleep(1)
                mylcd.lcd_clear()

                ## Create Hash Value for finger
                ##

                ## Loads the found template to charbuffer 1
                f.loadTemplate(positionNumber, 0x01)

                ## Downloads the characteristics of template loaded in charbuffer 1
                characterics = str(f.downloadCharacteristics(0x01)).encode('utf-8')
                val_hash = hashlib.sha256(characterics).hexdigest()
                ## Hashes characteristics of template
                print('SHA-2 hash of template: ' + val_hash)

                ## GETTING INFORMATION FROM DATABASE

                conn = sqlite3.connect('/home/pi/attendance/app.db')
                curs = conn.cursor()
                db_val = curs.execute('SELECT rollnum, hashval FROM finger_store WHERE 
                hashval IN (values(?))', [val_hash])
                print('line 116')
                print(val_hash)
                print(db_val)

                for row in db_val:
                    ext_id = row[0]
                    mylcd.lcd_clear()
                    print('121')
                    mylcd.lcd_display_string("YOUR ID NUMBER:",2,2)
                    mylcd.lcd_display_string(ext_id,3,3)
                    sleep(2)
                    mylcd.lcd_clear()
                    print('126')

                print('127')
                conn.commit()
                conn.close()
                print('line 129')
                con = sqlite3.connect('/home/pi/attendance/app.db')
                print('134')
                curs2 = con.cursor()
                print('136')
                curs2.execute('SELECT date FROM attendance WHERE (date, rollnum) IN (values(?, ?))', (datetime.date.today(), ext_id))
                print('138')
python
sql
asked on Stack Overflow Apr 26, 2019 by Tony Rodriguez • edited Apr 26, 2019 by stovfl

0 Answers

Nobody has answered this question yet.


User contributions licensed under CC BY-SA 3.0