duan0714 2013-06-27 03:47
浏览 50
已采纳

使用Go更新实体Appengine数据存储

I'm trying to find an effective example in how to perform updates on appengine datastore with Go. All the examples I've found on the web are very vague and mostly explains concepts and not the "real life". The appengine documentation for go says:

..."Updating an existing entity is a matter of performing another Put() using the same key."

My problem here is being in how to retrieve the key. So I have the code below to store and retrieve data:

func subscribe(w http.ResponseWriter, r *http.Request) {

    user := User {
        Name: r.FormValue("username"),
        Email: r.FormValue("useremail"),
        Flag: 0,
    }

    c := appengine.NewContext(r)
    //datastore.Put(c, datastore.NewIncompleteKey(c, "User", nil), &user)
    datastore.Put(c, datastore.NewKey(c, "User", "stringID", 0, nil), &user)

    template.Must(template.ParseFiles("confirmation.html")).Execute(w, nil)

}

func checkusers(w http.ResponseWriter, r *http.Request) {

    c := appengine.NewContext(r)

    qUsers := datastore.NewQuery("User")

    var users []User

    qUsers.GetAll(c, &users)

    template.Must(template.ParseFiles("users.html")).Execute(w, users)
}

How do I do an update on the flag property changing its value tom 1?

I'm a bit confused on this thing as I couldn't fully understand how the "key" is stored for each entity.

Any help would be very appreciated.

  • 写回答

1条回答 默认 最新

  • douren7179 2013-06-27 06:32
    关注

    todo an update you first need to identify if your object is a new or an old one. this can be simple done by adding the following method to your User struct:

    type User struct {
        Name string
        Email string
        Flag int64  `datastore:"-"`
    }
    func (u *User) IsNew() bool {
        return u.Flag == 0
    }
    

    this tells data-store to ignore the Flag field when storing and retrieving an object and because initial value of int64 is zero, a newly created object can be identified if Flag is zero

    so creating a new object just needs to set UserName and Email:

    user := User {
        Name: r.FormValue("username"),
        Email: r.FormValue("useremail")
    }
    

    then next step is to either use a IncompleteKey or a Key, for the put statement

    could look like this:

    var k *datastore.Key
    if user.IsNew() {
        k = datastore.NewIncompleteKey(c, "Users", nil)
    } else {
        k = datastore.NewKey(c, "Users", "", user.Flag, nil)
    }
    k, err := datastore.Put(c, k, user)
    if err != nil {
        return k, err
    }
    

    with an incomplete key, app-engine will generate a new key for you.
    after put you can assign the new key to your object:

    user.Flag = k.IntID
    

    now if you do a query later you need to assign the Id to your query result objects, the query will return the keys of query result in the same order so you can change your code like this:

    keys, err := q.GetAll(c, &users)
    if err != nil {
        return
    }
    l := len(users)
    for i := 0; i < l; i++ {
        users[i].Flag = keys[i].IntID()
    }
    

    thats all, for more information, just have a look a the reference document there is explained with methods return which values.
    https://developers.google.com/appengine/docs/go/datastore/reference

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 uniapp uview http 如何实现统一的请求异常信息提示?
  • ¥15 有了解d3和topogram.js库的吗?有偿请教
  • ¥100 任意维数的K均值聚类
  • ¥15 stamps做sbas-insar,时序沉降图怎么画
  • ¥15 买了个传感器,根据商家发的代码和步骤使用但是代码报错了不会改,有没有人可以看看
  • ¥15 关于#Java#的问题,如何解决?
  • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
  • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
  • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
  • ¥500 火焰左右视图、视差(基于双目相机)