基本上,我需要构建一个函数,根据给定的日期过滤查询并返回一个新查询。我是SQLAlchemy的新手,我查了类似的问题,但我仍然遇到同样的错误:
`Don't know how to literal-quote value datetime.datetime(2018, 1, 1, 8, 3, 1, 438278)`
这是我的代码:
def filter_dates(query_obj, datecols, start = None, end = None):
if end is None:
end = datetime.datetime.now()
if start is None:
start = end - datetime.timedelta(weeks=12)
print("%s to %s" % (start, end))
for datecol in datecols:
print("Filtrando datas!")
query_obj = query_obj.filter(datecol >= start)
query_obj = query_obj.filter(datecol <= end)
ReevTable.print_query(query_obj)
return query_obj
datecols是一个orm.attributes对象。假设我有一个名为User
的对象,其Datetime
属性名为created_at
。这是预期的行为:
query = session.query(Company.name, Company.created_at, Company.number_of_employees, Company.email_bounce_rate)
query = filter_dates(query_obj=query, datecols = [Company.created_at, Company.email_events.created_at])
query.all()
预期产出是仅在日期范围内创建公司的表格,跳出率应仅在指定日期范围内计算。这可能看起来很奇怪,但我不仅计算电子邮件,还计算其他类型的交互,因此我需要输入属性列表而不是单个属性列表。这就是我需要将此过滤与方法分开的原因。
我尝试过使用pandas datetime和timedelta,内置的python datetime模块,以及使用pd.to_datetime的简单字符串,但没有成功。每次都会引发同样的错误。我的公司专栏是在DateTime,所以我不知道还能做什么。
class Company(Base)
created_at = Column(DateTime, nullable=False)
我对SQLAlchemy完全陌生,我做错了什么?
完整追溯:
`Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "reev-data-science/tables/reevtable.py", line 128, in import_data
self.print_query(query_obj)
File "reev-data-science/tables/reevtable.py", line 107, in print_query
print(bcolors.OKBLUE + bcolors.BOLD + str(query_obj.statement.compile(compile_kwargs={"literal_binds":True})) + bcolors.ENDC)
File "<string>", line 1, in <lambda>
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/elements.py", line 442, in compile
return self._compiler(dialect, bind=bind, **kw)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/elements.py", line 448, in _compiler
return dialect.statement_compiler(dialect, self, **kw)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/compiler.py", line 453, in __init__
Compiled.__init__(self, dialect, statement, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/compiler.py", line 219, in __init__
self.string = self.process(self.statement, **compile_kwargs)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/compiler.py", line 245, in process
return obj._compiler_dispatch(self, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/annotation.py", line 80, in _compiler_dispatch
self, visitor, **kw)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/visitors.py", line 81, in _compiler_dispatch
return meth(self, **kw)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/compiler.py", line 1815, in visit_select
text, select, inner_columns, froms, byfrom, kwargs)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/compiler.py", line 1899, in _compose_select_body
t = select._whereclause._compiler_dispatch(self, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/visitors.py", line 81, in _compiler_dispatch
return meth(self, **kw)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/compiler.py", line 829, in visit_clauselist
for c in clauselist.clauses)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/compiler.py", line 826, in <genexpr>
s for s in
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/compiler.py", line 829, in <genexpr>
for c in clauselist.clauses)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/visitors.py", line 81, in _compiler_dispatch
return meth(self, **kw)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/compiler.py", line 829, in visit_clauselist
for c in clauselist.clauses)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/compiler.py", line 826, in <genexpr>
s for s in
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/compiler.py", line 829, in <genexpr>
for c in clauselist.clauses)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/visitors.py", line 81, in _compiler_dispatch
return meth(self, **kw)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/compiler.py", line 1080, in visit_binary
return self._generate_generic_binary(binary, opstring, **kw)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/compiler.py", line 1113, in _generate_generic_binary
self, eager_grouping=eager_grouping, **kw)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/visitors.py", line 81, in _compiler_dispatch
return meth(self, **kw)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/compiler.py", line 1244, in visit_bindparam
bindparam, within_columns_clause=True, **kwargs)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/compiler.py", line 1277, in render_literal_bindparam
return self.render_literal_value(value, bindparam.type)
File "/usr/local/lib/python3.5/dist-packages/sqlalchemy/sql/compiler.py", line 1295, in render_literal_value
"Don't know how to literal-quote value %r" % value)
NotImplementedError: Don't know how to literal-quote value datetime.datetime(2018, 1, 1, 9, 24, 46, 54634)`
print_query()方法:
`def print_query(query_obj):
print(bcolors.OKBLUE + bcolors.BOLD + str(query_obj.statement.compile(compile_kwargs={"literal_binds":True})) + bcolors.ENDC)`
答案 0 :(得分:0)
在过滤器子句中将 datatime
对象转换为 str
def filter_dates(query_obj, datecols, start = None, end = None):
if end is None:
end = datetime.datetime.now()
if start is None:
start = end - datetime.timedelta(weeks=12)
print("%s to %s" % (start, end))
for datecol in datecols:
print("Filtrando datas!")
query_obj = query_obj.filter(datecol >= str(start))
query_obj = query_obj.filter(datecol <= str(end))
ReevTable.print_query(query_obj)
return query_obj