-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.py
executable file
·146 lines (120 loc) · 5.04 KB
/
database.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
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
#!/usr/bin/python
# -*- coding: utf-8 -*-
import _mysql
class Database():
#Class used to interact with the database
def __init__(self):
#Tries to get connection
try:
self.connection = _mysql.connect(host="",user="",passwd="",db="")
#print(self.connection) #Used to Debug
except _mysql.Error as e:
print("ERROR! %d: %s" % (e.args[0],e.args[1]))
def find_songs(self,username):
'''
Returns all songs in the songs table that have a specific username
'''
query = ("SELECT song_title FROM songs WHERE username='%s'" % username)
self.connection.query(query)
result = self.connection.store_result()
rows = result.fetch_row(maxrows=0,how=0)
# print("These are the songs that we've found for " + username + ":")
# for row in rows:
# print(row)
return rows
def add_to_songs(self,username,song_title,chords):
'''
Inserts data into the songs table
'''
query = ("INSERT INTO songs (id,username,song_title,chords) VALUES (%d,'%s','%s','%s')" % (0,username,song_title,chords)) #I made the ID zero because then we don't need to manually increment, it will automatically do it for us [SQL MAGICCCC!]
self.connection.query(query)
self.connection.commit()
def add_to_chords(self,chord,chord_pattern):
'''
Inserts data into the chords table
'''
query = ("INSERT INTO chords (id,chord,chord_pattern) VALUES (%d,'%s','%s')" % (0,chord,chord_pattern))
self.connection.query(query)
self.connection.commit()
def print_table(self,table):
'''
Prints all contents of a specific table
'''
query = ("select * from %s" % table)
self.connection.query(query)
result = self.connection.store_result()
rows = result.fetch_row(maxrows=0,how=0)
for row in rows:
print(row)
def reset_table(self,table):
'''
Deletes everything from a specific table
'''
query = ("DELETE from %s" % table)
self.connection.query(query)
self.connection.commit()
def get_song_chords(self, username):
'''
Returns a dictionary of the user's songs mapped to their respective chords
'''
#Dictionary that hold all of the song name and their respective chords
self.songs_to_chords = {}
query = ("SELECT song_title, chords FROM songs WHERE username = '%s'" % username)
self.connection.query(query)
result = self.connection.store_result()
rows = result.fetch_row(maxrows=0,how=0)
for row in rows:
if row[0] not in self.songs_to_chords:
self.songs_to_chords[row[0].decode("utf-8")]=row[1].decode("utf-8")
return self.songs_to_chords
def get_chord_pattern(self,chord):
query = ("SELECT chord_pattern FROM chords WHERE chord = '%s'" % chord)
self.connection.query(query)
result = self.connection.store_result()
rows = result.fetch_row(maxrows=0,how=0)
return rows[0][0].decode("utf-8")
def send_to_request(self,pattern):
query = ("INSERT INTO requests (id,code) VALUES (%d,'%s')" % (0,pattern))
self.connection.query(query)
def get_latest_request(self):
query = ("SELECT * FROM requests ORDER BY id DESC LIMIT 1;")
self.connection.query(query)
result = self.connection.store_result()
row = result.fetch_row(maxrows=0,how=0)
return [row[0][1].decode("utf-8"),row[0][2]]
def remove_song(self,username, song_title):
query = ("DELETE FROM songs WHERE username='%s' AND song_title='%s'" % (username,song_title))
self.connection.query(query)
#MAKE SURE TO CLOSE THE CONNECTION
#Testing the database
if __name__ == "__main__":
database = Database()
#adding in chords
# database.add_to_chords("C","0003")
# database.add_to_chords("G","0232")
# database.add_to_chords("F","2010")
# database.add_to_chords("D","2220")
# database.add_to_chords("Am","2000")
# database.add_to_chords("A","2100")
# database.add_to_chords("Dm","2210")
# database.add_to_chords("Bb","3211")
# database.add_to_chords("D7","2223")
# database.add_to_chords("G7","0212")
# database.add_to_chords("Em","0432")
# database.add_to_chords("E7","1202")
# database.add_to_chords("A7","0100")
# database.add_to_chords("Bm","4222")
# database.add_to_chords("C7","0001")
# database.add_to_chords("B","4322")
# database.add_to_chords("E","4442")
# database.add_to_chords("Eb","0331")
# database.add_to_chords("Fm","1013")
# database.add_to_chords("Gm","0231")
database.print_table("requests")
#Testing get_chord_pattern method
# print("Here is the chord A!")
# print(database.get_chord_pattern("A"))
# print("Here is the chord Am!")
# print(database.get_chord_pattern("Am"))
database.connection.close()
print("</html>")