【问题标题】:query.filter and gqlquery.filter 和 gql
【发布时间】:2012-05-21 08:55:07
【问题描述】:

我正在尝试这个功能,但是出了点问题。我存储了名称“John”,但是当我将 john 传递给 username_ 并调用 check_user 时,输出始终为 return results,即使该名称未存储在谷歌数据存储中。为什么?

def check_user(self, username_):
    query = db.Query(Registrations)
    results = query.filter('username =', username_)
    if results:
        return results

user_username = self.request.get('username')
check_username_valid = self.check_user(user_username)

 if not check_username_valid:
    error_username_exists="Username already exists"

对于 GQL,我如何将变量 username_ 传递到查询中?喜欢:

 qr = db.GqlQuery("Select * from Registrations Where username = ?????")

【问题讨论】:

    标签: python google-app-engine google-cloud-datastore gql


    【解决方案1】:

    results是查询对象而不是结果,你需要调用query.filter('username =', username_).get()返回单个结果或query.filter('username =', username_).fetch()返回所有结果。

    【讨论】:

    • 谢谢,只是为了学习,如何将变量传递给查询?这可能会导致某种类型的注入?
    • 我认为它只是一个简单的字符串构建,在 ndb 中它变得更有趣。
    • 不!不要使用字符串连接插入参数!使用 GQL 文档中记录的参数替换(“:1”、“:2”等)。
    • @loops 我不明白你的问题,我以为你问过你的变量是如何在内部代码中传递给查询的。不要使用字符串构建来构建 GQL,因为它对注入很敏感,就像 SQL 一样。
    猜你喜欢
    • 1970-01-01
    • 2011-06-07
    • 1970-01-01
    • 2016-06-11
    • 1970-01-01
    • 2014-10-06
    • 2011-04-08
    • 2021-11-13
    • 2010-12-20
    相关资源
    最近更新 更多