我在 Google Datastore 中有一个表格,其中包含 n 列中的 n 值,其中一个是timestamp
timestamp
属性在表类(Java)中定义如下:
@Persistent
private Date timestamp;
表格如下:
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
我正在努力处理时间戳的数据类型..我认为问题就在那里......任何想法?应该声明哪种类型?
谢谢!
答案 0 :(得分:2)
你的问题(正如评论中所讨论的)似乎是你正在将一个字符串(可能)传递给GqlQuery参数。
为了按datetime
过滤您的查询,您需要将datetime
对象传递给查询参数。为此,请查看here如何转换它。
小例子:
# 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)