This repository was archived by the owner on Jun 10, 2025. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathids.py
More file actions
80 lines (63 loc) · 2.13 KB
/
ids.py
File metadata and controls
80 lines (63 loc) · 2.13 KB
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
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
import sqlite3
import os
import time # For Unix timestamp handling
import random
import string
try:
from prettytable import PrettyTable
except ImportError:
os.system("pip install prettytable")
from prettytable import PrettyTable
def id_generate(size, allowed_chars : str):
return ''.join(random.choice(allowed_chars) for x in range(size))
# Ten CSDL
db_name = "data"
# Ket noi vao CSDL SQLite
db = sqlite3.connect(f"{db_name}.db")
# Tao mot vat the de chay lenh SQL
cursor = db.cursor()
# Tao mot TABLE neu TABLE ids ko ton tai
cursor.execute('''
CREATE TABLE IF NOT EXISTS ids (
id TEXT NOT NULL UNIQUE,
type TEXT NOT NULL,
ep INTEGER NOT NULL,
donor TEXT NOT NULL,
donateday INTEGER NOT NULL
)
''')
# Retrieve and display data using PrettyTable
cursor.execute('SELECT * FROM ids')
rows = cursor.fetchall()
# Print the data in a pretty table
table = PrettyTable()
table.field_names = ["ID", "Type", "Episode", "Donor", "Day of donation"]
# Add rows to the PrettyTable
for row in rows:
table.add_row(row)
# Display the table
print(table)
cursor.execute("SELECT id FROM books")
fetch = cursor.fetchall()
print("Available books:", fetch)
ntype=str(input("Enter type of new item (BLANK IF IGNORE): "))
if (ntype != ""):
cursor.execute("SELECT title FROM books WHERE id=?", (ntype,))
print("Selected book type: "+str(cursor.fetchone()[0]))
nid = str(input("Enter id of new book (BLANK IF RANDOMIZED): "))
if nid == "": nid = id_generate(11, string.ascii_letters+string.digits)
print(f"New ID: [{nid}]")
nep = int(input("Enter episode: "))
donor = str(input("Enter donator: "))
# Insert sample data using current time for `borrow_day` and future time for `borrow_expire`
current_time = int(time.time()) # Get current Unix timestamp
data = (nid, ntype, nep, donor, current_time)
# Insert data into the table
cursor.execute('''
INSERT OR IGNORE INTO ids (id, type, ep, donor, donateday)
VALUES (?, ?, ?, ?, ?)
''', data)
# Commit the transaction and close the connection
db.commit()
# Close the database connection
db.close()