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 用stata实现聚类的代码
  • ¥15 请问paddlehub能支持移动端开发吗?在Android studio上该如何部署?
  • ¥170 如图所示配置eNSP
  • ¥20 docker里部署springboot项目,访问不到扬声器
  • ¥15 netty整合springboot之后自动重连失效
  • ¥15 悬赏!微信开发者工具报错,求帮改
  • ¥20 wireshark抓不到vlan
  • ¥20 关于#stm32#的问题:需要指导自动酸碱滴定仪的原理图程序代码及仿真
  • ¥20 设计一款异域新娘的视频相亲软件需要哪些技术支持
  • ¥15 stata安慰剂检验作图但是真实值不出现在图上