- html - 出于某种原因,IE8 对我的 Sass 文件中继承的 html5 CSS 不友好?
- JMeter 在响应断言中使用 span 标签的问题
- html - 在 :hover and :active? 上具有不同效果的 CSS 动画
- html - 相对于居中的 html 内容固定的 CSS 重复背景?
我不确定如何仅使用时间字段来完成对数据库的过滤。现在我有一个名为 DatabasePolgygon
class DatabasePolygon(dbBase):
__tablename__ = 'objects'
begin_time = Column(DateTime) # starting time range of shape
end_time = Column(DateTime) # ending time range of shape
# Other entries not relevant to this question
begin_time 和 end_time 可能等于 2006-06-01 14:45:23
等值,它们表示对象(在本例中为形状超过情节)涵盖。我想允许对我的用户进行高级搜索,特别是询问在某个时间范围内出现的所有对象。但是,如何使用 DateTime
字段完成此操作?
# Grab all shapes that appear above this certain time
query_result = query_result.filter(
DatabasePolygon.begin_time >= datetime.strptime(rng['btime']), %H:%M:%S')
)
问题是我将带有 Y-m-d H-M-S
的日期时间对象与仅带有 H-M-S
的对象进行比较。一个示例场景是,如果用户想要所有出现在 14:45:24 范围之外的对象,而不考虑年/月/日,那么我们将有 rng['btime']=14:45: 24
和 begin_time=2006-06-01 14:45:23
比较时似乎实际上没有过滤任何东西。
有什么方法可以高效比较此数据列中的时间?我希望能够做类似的事情
# Grab all shapes that appear above this certain time
query_result = query_result.filter(
DatabasePolygon.begin_time.time() >= datetime.strptime(rng['btime']), %H:%M:%S').time()
)
最佳答案
这似乎是可能的,但要满足一些条件。
目标 1:(完全)做到这一点。
使用名为 Thing
的类来保存“objects”表中的 id
和 begin_time
值:
class Thing(Base):
__tablename__ = 'objects'
id = Column(Integer, primary_key=True)
begin_time = Column(DateTime)
def __repr__(self):
return "<Thing(id=%d, begin_time='%s')>" % (self.id, self.begin_time)
并在SQLite数据库的“objects”表中测试数据
id begin_time
-- -------------------
1 1971-01-14 17:21:53
2 1985-05-24 10:11:12
3 1967-07-01 13:14:15
不幸的是,这不起作用:
engine = create_engine(r'sqlite:///C:\__tmp\test.db', echo=True)
Session = sessionmaker(bind=engine)
session = Session()
for instance in session.query(Thing)\
.filter(Thing.begin_time[11:]<'17:00:00')\
.order_by(Thing.id):
print(instance)
生产
NotImplementedError: Operator 'getitem' is not supported on this expression
但是,这确实有效......
engine = create_engine(r'sqlite:///C:\__tmp\test.db', echo=True)
conn = engine.connect()
result = conn.execute("SELECT id FROM objects WHERE substr(begin_time,12)<'17:00:00'")
id_list = [row[0] for row in result.fetchall()]
result.close()
conn.close()
Session = sessionmaker(bind=engine)
session = Session()
for instance in session.query(Thing)\
.filter(Thing.id.in_(id_list))\
.order_by(Thing.id):
print(instance)
目标 2:高效。
控制台输出显示第一个 SELECT 确实是
SELECT id FROM objects WHERE substr(begin_time,12)<'17:00:00'
所以如果我们使用 SQLite 3.9.0 或更高版本并且创建了一个“表达式索引”
CREATE INDEX time_idx ON objects(substr(begin_time,12));
那么 SQLite 就可以避免表扫描。不幸的是,即使是目前最新版本的 CPython 2.7 (2.7.11) 仍然附带一个太旧的 sqlite3
模块
Python 2.7.11 (v2.7.11:6d1b6a68f775, Dec 5 2015, 20:32:19) [MSC v.1500 32 bit (Intel)] on win32
>>> import sqlite3
>>> sqlite3.sqlite_version
'3.6.21'
因此索引不能存在于数据库中,否则 SQLAlchemy 会阻塞它:
sqlalchemy.exc.DatabaseError: (sqlite3.DatabaseError) malformed database schema (time_idx) - near "(": syntax error [SQL: "SELECT id FROM objects WHERE substr(begin_time,12)<'17:00:00'"]
因此,如果“高效”部分真的很重要,那么您可能需要说服 Python 使用更新版本的 SQLite。可以在问题中找到一些指导
Force Python to forego native sqlite3 and use the (installed) latest sqlite3 version
关于python - 按时间过滤sqlalchemy sqlite datetime列,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34845124/
我正在尝试创建一个使用 UUID 作为主键的用户模型: from src.db import db # SQLAlchemy instance import sqlalchemy_utils impo
在 sqlalchemy 中,我试图合并表,然后使用 WHERE 和 ORDER_BY 创建别名 有点像 SELECT * FROM ( SELECT [TABLE_ONE].[SOME_ID]
我正在使用 SQL Alchemy(通过 Flask_sqlalchemy)将 Python 字典列表插入到 Postgres 数据库中。 其中一个表是所有唯一项目的列表(表 1),而第二个是与某个项
This source详细说明如何使用关联代理创建具有 ORM 对象值的 View 和对象。 但是,当我附加一个与数据库中现有对象匹配的值(并且所述值是唯一的或主键)时,它会创建一个冲突的对象,因此我
SQLAlchemy Core和SQLAlchemy ORM的目的有什么区别? 最佳答案 顾名思义,ORM是一个对象关系映射器:其目的是将数据库关系表示为Python对象。 核心是查询构建器。其目的是
带有ForeignKey的Column是否自动创建索引? 还是我需要手动添加index=True? some_field = Column(Integer, ForeignKey(SomeModel.
我有一个主数据库,每个客户自己的数据库连接存储在其中。 因此,每个客户端都使用2个db:main和它自己的db,必须确定其连接 对于每个http调用。我如何使用flask-sqlalchemy扩展名执
当我仅对类进行继承时,它才起作用 class User(Base): __tablename__ = ’users’ id = Column(Integer, primary_key=
从用户的角度来看,SQLAlchemy 的查询日志似乎有点过于冗长,有时甚至有点神秘: 2015-10-02 13:51:39,500 INFO sqlalchemy.engine.base.Engi
我正在尝试使用 wtforms.ext.sqlalchemy QuerySelectMultipleField 显示复选框列表,但我无法在 GET 的表单上显示模型数据。 这是我的models.py
我想为查询返回一个中继连接。使用标准的 graphene-sqlalchemy 你可以这样做: class Query(graphene.ObjectType): node = relay.N
我在 centos 7.5 虚拟机上部署了最新的 Airflow ,并将 sql_alchemy_conn 和 result_backend 更新到 postgresql 实例上的 postgres
我想将多个项目插入到一个表中,并在发生冲突时更新该表。这是我想出的以下内容 from sqlalchemy.dialects.postgresql import insert meta = MetaD
我有以下模型: class Item(Base): a = relationship() b = relationship() c = relationship() d
我有 presto 和 superset 设置。 presto 运行良好,可以通过命令访问: ./app/hadoop/setjdk8.sh;bin/presto-cli --server http:
我一直在寻找一种在 sqlalchemy 中使用 tsvector 的方法(就像 INTEGER 等其他方法一样),但到目前为止我还不清楚如何做到这一点。我读过可以使用 UserDefinedType
我正在使用 sqlalchemy(现在使用 sqlite,但稍后可能会改变)来构建一个数据库,其中插入的顺序和 rowids 很重要。我基本上有以下几点: class Message(Base):
给定一个对象,我想知道如何知道它是否是 sqlalchemy 映射模型的实例。 通常,我会使用 isinstance(obj, DeclarativeBase)。但是,在这种情况下,我没有可用的 De
我已经通读了查询文档,如果有办法从查询中获取表名,就看不到任何地方 - 例如如果我有 q = query(Users) ,我可以得到Users从 q 退出? 最佳答案 请注意,像您这样的事件简单查询可
我不确定如何定义create schema foo迁移?我的模型如下所示(我正在使用Flask-Migrate): class MyTable(db.Model): __tablename__
我是一名优秀的程序员,十分优秀!