-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdatabase.py
More file actions
129 lines (103 loc) · 3.8 KB
/
Copy pathdatabase.py
File metadata and controls
129 lines (103 loc) · 3.8 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
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
import os
from sqlalchemy import ( # pylint: disable=unused-import
Column,
Float,
Integer,
String,
asc,
create_engine,
desc,
)
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import scoped_session, sessionmaker
# ----------------------------------------------------------------
# Clear Console
# ----------------------------------------------------------------
def clearConsole():
command = "clear"
if os.name in ("nt", "dos"): # If Machine is running on Windows, use cls
command = "cls"
os.system(command)
# ----------------------------------------------------------------
# SQLAlchemy
# ----------------------------------------------------------------
database_file = os.path.join(os.path.abspath(os.getcwd()), "database.db")
engine = create_engine("sqlite:///" + database_file, convert_unicode=True, echo=True)
db_session = scoped_session(
sessionmaker(autocommit=False, autoflush=False, bind=engine)
)
Base = declarative_base()
Base.query = db_session.query_property()
class Table(Base):
__tablename__ = "Table"
id = Column(Integer, primary_key=True)
server_id = Column(Integer, unique=False, nullable=False)
channel_id = Column(Integer, unique=False, nullable=False)
def __init__(self, server_id=None, channel_id=None):
self.server_id = server_id
self.channel_id = channel_id
Base.metadata.create_all(bind=engine)
# ----------------------------------------------------------------
# CRUD操作 (Create, read, update, delete)
# ----------------------------------------------------------------
# db_session.query(Table).filter().all()
# db_session.query(Table).limit(20).all()
# db_session.query(Table).first()
# db_session.query(Table).order_by(asc())
# db_session.query(Table).order_by(desc())
# ----------------------------------------------------------------
def create():
input_server_id = input("Server ID?: ")
input_channel_id = input("Channel ID?: ")
row = Table(server_id=input_server_id, channel_id=input_channel_id)
db_session.add(row)
db_session.commit()
def read():
db = db_session.query(Table).all()
print("----------------------------------------------------------------")
print("DATABASE INFOMATION")
print("----------------------------------------------------------------")
for row in db:
print("|", row.id, "|", row.server_id, "|", row.channel_id, "|")
print("----------------------------------------------------------------")
def update():
sel_row_num = input("Row ID?: ")
db = db_session.query(Table).filter(Table.id == sel_row_num).first()
db.server_id = input("Server ID?: ")
db.channel_id = input("Channel ID?: ")
db_session.commit()
def delete():
sel_row_num = input("Row ID?: ")
clearConsole()
if sel_row_num == "ALL DELETE":
print(
"""
----------------------------------------------------------------
NOTE: DO YOU WANT TO ALL DELETE IN THE DATABASE?
----------------------------------------------------------------
Yes: 1
No: 0
----------------------------------------------------------------
"""
)
sel_num = input("Number?: ")
if sel_num == "1":
clearConsole()
print(
"""
----------------------------------------------------------------
NOTE: IF YOU WANTED TO UNDO YOU CAN'T IT. ARE YOU SURE DELETE?
----------------------------------------------------------------
Yes: 1
No: 0
----------------------------------------------------------------
"""
)
note_sel_num = input("Number?: ")
if note_sel_num == "1":
clearConsole()
db_session.query(Table).delete()
db_session.commit()
else:
db_session.query(Table).filter(Table.id == sel_row_num).delete()
db_session.commit()