psycopg2:存储周数的间隔类型
我正在使用 postgresql 和 python,我需要按一年中的每周组存储数据。因此,有很多选择:
- 两个单独字段中的周和年、
- 指向一周开始的日期(或一周中的随机一天)
- 以及我喜欢的:间隔类型。
我从来没有使用过它,但阅读文档似乎很合适。但是,阅读 psycopg 文档时,我发现间隔映射到 python timedelta 对象...对我来说似乎很奇怪, timedelta 只是一个区别。
所以,这里确实有两个问题:
- 我可以使用 psycopg2 处理这个选择吗?
- 是更好的选择吗?
谢谢
I'm using postgresql and python and I need to store data group by week of the year. So, there's plenty alternatives:
- week and year in two separated fields
- a date pointing to the start of the week (or a random day of the week)
- And, the one I like: an interval type.
I never use it, but reading the docs, seems to fit. But then, reading psycopg docs I found interval mapped to python timedelta object... seems weird to me, a timedelta is just a difference.
So, there are two question here, really:
- Can I handle this choice using psycopg2?
- Is the better alternative?
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
PostgreSQL 间隔类型并不是您真正想要的——它专门用于存储任意长度的时间,范围从一微秒到几百万年。区间没有起点或终点;这只是“多长时间”的衡量标准。
如果您特别想要存储与事件相关的周,那么您最好选择前两个选项之一。
The PostgreSQL interval type isn't really what you're looking for -- it's specifically intended for storing an arbitrary length of time, ranging anywhere from a microsecond to a few million years. An interval has no starting or ending point; it's just a measure of "how long".
If you're specifically after storing which week an event is associated with, you're probably better off with either of your first two options.