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.
Have GO installed on your machine.
tar -xvzf PROGRESS_DATADIRECT_ODBC_ORACLE_LINUX_64.tgz
./PROGRESS_DATADIRECT_ODBC_8.0_LINUX_64_INSTALL.bin -i console
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.
source .bashrc
env
[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
go get github.com/alexbrainman/odbc
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.