ODBC TUTORIAL

Connect to Oracle, Salesforce and others using ODBC from Golang on Windows

Updated: 26 Feb 2021

Introduction

If you are looking to connect to your databases like SQL Server, Oracle, DB2, Postgres, MongoDB etc., or SaaS apps like Salesforce, Eloqua, Oracle Sales Cloud, Oracle Service Cloud etc., using a ODBC driver from your Go application, this is the place to get started. In this tutorial, we will walk you through on how you can connect to Oracle database from Go using Progress DataDirect ODBC drivers.

Download and Install DataDirect Oracle ODBC driver

  1. Go to our website and download the 64-bit ODBC driver for the Oracle database for Windows.
  2. Extract the package and install the ODBC driver by running the installer.
  3. Now Open ODBC Administrator (64-bit) and Click on Add, to add a new datasource.


    Add new ODBC DSN

  4. Choose “DataDirect 8.0 Oracle Wire Protocol” as your driver

    Choose DataDirect Oracle driver

  5. Now fill in the connection information such as hostname, port, SID/Service Name and click on Test Connect to verify connection details.

Pre-requisites

Have GO installed on your machine.

Connecting to ODBC from Go

  1. Get ODBC package for Go by running the following command

     

    go get github.com/alexbrainman/odbc


  2. Following is a sample application on how you can connect to Oracle using Progress DataDirect ODBC driver and query a table. This is just a starter code, you can modify this as per your needs.
    
package main
 
import (
    "database/sql"
    "fmt"
 
    _ "github.com/alexbrainman/odbc"
)
 
func main() {
 
    var (
        regionID   int
        regionName string
    )
 
    //Open Connection. Provide DSN, Username and Password
    db, err := sql.Open("odbc", "DSN=Oracle;Uid=saikrishnabobba;Pwd=<progress>")
    if err != nil {
        fmt.Println("Connection Failed :( ", err)
    } else {
        fmt.Println("Can Connect :)")
    }
 
    //Provide the Query to execute
    rows, err := db.Query("select REGION_ID, REGION_NAME from REGIONS")
 
    if err != nil {
        fmt.Println("Unable to Query :( ", err)
    }
 
    //Parse the Resultset
    defer rows.Close()
    for rows.Next() {
        err := rows.Scan(®ionID, ®ionName)
        if err != nil {
            fmt.Println("Error when parsing :( ", err)
        }
 
        fmt.Println(regionID, regionName)
    }
 
    err = rows.Err()
    if err != nil {
        fmt.Println(err)
    }
 
    //Close the connection
    defer db.Close()
}

You can use similar steps with any of DataDirect ODBC suite of drivers available for Relational databases like SQL Server, MySQL, PostgreSQL etc., Big Data sources like Hive, Spark etc., SaaS sources like Salesforce, Eloqua, Oracle Sales Cloud etc., and NoSQL Data sources like MongoDB. Feel free to try any of our drivers with your Go apps for connecting to your datasource of your choice.

Connect any application to any data source anywhere

Explore all DataDirect Connectors

Need additional help with your product?

Get Customer Support