如何在sqlalchemy python3中与dateTime进行原始查询
因此,我试图根据DateTime字段Timestamp
从表消息
查询,但我一直遇到错误。也许我试图查询的方式是不正确的。
def get_test(self, start_date, end_date):
query = 'SELECT * from messages WHERE messages."timeStamp" > {start_date} and messages."timeStamp" < {end_date};'
res = self.session.execute(query)
self.session.commit()
return res.fetchall()
运行此代码时,我会收到以下错误。
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.SyntaxError) syntax error at or near "00"
LINE 1: ... messages WHERE messages."timeStamp" > 2021-06-01 00:00:00 a...
我还尝试使用dateTime.strptime
方法,但这也无法使用。
def get_test(self, start_date, end_date):
query = f'''SELECT * from messages WHERE messages."timeStamp" > {datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S")}
and messages."timeStamp" < {datetime.strptime(end_date, "%Y-%m-%d %H:%M:%S")};'''
res = self.session.execute(query)
self.session.commit()
return res.fetchall()
我还尝试使用查询参数,但这也没有用。
def get_test(self, start_date, end_date):
query = f'SELECT * from messages WHERE messages."timeStamp" > %s and messages."timeStamp" < %s;'
res = self.session.execute(query, (datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S"),
datetime.strptime(end_date, "%Y-%m-%d %H:%M:%S")))
self.session.commit()
return res.fetchall()
如果有人能在这里帮助我,真的很感激。
So I am trying to query from a table messages
on the basis of a datetime field timeStamp
but I keep getting an error. Maybe the way I am trying to query is not correct.
def get_test(self, start_date, end_date):
query = 'SELECT * from messages WHERE messages."timeStamp" > {start_date} and messages."timeStamp" < {end_date};'
res = self.session.execute(query)
self.session.commit()
return res.fetchall()
When I run this code, I get the following error.
sqlalchemy.exc.ProgrammingError: (psycopg2.errors.SyntaxError) syntax error at or near "00"
LINE 1: ... messages WHERE messages."timeStamp" > 2021-06-01 00:00:00 a...
I also tried using datetime.strptime
method but that didn't work too.
def get_test(self, start_date, end_date):
query = f'''SELECT * from messages WHERE messages."timeStamp" > {datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S")}
and messages."timeStamp" < {datetime.strptime(end_date, "%Y-%m-%d %H:%M:%S")};'''
res = self.session.execute(query)
self.session.commit()
return res.fetchall()
I also tried using the query parameters but that didn't work either.
def get_test(self, start_date, end_date):
query = f'SELECT * from messages WHERE messages."timeStamp" > %s and messages."timeStamp" < %s;'
res = self.session.execute(query, (datetime.strptime(start_date, "%Y-%m-%d %H:%M:%S"),
datetime.strptime(end_date, "%Y-%m-%d %H:%M:%S")))
self.session.commit()
return res.fetchall()
Would really appreciate if anyone could help me out here.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(1)
我认为这里有几件事。
我不认为您的F线串正在填充,而Strptime是将弦变成日期,而不是相反。
我会这样写:
I think there are a couple of things going on here.
I don't think your f-strings are populating and strptime is for turning strings into dates, not the other way round.
I'd write it like this: