Google app engine 使用时间戳检索数据的GQL查询(python)

Google app engine 使用时间戳检索数据的GQL查询(python),google-app-engine,python-2.7,google-cloud-datastore,Google App Engine,Python 2.7,Google Cloud Datastore,我在googledatastore中有一个表,它在n列中保存了n个值,其中一个是时间戳 timestamp属性在表类(Java)中定义如下: 表格如下: id | value | timestamp ---------------------------------------------------------- 1 | ABC | 2014-02-02 21:07:40.822000

我在googledatastore中有一个表,它在n列中保存了n个值,其中一个是
时间戳

timestamp
属性在表类(Java)中定义如下:

表格如下:

    id    |    value    |            timestamp    
----------------------------------------------------------
     1    |    ABC      |    2014-02-02 21:07:40.822000   
     2    |    CDE      |    2014-02-02 22:07:40.000000   
     3    |    EFG      |       
     4    |    GHI      |    2014-02-02 21:07:40.822000   
     5    |    IJK      |       
     6    |    KLM      |    2014-01-02 21:07:40.822000   
timestamp
列后来添加到表中,因此有些行没有相应的
timestamp

我正在尝试,使用Python Google App Engine构建一个api,将具有时间戳
=
的行总数返回到某个值

例如:

-- This is just an example
SELECT * FROM myTable WHERE timestamp >= '2014-02-02 21:07:40.822000'
我用python编写了这个类:

import sys
...
import webapp2

from google.appengine.ext import db

class myTable(db.Model):
    value = db.StringProperty()
    timestamp = datetime.datetime

class countHandler(webapp2.RequestHandler):
    def get(self, tablename, timestamp):

        table = db.GqlQuery("SELECT __key__ FROM " + tablename + " WHERE timestamp >= :1",  timestamp )
        recordsCount = 0

        for p in table:
            recordsCount += 1

         self.response.out.write("Records count for table " + tablename + ": " + str(recordsCount))

app = webapp2.WSGIApplication([
    ('/count/(.*)/(.*)', countHandler)
], debug=True)
我已经成功地部署了它,我能够调用它,但由于某些原因,我不明白它总是说什么

Records count for table myTable: 0
我正在努力处理时间戳的数据类型。。我认为问题就在那里。。有什么想法吗?应该声明哪种类型

谢谢大家!

您的问题(也在注释中讨论过)似乎是您正在(可能)向GqlQuery参数传递一个字符串

要按
datetime
筛选查询,需要将
datetime
对象传递到查询参数中。为此,我们来看看如何转换它

小例子:

# not sure how your timestamps are formatted but supposing they are strings
# of eg 2014-02-02 21:07:40.822000
timestamp = datetime.datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S.%f" )

table = db.GqlQuery("SELECT __key__ FROM " + tablename + " WHERE timestamp >= :1",  timestamp)

您确定时间戳不是字符串吗?据我所知,它必须是datetime。@JimmyKane我在Java类中将它定义为
Date
,所以它应该是Date。。在python方面,我将尝试使用
timestamp=db.StringProperty()
,如果这是您的意思,我认为您需要使用datetime格式的时间戳。只需将其转换为datetime并将该对象用于查询。告知它是否有效。我有一些时间在我的查询和atm中使用它,我没有方便的测试。@JimmyKane很抱歉很无聊,但你是说python还是java端?(在python中可能有一些
datetime
不同于
datetime.datetime
?)@JimmyKane您的提示解决了我的问题,谢谢。如果你回答原来的问题,我会接受你的回答!再次感谢!实际上,我使用了另一个示例:
tmstmp=dateutil.parser.parse(timestamp)
。请注意,通过这种方式,我需要下载并导入dateutil lib。@BeNdErR确信这也会起作用。我对datetime模块比较熟悉。很高兴它解决了这个问题。也许你的解决方案可以帮我节省导入新库的时间:)
# not sure how your timestamps are formatted but supposing they are strings
# of eg 2014-02-02 21:07:40.822000
timestamp = datetime.datetime.strptime(timestamp, "%Y-%m-%d %H:%M:%S.%f" )

table = db.GqlQuery("SELECT __key__ FROM " + tablename + " WHERE timestamp >= :1",  timestamp)