Repository navigation
Expand file tree
/
Copy pathdatabase.py
More file actions
executable file
·191 lines (153 loc) · 5.35 KB
/
Copy pathdatabase.py
File metadata and controls
executable file
·191 lines (153 loc) · 5.35 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
from sqlalchemy import Column, ForeignKey, Integer, String, REAL
from sqlalchemy.ext.declarative import declarative_base
from sqlalchemy.orm import relationship, sessionmaker
from sqlalchemy import create_engine
# import Owner
Base = declarative_base()
class Product(Base):
__tablename__ = 'Product'
id = Column(Integer, primary_key=True)
name = Column(String)
owner = Column(String)
price = Column(REAL)
description = Column(String)
img = Column(String)
def dictify(self):
return {
'id': self.id,
'name': self.name,
'owner': self.owner,
'price':self.price,
'description':self.description,
'img' : self.img
}
class Owner(Base):
__tablename__ = 'User'
id = Column(Integer, primary_key=True)
name = Column(String)
lon = Column(REAL)
lat = Column(REAL)
info = Column(String)
def get_loc(self):
return (self.lon, self.lat)
class Data(object):
def createTable(self):
engine = create_engine('sqlite:///data.db')
Base.metadata.create_all(engine)
def insertItem(self, name, owner, price, description, img):
engine = create_engine('sqlite:///data.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
product = Product(name=name, owner=owner, price=price, description=description, img=img)
session.add(product)
session.commit()
session.close()
def searchItemByName(self, name):
print("\nSearch for item: " + name + "...")
if name == '':
return []
engine = create_engine('sqlite:///data.db')
Base.metadata.bind = engine
DBSession = sessionmaker()
DBSession.bind = engine
session = DBSession()
data = session.query(Product).filter(Product.name.contains(name))
session.close()
return data
def searchItemById(self, id):
print("\nSearch for item with id: " + str(id) + "...")
engine = create_engine('sqlite:///data.db')
Base.metadata.bind = engine
DBSession = sessionmaker()
DBSession.bind = engine
session = DBSession()
data = session.query(Product).filter(Product.id == id)
session.close()
return data
def searchItemByOwnerName(self, ownername):
engine = create_engine('sqlite:///data.db')
Base.metadata.bind = engine
DBSession = sessionmaker()
DBSession.bind = engine
session = DBSession()
data = session.query(Product).filter(Product.owner == ownername)
session.close()
return data
def searchItemByOwnerId(self, id):
engine = create_engine('sqlite:///data.db')
Base.metadata.bind = engine
DBSession = sessionmaker()
DBSession.bind = engine
session = DBSession()
data = session.query(Product).filter( Product.owner == session.query(Owner).get(id).name)
session.close()
return data
def deleteItem(self, id):
engine = create_engine('sqlite:///data.db')
Base.metadata.bind = engine
DBSession = sessionmaker()
DBSession.bind = engine
session = DBSession()
data = session.query(Product).filter_by(id=id).first()
session.delete(data)
session.commit()
session.close()
def createOwner(self, name, lon, lat, info):
engine = create_engine('sqlite:///data.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
product = Owner(name=name, lon=lon, lat=lat, info=info)
session.add(product)
session.commit()
session.close()
def findOwner(self, name):
engine = create_engine('sqlite:///data.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
owners = session.query(Owner).filter_by(name=name)
session.commit()
session.close()
return owners
def updateItem(self, id, name, owner_name, price, description, img):
engine = create_engine('sqlite:///data.db')
Base.metadata.bind = engine
DBSession = sessionmaker(bind=engine)
session = DBSession()
session.query(Product).filter_by(id=id).update(
{'name': name, 'price': price, 'owner':owner_name, 'description': description, 'img':img})
session.commit()
session.close()
# data = Data()
# try:
# data.createTable()
# except:
# print("Table already there!")
#
# def tryInsert(name, owner, price, description):
# try:
# data.insertItem(name, owner, price, description)
# print("Item insert succeed!")
# except:
# print("item already there!")
#
# def tryDelete(id):
# try:
# data.deleteItem(id)
# print("Item:" , "id:" + str(id), "delete success")
# except:
# print("Item does not exist:" , "id:" + str(id))
#
# tryInsert('mobile phone', "JB-hifi", '100', 'cool')
# tryInsert('mobile phone', "JB-hifi", '99', 'cool')
# tryInsert('mobile phone', "JB-hifi", '98', 'cool')
# tryInsert('mobile phone', "JB-hifi", '97', 'cool')
# # tryDelete('005')
# tryInsert('mobile phone', "JB-hifi", '96', 'cool')
#
#
# data.searchItem('mobile phone')
# data1 = data.searchItem('005')
# data.search_author('Tom')