谷歌应用引擎数据存储工作奇怪

时间:2012-07-10 05:53:31

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

我试图在检查手机号码是否存在后将数据存储在谷歌应用引擎数据存储中,但代码总是显示“失败”,即使手机号码是否在数据库中。它显示故障但仍将数据存储在数据存储中 请查看此处的代码,请让我知道我在做什么错误

String name= request.getParameter("name");
String email=request.getParameter("email");
String mobileno= request.getParameter("mobileno");
String password= request.getParameter("passwd");
String status=""; 
//
Query query = new Query("dailyquizuser");
    query.addFilter("mobileno", Query.FilterOperator.EQUAL, mobileno);
     DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    PreparedQuery pq = datastore.prepare(query);
    Entity dailyquizuserentity = pq.asSingleEntity();

    out.println("dailyquizuserentity: " + dailyquizuserentity);
    if(dailyquizuserentity != null){
        status="failure";
        out.println(status);


    } else {
        Key dailyquizuserKey = KeyFactory.createKey("dailyquizuser", "dailyquiz");
        Entity  dailyquizuser = new Entity("dailyquizuser", dailyquizuserKey);
        dailyquizuser.setProperty("name",  name);
        dailyquizuser.setProperty("email", email);
        dailyquizuser.setProperty("mobileno", mobileno);
        dailyquizuser.setProperty("password", password);

       DatastoreService datastore1 = DatastoreServiceFactory.getDatastoreService();
        datastore1.put(dailyquizuser);
        out.println("dailyquizuser: " + dailyquizuser);
        status="success";
        out.println(status);

    }


    %> 

1 个答案:

答案 0 :(得分:0)

一些注意事项:

  1. 您应该检查mobileno变量不是null(与所有其他必需参数相同)。这可以解释为什么始终找到实体 - null是有效的属性值。使用null过滤器查询将找到具有null属性的实体,但查询没有属性的实体将无法找到它。

  2. 您展示的代码会创建具有单个父级的实体:

    // a key to a parent
    Key dailyquizuserKey = KeyFactory.createKey("dailyquizuser", "dailyquiz"); 
    
    // new entity with a parent
    Entity  dailyquizuser = new Entity("dailyquizuser", dailyquizuserKey); 
    

    因此,在搜索“dailyquizuser”时,您应该考虑使用祖先查询。

  3. addQuery(..)已弃用。请改用setQuery()