-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathWrite.py
53 lines (37 loc) · 1.33 KB
/
Write.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
#!/usr/bin/env python
import sqlite3
import secrets
# import RPi.GPIO as GPIO
# from mfrc522 import SimpleMFRC522
urlSafeKey = secrets.token_urlsafe()
# reader = SimpleMFRC522()
conn = sqlite3.connect('rfids.db')
c = conn.cursor()
try:
print("Please tap the RFID Tag to read the ID.")
ids, text = reader.read()
print("Success!")
print("Checking the database for ID.")
# Checking the database to see if the tag is there.
c.execute('SELECT * FROM rfid WHERE tagid=?;', (ids,))
returnedRecord = c.fetchone()
playlistName = input('Playlist Name:') # Requesting an playlist name from the command line.
url = input('URL: ')
# print(urlSafeKey)
print("Place your tag to write key")
reader.write(urlSafeKey)
print("Written")
# If the returnedRecord tagid value is blank then do an insert statement.
# Else do an update since the record already exists.
if returnedRecord is None:
# Set the record with all values needed to execute the insert statement.
record = [(None, ids, urlSafeKey, url, playlistName),]
# Execute the insert statment.
c.executemany('INSERT INTO rfid VALUES (?,?,?,?,?)', record)
else:
print("Tag Exists, updating Record.")
c.execute("UPDATE rfid SET uniqueid = ?, url = ?, playlistname = ? WHERE tagid = ?;", (urlSafeKey, url, playlistName, ids))
conn.commit()
conn.close()
finally:
GPIO.cleanup()