-
Notifications
You must be signed in to change notification settings - Fork 2
/
Copy pathdatabase.py
180 lines (116 loc) · 4.87 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
#!/usr/bin/env python
#-----------------------------------------------------------------------
# database.py
# Author: Bob Dondero
#-----------------------------------------------------------------------
from sqlite3 import connect
from sys import stderr
from os import path
from entry import Entry
import sqlite3
import json
#-----------------------------------------------------------------------
class Database:
def __init__(self):
self._connection = None
def connect(self):
self._connection = sqlite3.connect('entries.db')
def disconnect(self):
self._connection.close()
# enty is Entry object from entry.py
def insertEntry(self, entry):
try:
cursor = self._connection.cursor()
latitude, longitude = entry.coordinates()
values = (entry.placename, entry.location, entry.description, entry.image, latitude, longitude)
QUERY_STRING = ''' INSERT INTO entries(placename,location,description, imageLocation, latitude, longitude)
VALUES(?,?,?,?,?,?) '''
cursor.execute(QUERY_STRING, (values))
self._connection.commit()
return cursor.lastrowid
except Exception as e:
print(e)
# searchEntry returns all rows, fields that contain nameQuery or locQuery (name or location)
# returns list of Entry objects
# we probably don't need this method actually
def searchEntry(self, nameQuery='', locQuery=''):
cursor = self._connection.cursor()
# uppercase everything to make search case insensitive
if (nameQuery == ''):
name = '%'
else:
name = '%' + nameQuery.upper() + '%'
if (locQuery== ''):
location = '%'
else:
location = '%' + locQuery.upper() + '%'
QUERY_STRING = """ SELECT placename, location, description, imagelocation from entries
WHERE UPPER(placename) like ? AND
UPPER(location) like ?
"""
cursor.execute(QUERY_STRING, (name, location))
rows = cursor.fetchall()
def makeEntry(row):
return Entry(placename=row[0], location=row[1], description=row[2], image=row[3])
entries = map(makeEntry, rows)
return list(entries)
# takes all entries in the database and returns a json object that you can send to the frontend
# each element in this json list is an entry with fields placename, location, description, imagelocation, latitude, longitude
def allEntries(self):
cursor = self._connection.cursor()
QUERY_STRING = """ SELECT placename, location, description, imagelocation, latitude, longitude from entries
"""
cursor.execute(QUERY_STRING)
row = cursor.fetchone()
entries = []
while row is not None:
entry = {}
entry['placename'] = row[0]
entry['location'] = row[1]
entry['description'] = row[2]
entry['imagelocation'] = row[3]
entry['latitude'] = row[4]
entry['longitude'] = row[5]
entries.append(entry)
row = cursor.fetchone()
return json.dumps(entries)
# only use this if you need to change the table
def changeTable(self):
try:
cursor = self._connection.cursor()
QUERY_STRING = ''' DELETE FROM entries'''
cursor.execute(QUERY_STRING)
self._connection.commit()
return
except Exception as e:
print(e)
# dont' call this again, i already made a table
def createTable():
con = sqlite3.connect('entries.db')
cur = con.cursor()
# Create table
cur.execute(""" CREATE TABLE IF NOT EXISTS entries (
id integer PRIMARY KEY,
placename text NOT NULL,
location text,
description text,
imagelocation text,
latitude float,
longitude float
); """)
# Save (commit) the changes
con.commit()
# We can also close the connection if we are done with it.
# Just be sure any changes have been committed or they will be lost.
con.close()
def main():
# inserted this entry already
entry1 = Entry(placename='Ho Family', location='Garden Grove', description='cool stuff')
database = Database()
database.connect()
print(database.allEntries())
database.disconnect()
#-----------------------------------------------------------------------
# For testing:
if __name__ == '__main__':
main()