-
Notifications
You must be signed in to change notification settings - Fork 1
/
database.py
185 lines (145 loc) · 5.63 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
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
#encoding:utf8
import sqlite3 as sql
import sys
import os.path
import re
# checks if device is mobile or desktop
def is_mobile(request):
ismobile = False
if request.headers.has_key('User-Agent'):
user_agent = request.headers['User-Agent']
# Test common mobile values.
patterns = "(xoom|up.browser|up.link|mmp|symbian|smartphone|phone|tablet|midp|wap|windows ce|pda|mobile|mini|palm|netfront|nokia)"
patt_compiled = re.compile(patterns, re.IGNORECASE)
match = patt_compiled.search(user_agent)
if match:
ismobile = True
return ismobile
# creates a new line in musics table
def create_song(name, notes):
command = 'INSERT INTO musics(name, notes) VALUES("' + name + '", "' + notes + '")'
db.execute(command)
db.commit()
#shows the all content from musics table
def get_all_notes():
command = 'SELECT * FROM musics'
result = db.execute(command)
rows = result.fetchall()
d = []
for row in rows:
name = {"id":row[0],"name":row[1],"notes":row[2]}
d.append(name)
return d
#shows the all content from interpretations table
def get_all_songs():
command = 'SELECT * FROM interpretations'
result = db.execute(command)
rows = result.fetchall()
d = []
for row in rows:
name = {"id":row[0],"id_music":row[1],"name":row[2],"registration":row[3],"effects":row[4],"upvotes":row[5],"downvotes":row[6]}
d.append(name)
return d
# shows all id's from musics table, raising IndexError exception when a sequence subscript is out of range
def last_id():
try:
result = db.execute("SELECT id FROM musics")
rows = result.fetchall()
x = []
for row in rows:
x.append(row[0])
return x[len(x)-1]+1
except IndexError, e:
return 1
# shows all id's from interpretations table, raising IndexError exception when a sequence subscript is out of range
def last_id_interpretations():
try:
result = db.execute("SELECT id FROM interpretations")
rows = result.fetchall()
x = []
for row in rows:
x.append(row[0])
return x[len(x)-1]+1
except IndexError, e:
return 1
# creates a new line in interpretations table
def create_interpretation(registration, effects,id_music,name):
command = 'INSERT INTO interpretations(id_music,name,registration, effects) VALUES("' + id_music + '","' + name + '","' + registration + '", "' + effects + '")'
db.execute(command)
db.commit()
# shows the all content from interpretations table
def get_interpretations():
command = 'SELECT * FROM interpretations'
db.execute(command)
# shows the content from musics table
def list_songs(music):
print ' ID / NAME / NOTES '
for row in musics:
print "%s / %s / %s".format(row[0], row[1], row[2])
# shows the notes from a song according to corresponding id, in musics table
def get_notes(ID):
command = 'SELECT notes FROM musics WHERE id =' + ID
result = db.execute(command)
return result.fetchone()
# shows the name and notes from a song according to corresponding id, , in musics table
def get_notes_and_name(ID):
command = 'SELECT name,notes FROM musics WHERE id =' + ID
result = db.execute(command)
return result.fetchone()
# update the name or notes from musics table according to corresponding id. checks if there is not empty spaces in order to update
def edit_musics(ID, name, notes):
if name != '':
command = 'UPDATE musics SET name = "' + name + '" WHERE id = ' + ID
db.execute(command)
if notes != '':
command = 'UPDATE musics SET notes = "' + notes + '" WHERE id = ' + Id
db.execute(command)
# update the registration or effects from interpretations table according to corresponding id. checks if there is not empty spaces in order to update
def edit_interpretations(ID, registration, effects):
if registration != '':
command = 'UPDATE interpretations SET registration = "' + registration + '" WHERE id = ' + ID
db.execute(command)
if effects != '':
command = 'UPDATE interpretations SET effects = "' + effects + '" WHERE id = ' + ID
db.execute(command)
# shows a specifif line from interpretations table according to corresponding id
def get_songs(ID):
command = 'SELECT * FROM interpretations WHERE id_music = ' + ID
result = db.execute(command)
rows = result.fetchall()
d = []
for row in rows:
name = {"id":row[0],"id_music":row[1],"name":row[2],"registration":row[3],"effects":row[4],"upvotes":row[5],"downvotes":row[6]}
d.append(name)
return d
#increases in one unity the posivite likes in interpretations table
def add_upvotes(ID):
command = 'SELECT upvotes FROM interpretations WHERE id = ' + str(ID)
result = db.execute(command)
result = result.fetchone()
result = result[0] + 1
command = 'UPDATE interpretations SET upvotes = "' + str(result) + '" WHERE id = ' + str(ID)
db.execute(command)
db.commit()
#increases in one unity the negative likes in interpretations table
def add_downvotes(ID):
command = 'SELECT downvotes FROM interpretations WHERE id = ' + str(ID)
result = db.execute(command)
result = result.fetchone()
result = result[0] + 1
command = 'UPDATE interpretations SET downvotes = "' + str(result) + '" WHERE id = ' + str(ID)
db.execute(command)
db.commit()
#cria uma nova songs database if no one is available. needs create.txt file to work properly.
if not os.path.isfile('songs.db'):
if not os.path.isfile('create.txt'):
print 'We need a database to run the program. We don\'t have one, so we need a "create.txt" file to create the library. Copy a valid "create.txt" file to this folder and reopen the program.'
exit(1)
db = sql.connect('songs.db')
createDBfile = open('create.txt', 'r')
for line in createDBfile:
db.execute(line)
db.commit()
createDBfile.close()
db.close()
db = sql.connect('songs.db',check_same_thread=False)