我有一个sql命令,只想在某些条件下选择一些记录(使用python & db is Postres):所以,我的查询是:
current_date= datetime.now()
tt = yield self.db.execute(self.db.execute('SELECT "Id", "RubricId", "IsRubric"
FROM whis2011."CoockieUserInterests"
'WHERE "UserId" = %s AND "Date" = %s '
% (Id, current_date))
result=tt.fetchall()[0]问题:当我想将日期时间传递给输入“日期”时,我得到了错误:
syntax error at or near "00"
LINE 1: ...rests" WHERE "UserId" = 1 AND "Date" = 2016-10-05 00:22:07.3...
^db中的所有“日期”字段都类似于: 2016-09-25 00:00:00
此外,数据库中字段"Date“的数据类型是”没有时区的时间戳“。
这是我的游泳池
application.db = momoko.Pool(
dsn='dbname=xxxx user=xxxxx password=xxxxx host=xxxx port=5432',
size=1,
ioloop=ioloop,
)我如何选择这样的格式在我的数据库中的“日期”?
发布于 2016-10-04 21:54:57
您没有说明用于连接postgresql的模块。让我们暂时假设它是psycopg2。
在这种情况下,可以使用以下方法将参数传递给查询:
current_date = datetime.now()
self.db.execute(
'SELECT Id, RubricId, IsRubric '
'FROM whis2011.CoockieUserInterests '
'WHERE UserId = %s AND Date = %s',
(Id, current_date))注意,我们是,而不是,这里使用的是%内插运算符。相反,我们使用%s标记sql参数,然后将它们分别传递给cursor.execute。
如果您使用不同的包连接到Postgres,那么它可能以不同的方式标记参数。有关详细信息,请查看文档或模块paramstyle。
https://stackoverflow.com/questions/39862199
复制相似问题