36 lines
933 B
Python
36 lines
933 B
Python
|
import sqlite3
|
||
|
import random
|
||
|
from time import sleep
|
||
|
|
||
|
# Create a SQLite database and table if not exists
|
||
|
conn = sqlite3.connect('random_numbers.db')
|
||
|
c = conn.cursor()
|
||
|
|
||
|
try:
|
||
|
c.execute("""CREATE TABLE IF NOT EXISTS random_numbers(id INTEGER PRIMARY KEY, number INT)""")
|
||
|
conn.commit()
|
||
|
except:
|
||
|
pass
|
||
|
|
||
|
def generate_and_update():
|
||
|
random_number = random.randint(32768, 65535)
|
||
|
c.execute("SELECT COUNT(*) FROM random_numbers WHERE number=?", (random_number,))
|
||
|
count = c.fetchone()[0]
|
||
|
|
||
|
if count == 0:
|
||
|
c.execute("INSERT INTO random_numbers(number) VALUES(?)", (random_number,))
|
||
|
conn.commit()
|
||
|
print(f"{random_number}")
|
||
|
else:
|
||
|
raise RuntimeError("No unique number found.")
|
||
|
|
||
|
def main():
|
||
|
try:
|
||
|
generate_and_update()
|
||
|
except RuntimeError as e:
|
||
|
print(e)
|
||
|
print("Try running the script again to get a new unique number.")
|
||
|
|
||
|
if __name__ == "__main__":
|
||
|
main()
|