R
R
re-incarnation2021-06-26 11:41:56
go
re-incarnation, 2021-06-26 11:41:56

Why do I have after Update, Select returns the old data?

code send message
//session /ответ
lp.MessageNew(func(_ context.Context, obj events.MessageNewObject) {
      msg := obj.Message.Text
      //prefix
      startWith := "/ответ"
      //check prefix
      starts := strings.HasPrefix(msg, startWith)
      if starts == true {
        //trim message
        res_trim := strings.Trim(msg, "/ответ ")
        //check empty text
        if res_trim != "" {
          //search note in table where asker -> answer = ask_id
          res_search_active_ask, err100 := db.Query(fmt.Sprintf("SELECT `asker`, `answer`, `closed` FROM `ucp` WHERE `closed` = '0' AND (`asker` = '%d' OR `answer` = '%d')", msg_from_id))
          if err100 != nil {
            panic(err100)
          }
          for res_search_active_ask.Next(){
            err := res_search_active_ask.Scan(&ucp.Asker, &ucp.Answer, &ucp.Closed)
            if err != nil {
              panic(err)
            }
          }
          if ucp.Closed == 0 {
          if msg_from_id == ucp.Answer {
            session_message_redirect, err11 := vk.MessagesSend(api.Params{
              "peer_id": ucp.Asker,
              "random_id": 0,
              "message": assistant + res_trim,
            })
            //err check
            if err11 != nil {
              panic(err11)
            }
                spew.Dump(session_message_redirect)
          }else{
            session_message_redirect2, err112 := vk.MessagesSend(api.Params{
              "peer_id": msg_from_id,
              "random_id": 0,
              "message": error_assistant_havent_ask,
            })
            //err check
            if err112 != nil {
              panic(err112)
            }
                spew.Dump(session_message_redirect2)
          }
        }else{
            session_message_redirect3, err1123 := vk.MessagesSend(api.Params{
              "peer_id": msg_from_id,
              "random_id": 0,
              "message": error_user_havent_ask,
            })
            //err check
            if err1123 != nil {
              panic(err1123)
            }
                spew.Dump(session_message_redirect3)
        }
}
}
})


code close session
//close ask
  lp.MessageNew(func(_ context.Context, obj events.MessageNewObject) {
      msg := obj.Message.Text
      //prefix
      startWith := "/close"
      //cheack prefix
      starts := strings.HasPrefix(msg, startWith)
      if starts == true {
        //trim message
        res_trim := strings.Trim(msg, "/close ")
        //cheack empty text
        if res_trim != "" {
          res_answer_active_status, err35 := db.Query(fmt.Sprintf("SELECT `answer`, `asker`, `closed` FROM `ucp` WHERE `closed` = '0' AND (`answer` = '%d' OR `asker` = '%d')", msg_from_id))
          if err35 != nil {
            panic(err35)
          }
          for res_answer_active_status.Next() {
            err36 := res_answer_active_status.Scan(&ucp.Answer, &ucp.Asker, &ucp.Closed)
            if err36 != nil {
              panic(err36)
            }
          }
          if ucp.Closed == 0 {
          if msg_from_id == ucp.Answer || msg_from_id == ucp.Asker {
            //revers string to int for req
           res_trim_int, err34 := strconv.Atoi(res_trim)
           if err34 != nil {
             panic(err34)
           }
                    // update answer id
          update_answer_id_start, err33 := db.Query(fmt.Sprintf("UPDATE `ucp` SET `closed` = '1' WHERE `id` = '%d'", res_trim_int))
          if err33 != nil {
            panic(err33)
          }
          defer update_answer_id_start.Close()
          //send message
          send, err := vk.MessagesSend(api.Params{
            "peer_id": msg_from_id,
            "random_id": 0,
            "message": closed_ask + res_trim,
          })

          if err != nil {
            panic(err)
          }
          spew.Dump(send)
          spew.Dump(update_answer_id_start)
          }else{
            send, err := vk.MessagesSend(api.Params{
              "peer_id": msg_from_id,
              "random_id": 0,
              "message": "ERROR: You havenot active ask.",
            })

            if err != nil {
              panic(err)
            }
            spew.Dump(send)
          }
        }else{
          session_message_redirect3, err1123 := vk.MessagesSend(api.Params{
            "peer_id": msg_from_id,
            "random_id": 0,
            "message": error_user_havent_ask,
          })
          //err check
          if err1123 != nil {
            panic(err1123)
          }
              spew.Dump(session_message_redirect3)
        }
        }
      }
  })


The essence of my code is that a question comes in private messages to the community, after which it is redirected to the VK conversation, and in the conversation, support operators take it and already communicate, in other words, a chat bot, but when the "session" is closed with the /close ID command, the data is entered into the database , everything is fine, but the "session" is not updated, and returns that this question is open, but there is a record in the database that it was closed. Everything returns to normal when I stop the bot, and then restart it, and so on in a circle.

Answer the question

In order to leave comments, you need to log in

1 answer(s)
E
Evgeny Mamonov, 2021-06-26
@re-incarnation

The problem is in this section, Query is used to retrieve data, you need to use Exec to change

update_answer_id_start, err33 := db.Query(fmt.Sprintf("UPDATE `ucp` SET `closed` = '1' WHERE `id` = '%d'", res_trim_int))
defer update_answer_id_start.Close()

For example like this
update_answer_id_start, err33 := db.Exec("UPDATE `ucp` SET `closed` = '1' WHERE `id` = ?", res_trim_int)

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question