doudang4568 2018-08-16 21:14
浏览 57
已采纳

Golang和Postgres互动-从int64表中减去1每个查询

I'm learning to create HTTP API's,

I'm creating a dummy API credit system, as that's what I'm interested in specifically.

This is what i came up with: (I'm using PQ Driver)

if !dummy.creds <= 0 {
    c.JSON(404, gin.H{
        "success": false,
        "message": "No Credits!",
    })
    return
}

However, let's say i edit the SQL table to say 50 Credits, How do subtract 1 credit everytime the dummy API is queried?

So for example, if i Query the API and it returns the successful dummy data (in JSON) from the SQL table, how can i subtract one credit, so that i only have 49 credits, then 48, 47, 46, etc.

UPDATE: This is what i came up with

_, err := db.Exec("UPDATE dummy SET creds = -1 WHERE email = $1")
if err != nil {
    log.Fatal(err)

}

Instead of working, it fails to do anything.

What am i doing wrong?

Any answers & knowledge into this would be amazing!

Thanks!

  • 写回答

1条回答 默认 最新

  • duanjuan1103 2018-08-19 11:02
    关注

    As posted by @seva

    I believe the question is about returning consistent results. It's not about go then. It's about database. Search "optimistic locking" vs "pessimistic locking", "select for update" etc. Here is also a basic (but good) introduction to working with databases in go (if you need it): go-database-sql.org/index.html

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

报告相同问题?

悬赏问题

  • ¥15 高价求中通快递查询接口
  • ¥15 解决一个加好友限制问题 或者有好的方案
  • ¥15 关于#java#的问题,请各位专家解答!
  • ¥15 急matlab编程仿真二阶震荡系统
  • ¥20 TEC-9的数据通路实验
  • ¥15 ue5 .3之前好好的现在只要是激活关卡就会崩溃
  • ¥50 MATLAB实现圆柱体容器内球形颗粒堆积
  • ¥15 python如何将动态的多个子列表,拼接后进行集合的交集
  • ¥20 vitis-ai量化基于pytorch框架下的yolov5模型
  • ¥15 如何实现H5在QQ平台上的二次分享卡片效果?