V
V
varstring2017-09-23 08:19:50
go
varstring, 2017-09-23 08:19:50

Error while transferring data to the database, what to do?

In general, I'm trying to transfer data to write them to the database, as I get an error, Gogland IDE says that something is wrong with - go handlePacket(connectFD) it is called immediately after socket.Accept() in a new thread. Tell me what might not be so . oh yes the program crashes in the db.QueryRow(sqlStatement,email,password).Scan(&id) method

func handlePacket(conn net.Conn) {
  rw := bufio.NewReadWriter (bufio.NewReader (conn), bufio.NewWriter (conn))
  defer conn.Close()

  packet := model.RegistrationMessage{}
  client := JsonDecoderMessage(rw).Decode(&packet)
  if client != nil {
    puts("Error from Decode.Please NO :(")
  }

  if 	packet.MessageType 		== model.AUTH_MESSAGE {
    puts("Auth")
  }else if packet.MessageType == model.REGS_MESSAGE {
    puts("Regs")
    Registration(packet.Login,packet.Password)
    puts("good")
  }
}

and here I establish a connection to the database and try to send data to the database
var db *sql.DB
func InitDataBase(){
  psqlInfo := fmt.Sprintf("host=%s port=%d user=%s "+
    "password=%s dbname=%s sslmode=disable",
    host, port, user, password, dbname)
  db, err := sql.Open("postgres", psqlInfo)
  if err != nil {
    panic(err)
  }
  defer db.Close()

  err = db.Ping()
  if err != nil {
    panic(err)
  }
  fmt.Println("Successfully connected!")
}
func Registration( email, password string)  {
  sqlStatement := `INSERT INTO account0( email,password)
          VALUES ($1, $2)
          RETURNING id`
  id:=0
  err := db.QueryRow(sqlStatement,email,password).Scan(&id)
  if err != nil {
    panic(err)
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
V
varstring, 2017-09-27
@varstring

I found the answer)) the error turned out to be in the assignment, instead it should have been
db, err = sql.Open("postgres", psqlInfo)

A
Alexander Pavlyuk, 2017-09-23
@pav5000

When the InitDataBase() function ends, you close the connection to the database, so you cannot get anything from it later.
defer db.Close() executes right after fmt.Println("Successfully connected!")

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question