B
B
Baters1252018-12-10 17:55:20
Oracle
Baters125, 2018-12-10 17:55:20

How to connect Oracle Database to Go?

Hello!
I will say right away that I’m just a beginner, a beginner, I want to learn how to work with a relational database in Go, the choice fell on Oracle Database.
Can you please tell me how to properly connect Oracle Database to Go?
The example below throws the error "panic: sql: unknown driver "Oracle" (forgotten import?)" (didn't he pull them up himself?)
I'll be glad to hear your answers :)

package main

import (
  "database/sql"
  "fmt"
  "log"
)
func main() {
  fmt.Println(sql.Drivers())
  db, err := sql.Open("Oracle", "login:[email protected](ip:port)")
  if err != nil {
    panic(err)
  }
  value, err := db.Query("select table_name from tabs where ... ", 1)

  var table_name string

  if err != nil {
    log.Fatal(err)
  }
defer value.Close()
  for value.Next() {
    err := value.Scan(&table_name)
    if err != nil {
      log.Fatal(err)
    }
    log.Println(table_name)
  }
  err = value.Err()
  if err != nil {
    log.Fatal(err)
  }
}

Answer the question

In order to leave comments, you need to log in

2 answer(s)
E
evgensoft, 2018-12-11
@Baters125

It is necessary to add a specific driver connection to the import block, for example -
Opening a connection like this -
db, err := sql.Open("goracle", "user/[email protected](DESCRIPTION=(ADDRESS_LIST= (ADDRESS=(PROTOCOL=tcp)(HOST=hostname)(PORT=port)))(CONNECT_DATA=(SERVICE_NAME=sn)))")

P
Pavel Shvedov, 2018-12-10
@mmmaaak

you need to manually connect the package with the appropriate driver through '_'

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question