【发布时间】:2016-04-10 05:26:09
【问题描述】:
我的数据库中有一个名为coordinates 的列,现在坐标列包含有关对象在我的图表中占用的时间范围的信息。我想让用户按日期过滤,但问题是我使用函数来正常确定日期。采取:
# query_result is the result of some filter operation
for obj in query_result:
time_range, altitude_range = get_shape_range(obj.coordinates)
# time range for example would be "2006-06-01 07:56:17 - ..."
现在,如果我想按日期过滤,我希望是 like:
query_result = query_result.filter(
DatabaseShape.coordinates.like('%%%s%%' % date))
但问题是我首先需要将get_shape_range 应用于coordinates 才能接收字符串。有什么办法……我猜是transform_filter操作?这样在like 发生之前,我将一些函数应用于坐标?在这种情况下,我需要编写一个只返回时间的get_time_range 函数,但问题仍然存在。
编辑:这是我的数据库类
class DatabasePolygon(dbBase):
__tablename__ = 'objects'
id = Column(Integer, primary_key=True) # primary key
tag = Column(String) # shape tag
color = Column(String) # color of polygon
time_ = Column(String) # time object was exported
hdf = Column(String) # filename
plot = Column(String) # type of plot drawn on
attributes = Column(String) # list of object attributes
coordinates = Column(String) # plot coordinates for displaying to user
notes = Column(String) # shape notes
lat = Column(String)
@staticmethod
def plot_string(i):
return constants.PLOTS[i]
def __repr__(self):
"""
Represent the database class as a JSON object. Useful as our program
already supports JSON reading, so simply parse out the database as
separate JSON 'files'
"""
data = {}
for key in constants.plot_type_enum:
data[key] = {}
data[self.plot] = {self.tag: {
'color': self.color,
'attributes': self.attributes,
'id': self.id,
'coordinates': self.coordinates,
'lat': self.lat,
'notes': self.notes}}
data['time'] = self.time_
data['hdfFile'] = self.hdf
logger.info('Converting unicode to ASCII')
return byteify(json.dumps(data))
我使用的是 sqlite 3.0。大多数东西背后都是字符串的原因是因为我要存储在数据库中的大多数值都是作为字符串发送的,所以存储是微不足道的。我想知道我是否应该使用 before 函数来完成所有这些解析魔法,并且只拥有更多的数据库条目?对于像十进制 time_begin、time_end、latitude_begin 这样的东西,而不是包含我解析的 time 范围的字符串在过滤时找到 time_begin 和 time_end
【问题讨论】:
-
我认为最好的方法是将坐标作为类并将其作为数据库的元素,在那里您可以在坐标上添加所需的方法,而无需调用像 DatabaseShape.coordinates.get_shape_range 这样的更改数据库跨度>
-
@Eliethesaiyan ,如果我将一个类作为我数据库的一个元素,我可以做类似
DatabaseShape.coordinates.get_shape_range.like('date')?但是在我的情况下 get_shape_range 不会返回一个字符串,所以你不能只使用.like吗? -
您使用的是哪个数据库?
coordinates的内容是什么样的,是字符串吗? -
您正在尝试完成数据库的工作 - 过滤“query_result”。我会让数据库为你做这件事。在数据库端进行过滤。你用的是什么类型的数据库?你能对表格结构有所了解吗?使用适当的数据模型会相当容易。
-
"大多数东西背后都是字符串的原因是因为我要存储在数据库中的大多数值都是作为字符串发送的,所以存储是微不足道的。"正如您所发现的那样,考虑如何为分析目的构建数据并非易事。
标签: python python-2.7 sqlalchemy