Open mathiasyeremiaaryadi opened 1 month ago
@mathiasyeremiaaryadi
Please share the output of "db2level" command. Also check the connection to database server and access to the server.
If you are using environment variables then add "viper.AutomaticEnv()" in your go program.
Thanks
@vmathur12 Hi, thank you for the feedback, here is the db2level command that you asked:
DB21085I This instance or install (instance name, where applicable: "*") uses
"64" bits and DB2 code release "SQL11054" with level identifier "0605010F".
Informational tokens are "DB2 v11.5.4000.1449", "s2006161200",
"DYN2006161200WIN64", and Fix Pack "0".
Product is installed at "C:\Users\mathi\go\pkg\mod\github.com\ibmdb\clidriver"
with DB2 Copy Name "IBM Data Server Driver For ODBC and CLI".
And I did use the viper.AutomaticEnv()
as you suggested.
Its finally worked on the external CMD windows, but the error still persist if I run the program in the VS Code CMD.
@mathiasyeremiaaryadi Reading the environment variables from JSON or YAML config files. The following is the sample of JSON config file.
config.json file:
{ "DB2_DATABASE" : "database", "DB2_USERNAME" : "usr", "DB2_PASSWORD" : "password", "DB2_HOSTNAME" : "hostname", "DB2_PORT" : "10000", "DB2_PROTOCOL" : "TCPIP" }
go file:
func DB2() (*sql.DB, error) {
viper.SetConfigName("config")
viper.SetConfigType("json")
viper.AddConfigPath(".")
viper.AutomaticEnv()
viper.ReadInConfig()
dataSourceName := fmt.Sprintf(
"Hostname=%s;Database=%s;Port=%s;UID=%s;PWD=%s",
viper.GetString("DB2_HOSTNAME"),
viper.GetString("DB2_DATABASE"),
viper.GetString("DB2_PORT"),
viper.GetString("DB2_USERNAME"),
viper.GetString("DB2_PASSWORD"),
)
db2Connection, err := sql.Open("go_ibm_db", dataSourceName)
if err != nil {
return db2Connection, err
}
err = db2Connection.Ping()
if err != nil {
return db2Connection, err
}
return db2Connection, nil
}
Hi, I am running Go program with the library on my local Windows and I have followed all the setup instruction with detail below:
Here is the go code for the connection:
$ go env
echo %IBM_DB_HOME%
C:\Users\mathi\go\pkg\mod\github.com\ibmdb\clidriver
echo %PATH%
go run .