ODBC TUTORIAL

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

Updated: 21 Feb 2022

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 on a Linux machine.

Pre-requisites

Have GO installed on your machine.

Download and Install DataDirect Oracle ODBC driver

  1. Go to our website and download the 64-bit ODBC driver for the Oracle database for Linux.
  2. Extract the contents of the package by running this command

    tar -xvzf PROGRESS_DATADIRECT_ODBC_ORACLE_LINUX_64.tgz


  3. Now run the below command to install the Progress DataDirect ODBC driver

     

    ./PROGRESS_DATADIRECT_ODBC_8.0_LINUX_64_INSTALL.bin -i console


  4. Go through the installation and stick to default options. You should be done with it few minutes.
  5. After the installation has completed, edit .bashrc and add the following lines to add necessary environment variables

     

    LD_LIBRARY_PATH=/opt/Progress/DataDirect/ODBC_80_64bit/lib:/opt/Progress/DataDirect/ODBC_80_64bit/jre/lib/amd64/server
     
    export LD_LIBRARY_PATH
     
    ODBCINI=/opt/Progress/DataDirect/ODBC_80_64bit/odbc.ini
     
    export ODBCINI
     
    ODBCINST=/opt/Progress/DataDirect/ODBC_80_64bit/odbcinst.ini
     
    export ODBCINST

    Note: If you changed the install path during the installation, please provide the corresponding path for above configuration files.

  6. Save the .bashrc and exit. Now run the below command to execute .bashrc to add the environment variables for all your sessions.

     

    source .bashrc
  7. Verify if all the three environment variables are present by running the below command
env



Check Environment variables

Configure ODBC DSN for Oracle Connection

  1. Go to the path /opt/Progress/DataDirect/ODBC_80_64bit/ and edit odbc.ini as shown below.


    [ODBC]
    IANAAppCodePage=4
    InstallDir=/opt/Progress/DataDirect/ODBC_80_64bit
    Trace=0
    TraceFile=odbctrace.out
    TraceDll=/opt/Progress/DataDirect/ODBC_80_64bit/lib/ddtrc28.so
     
    [Oracle]
    Description=DataDirect 8.0 Oracle Wire Protocol
    Driver=/opt/Progress/DataDirect/ODBC_80_64bit/lib/ddora28.so
    AccountingInfo=
    Action=
    AlternateServers=
    ApplicationName=
    ApplicationUsingThreads=1
    ArraySize=60000
    AuthenticationMethod=1
    CachedCursorLimit=32
    CachedDescriptionLimit=0
    CatalogIncludesSynonyms=1
    CatalogOptions=0
    ClientHostName=
    ClientID=
    ClientUser=
    ConnectionRetryCount=0
    ConnectionRetryDelay=3
    CryptoProtocolVersion=TLSv1.2,TLSv1.1,TLSv1
    CryptoLibName=
    DataIntegrityLevel=1
    DataIntegrityTypes=SHA1,MD5
    DefaultLongDataBuffLen=1024
    DescribeAtPrepare=0
    EditionName=
    EnableScrollableCursors=1
    EnableServerResultCache=0
    EnableDescribeParam=0
    EnableStaticCursorsForLongData=0
    EncryptionLevel=1
    EncryptionMethod=0
    EncryptionTypes=
    FailoverGranularity=0
    FailoverMode=0
    FailoverPreconnect=0
    FetchTSWTZasTimestamp=0
    GSSClient=native
    HostName=<Hostname>
    HostNameInCertificate=
    IANAAppCodePage=4
    InitializationString=
    KeyPassword=
    Keystore=
    KeystorePassword=
    LoadBalancing=
    LobPrefetchSize=4000
    LocalTimezoneOffset=
    LockTimeout=-1
    LogonID=
    LoginTimeout=15
    Module=
    PortNumber=1521
    PRNGSeedFile=/dev/random
    PRNGSeedSource=0
    ProcedureRetResults=0
    ProgramID=
    QueryTimeout=0
    ReportCodepageConversionErrors=0
    ReportRecycleBin=0
    ServerName=
    ServerType=0
    ServiceName=
    SID=xe
    SSLLibName=
    SupportBinaryXML=0
    KeepAlive=0
    TimestampEscapeMapping=0
    TNSNamesFile=
    Truststore=
    TruststorePassword=
    UseCurrentSchema=1
    ValidateServerCertificate=1
    WireProtocolMode=2


  2. In the above configuration, the 4 parameters you would need to modify are the DSN Name, HostName, Port Number and SID/Service Name.
  3. Save the file after you have configured your connection and exit.

Connect to Oracle 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