0

看跌期权:

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    try{
        Entity e =  new Entity("Winners", "NFL" ); 
        String winner_game = new String("winner_" + input_game );
        e.setProperty(winner_game, input_win_value );
        datastore.put(e);

……

获取:

    DatastoreService datastore = DatastoreServiceFactory.getDatastoreService();
    try{
        Entity e = datastore.get(KeyFactory.createKey("Winners",  "NFL" )); 
        String winner_game = new String("winner_" + i );
        if( e.getProperty(winner_game) != null )
        {
            Integer property = new Integer(e.getProperty( winner_game ).toString());
            return property;
        }
        else{
            return 0;
        }

...

e.getProperty 正在返回 null ......有什么想法吗?

4

1 回答 1

1

不要创建新的字符串/整数对象。如果您需要在属性名称上附加一些内容,请直接执行。

Entity e = new Entity("Winners", "NFL" ); 
String winner_game = "winner_" + input_game; // Where input_game is "X"
e.setProperty(winner_game, input_win_value); // int input_win_value = 10;
datastore.put(e);

相同的检索:

Entity e = datastore.get(KeyFactory.createKey("Winners",  "NFL" )); 
String winner_game = "winner_" + i; // where i is also "X"
if( e.getProperty(winner_game) != null ) {
   // Casting is ugly but does the trick
   Integer property = (Integer) e.getProperty(winner_game);
   return property;
} else{
   return 0;
}
于 2013-10-16T16:41:59.500 回答