Search code examples
pythonmysqltotp

Assign variable from database


I'm trying to create a python smart lock thing using rfid and the password instead of regular "1234", I'm using Time OTP with PyOTP Libray.

At the moment I'm stuck with how to assign the secret variable value at the if function using data from secret from table users.

How do I do assign the secret value from users table to the variable secret

IF FUNCTION

print("Place card near scanner")
id, text = reader.read()
cursor.execute("Select id From users Where rfid_uid="+str(id))
result = cursor.fetchone()

if cursor.rowcount > = 1:
   print("Welcome\nType the code from your authenticator app")
   secret = ('''The data from "secret" in users table''')

users Table

+----+--------------+-------------+------------------+---------------------+
| id | rfid_uid     | name        | secret           | created             |
+----+--------------+-------------+------------------+---------------------+
|  1 | 939940479059 | Blue Dongle | LONGSTRINGOFCODE | 2020-12-10 09:07:34 |
+----+--------------+-------------+------------------+---------------------+

Thank You


Solution

  • If you modify the select statement you should be able to select the secret in addition to the id.

    print("Place card near scanner")
    id, text = reader.read()
    cursor.execute("SELECT id, secret FROM users WHERE rfid_uid="+str(id))
    result = cursor.fetchone()
    
    if cursor.rowcount >= 1:
        print("Welcome\nType the code from your authenticator app")
        secret = result[1]