-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathQuery.py
More file actions
123 lines (110 loc) · 3.91 KB
/
Query.py
File metadata and controls
123 lines (110 loc) · 3.91 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
import sqlite3
conn = sqlite3.connect('destinations.db')
cursor = conn.cursor()
class Query:
def __init__(self):
self.selected_item = None
def byName(self):
"""
Query to get all the first letters when clicked on the name button from db
"""
cursor.execute('''
SELECT DISTINCT SUBSTR(destination_name, 1, 1) AS first_char
FROM destinations
ORDER BY first_char
''')
distinct_first_chars = cursor.fetchall()
dest_char_list = [char[0] for char in distinct_first_chars]
return dest_char_list
def byMonth(self):
"""
Query to get all months when clicked on the month button from db
"""
cursor.execute('''
SELECT month
FROM months
ORDER BY month_num
''')
distinct_months = cursor.fetchall()
month_list = [month[0] for month in distinct_months]
return month_list
def byRank(self):
"""
Query to get all rank numbers when clicked on the rank button from db
"""
cursor.execute('''
SELECT DISTINCT ranking AS rank
FROM destinations
ORDER BY rank
''')
distinct_ranks = cursor.fetchall()
rank_list = [rank[0] for rank in distinct_ranks]
return rank_list
def destination_by_name(self, dest_char):
"""
Query to get the destination by name from the db when selecting
an option from the dialog window
"""
cursor.execute('''
SELECT DISTINCT destination_name AS dest_name
FROM destinations
WHERE SUBSTR(destination_name, 1, 1) = ?
''', (dest_char,))
distinct_destinations = cursor.fetchall()
destination_list = [destination[0] for destination in distinct_destinations]
return destination_list
def destination_by_month(self, mnth):
"""
query to get the destination by month from the db when selecting
an option from the dialog window
"""
cursor.execute('''
SELECT DISTINCT destination_name, ranking AS dest_name
FROM destinations
WHERE month = ?
ORDER BY ranking
''', (mnth,))
distinct_destinations = cursor.fetchall()
destination_list = [f'{destination[1]}. {destination[0]}' for destination in distinct_destinations]
return destination_list
def destination_by_rank(self, rank):
"""
query to get the destination by rank from the db when selecting
an option from the dialog window
"""
cursor.execute('''
SELECT DISTINCT destination_name, month AS dest_name
FROM destinations
WHERE ranking = ?
ORDER BY month_num
''', (rank,))
distinct_destinations = cursor.fetchall()
destination_list = [f'{destination[0]}: {destination[1]}' for destination in distinct_destinations]
return destination_list
def destination_description(self, selected_item , param):
"""
- query for selected item when the user clicks on a choice in the listbox
- gets summary and url details from the user selected item
"""
self.param = param
if self.param == 'Name':
dest = selected_item
elif self.param == 'Month':
dest = selected_item.split('.')[1].strip()
elif self.param == 'Rank':
dest = selected_item.split(':')[0].strip()
cursor.execute('''
SELECT summary, url
FROM destinations
WHERE destination_name = ?
AND url IS NOT NULL
LIMIT 1
''', (dest,))
destination_summary = cursor.fetchall()
dest_summary = None
dest_url = None
for destination in destination_summary:
dest_summary = destination[0]
dest_url = destination[1]
# print(dest_summary, dest_url)
return dest_summary, dest_url