Linux is widely used in server environments due to its stability and security. Go, known for its simplicity and efficiency, has become one of the preferred languages for backend development. Connecting Go to a database can greatly enhance data processing efficiency and performance. This article provides a step-by-step guide on how to connect to the GBase 8s database via ODBC using Go in a Linux environment, from setting up the environment to implementing the code.
1. Environment Setup: Install Go
First, we need to install the Go programming language on the Linux system. Download the latest Go package:
tar -C /usr/local -xzf go1.21.1.linux-amd64.tar.gz
Add /usr/local/bin
to your PATH
environment variable so that you can use the go
command.
2. Environment Setup: Install go-odbc
You will need to install unixodbc
and gcc
beforehand. The go-odbc
package can be downloaded using git
or as an offline package. Download it from: go-odbc on GitHub
After downloading go-odbc-master.zip
, extract it and copy it to /usr/local/go/src
. It is recommended to rename the folder from go-odbc-master
to odbc
. Navigate to /usr/local/go/src/odbc
and execute make.bash
to complete the installation.
3. Configure GBase ODBC
On a Linux system, you need to install unixODBC
or iODBC
to use the GBase 8s ODBC driver. We recommend using the unixODBC
driver manager. The unixODBC
package is typically provided along with the GBase 8s ODBC installation. Alternatively, you can download it from the official unixODBC
website.
First, navigate to the system disk directory, for example:
/media/RHEL-6.6 Server.x86_64/Packages
Install the following packages:
-
unixODBC-2.2.14-1.x86_64.rpm
: The data source manager forunixODBC
-
unixODBC-devel-2.2.14-1.x86_64.rpm
: Development package forunixODBC
After successful installation, you can verify the unixODBC
installation with the following command:
ODBC Configuration Files
vi /etc/odbcinst.ini
#GBase
[GBase]
Description = ODBC for GBase
Driver = /opt/gbase/lib/cli/iclit09b.so
Setup = /opt/gbase/lib/cli/iclit09b.so
Driver64 = /opt/gbase/lib/cli/iclit09b.so
Setup64 = /opt/gbase/lib/cli/iclit09b.so
FileUsage = 1
vi /etc/odbc.ini
[test]
Driver =/opt/gbase/lib/cli/iclit09b.so
SERVER =gbaseserver2
UID =gbasedbt
PWD =gbasedbt
DATABASE =db_utf8
PORT =9488
CHARSET =UTF8
[ODBC]
;uncomment the below line for UNICODE connection
UNICODE=UCS-2
Now that the ODBC configuration is complete, you can use isql
to verify whether ODBC has been configured successfully.
Set Environment Variables
export LD_LIBRARY_PATH=${GBASEDBTDIR}/lib:${GBASEDBTDIR}/lib/esql:${GBASEDBTDIR}/lib/cli
Verify GBase ODBC Configuration
To verify the ODBC configuration, use isql
:
4. Code Test
Set the ODBCINI
environment variable for the user running the Go program.
Example Go code to connect to the GBase 8s database:
package main
import (
"fmt"
"odbc"
)
func main() {
fmt.Printf("%s\n", "Creating database connection")
conn, _ := odbc.Connect("DSN=gbase14;UID=gbasedbt;PWD=gbasedbt")
fmt.Printf("%s\n", "Connection successful")
stmt1, _ := conn.ExecDirect("create table if not exists test(a int,b char(10))")
stmt1.Execute()
stmt2, _ := conn.ExecDirect("insert into test values(1,'Hello')")
stmt2.Execute()
stmt3, _ := conn.Prepare("select * from test")
stmt3.Execute()
rows, err := stmt3.FetchAll()
for i, row := range rows {
println(i, row.GetInt(0), row.GetString(1))
}
if err != nil {
fmt.Println(err)
return
}
stmt1.Close()
stmt2.Close()
stmt3.Close()
conn.Close()
return
}
Top comments (0)