-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathdb.py
More file actions
211 lines (193 loc) · 5.43 KB
/
db.py
File metadata and controls
211 lines (193 loc) · 5.43 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
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
import psycopg2
# import pdb
from os import getenv
from dotenv import load_dotenv
from pathlib import Path
from psycopg2.extensions import ISOLATION_LEVEL_AUTOCOMMIT
from datetime import datetime
load_dotenv(verbose=True)
def connect():
con = psycopg2.connect(
host="localhost",
database="discordbot",
user=getenv("POSTGRES_USER"),
password=getenv("POSTGRES_PASS")
)
return con
def disconnect(con):
if con is not None:
con.close()
def db_test():
con = None
try:
con = psycopg2.connect(
host="localhost",
user=getenv("POSTGRES_USER"),
password=getenv("POSTGRES_PASS")
)
cursor = con.cursor()
cursor.close()
print('DB Connected')
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
disconnect(con)
def create_database():
con = None
try:
con = connect()
con.set_isolation_level(ISOLATION_LEVEL_AUTOCOMMIT)
cursor = con.cursor()
name_Database = "discordbot"
sqlCreateDatabase = "create database "+name_Database+";"
cursor.execute(sqlCreateDatabase)
cursor.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
disconnect(con)
def create_table(commands):
con = None
try:
con = connect()
cursor = con.cursor()
for command in commands:
cursor.execute(command)
cursor.close()
con.commit()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
disconnect(con)
def create_table_bingo_words():
commands = (
"""
CREATE TABLE wordbingo (
bingo_word VARCHAR(225) PRIMARY KEY,
bingo_scored BOOL NOT NULL,
bingo_stamp DATE
)
""",
)
create_table(commands)
def insert_bingo_words(word):
sql = "INSERT INTO wordbingo(bingo_word, bingo_scored, bingo_stamp) VALUES(%s, %s, %s);"
command = (word, False, datetime.today().strftime('%Y-%m-%d'))
con = None
try:
con = connect()
cursor = con.cursor()
cursor.execute(sql, command)
cursor.close()
con.commit()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
disconnect(con)
def remove_bingo_words(word):
sql = "DELETE FROM wordbingo WHERE bingo_word = %s;"
con = None
try:
con = connect()
cursor = con.cursor()
cursor.execute(sql, (word,))
cursor.close()
con.commit()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
disconnect(con)
def get_bingo_words():
con = None
try:
con = connect()
cursor = con.cursor()
cursor.execute("SELECT bingo_word, bingo_scored, bingo_stamp FROM wordbingo")
words = cursor.fetchall()
cursor.close()
return words
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
disconnect(con)
def get_bingo_result(word):
con = None
try:
con = connect()
cursor = con.cursor()
cursor.execute("SELECT bingo_word, bingo_scored, bingo_stamp FROM wordbingo WHERE bingo_word = %s", (word,))
word = cursor.fetchone()
cursor.close()
return word
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
disconnect(con)
def update_bingo_words(bingo_word, bingo_scored):
sql = """ UPDATE wordbingo
SET bingo_scored = %s, bingo_stamp = %s
WHERE bingo_word = %s"""
command = (bingo_scored, datetime.today().strftime('%Y-%m-%d'), bingo_word)
con = None
try:
con = connect()
cursor = con.cursor()
cursor.execute(sql, (command))
con.commit()
cursor.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if con is not None:
con.close()
def reset_bingo_words():
sql = """ UPDATE wordbingo
SET bingo_scored = False
WHERE bingo_scored = True"""
con = None
try:
con = connect()
cursor = con.cursor()
cursor.execute(sql)
con.commit()
cursor.close()
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if con is not None:
con.close()
def count_bingo_words(state):
sql = """ SELECT COUNT(*) FROM wordbingo WHERE bingo_scored = %s"""
con = None
try:
con = connect()
cursor = con.cursor()
cursor.execute(sql, (state,))
con.commit()
count = int(cursor.fetchone()[0])
cursor.close()
return count
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if con is not None:
con.close()
def count_all_bingo_words():
sql = """ SELECT COUNT(*) FROM wordbingo"""
con = None
try:
con = connect()
cursor = con.cursor()
cursor.execute(sql)
con.commit()
count = int(cursor.fetchone()[0])
cursor.close()
return count
except (Exception, psycopg2.DatabaseError) as error:
print(error)
finally:
if con is not None:
con.close()
if __name__ == '__main__':
create_database()
create_table_bingo_words()