golang mac os接続oracleデータベース
2779 ワード
connect oracle database with golang in mac os
coding in mac os ,build go file ,test connect to oracle db
mac osにgoコードを記述し、oracleデータベースへの接続をテストします。
インストールクライアントのダウンロード
instantclient-basic-macos.x64-11.2.0.4.0 instantclient-sdk-macos.x64-11.2.0.4.0 download http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html
解凍
2つのファイルを解凍し、1つのディレクトリにマージします./Users/nagatyase/instantclient_11_2
/Users/nagatyase/instantclient_11_2/sdk
コピー、リンク
cd /Users/nagatyase/instantclient_11_2
cp libclntsh.dylib.11.1 libclntsh.dylib
ln libclntsh.dylib /usr/lib/libclntsh.dylib
ln libocci.dylib.11.1 /usr/lib/libocci.dylib
ln libociei.dylib /usr/lib/libociei.dylib
ln libnnz11.dylib /usr/lib/libnnz11.dylib
リンク失敗、実行
sudo chown -R $(whoami)
sudo chown -R $(whoami) /usr/lib
pkg-configのダウンロード
download https://lists.freedesktop.org/archives/pkg-config/2017-March/001084.html Guide to pkg-config https://people.freedesktop.org/~dbn/pkg-config-guide.html#faqまたはbrewでインストールsudo brew install pkg-config
./configure --with-internal-glib
make
sudo make install
新しいoci 8.pc
内容は以下の通りprefixdir=/Users/nagatyase/instantclient_11_2
libdir=${prefixdir}
includedir=${prefixdir}/sdk/include
Name: OCI
Description: Oracle database driver
Version: 12.2
Libs: -L${libdir} -lclntsh
Cflags: -I${includedir}
oci8.PC位置/Users/nagatyase/instantclient_11_2
環境変数の設定
PKG_CONFIG_PATH=/Users/nagatyase/instantclient_11_2
LD_LIBRARY_PATH=/Users/nagatyase/instantclient_11_2
テストの実行
go get github.com/mattn/go-oci8
作成oracle_db.go package main
import (
"fmt"
_ "github.com/mattn/go-oci8"
"database/sql"
)
func main() {
db, err := sql.Open("oci8", "username/pwd@ip:1521/dbname")
if err != nil {
fmt.Println("abc", 123, err)
return
}
defer db.Close()
if err = db.Ping(); err != nil {
fmt.Printf("Error connecting to the database: %s
", err)
return
}
rows, err := db.Query("select 2+2 from dual")
if err != nil {
fmt.Println("Error fetching addition")
fmt.Println(err)
return
}
defer rows.Close()
for rows.Next() {
var sum int
rows.Scan(&sum)
fmt.Printf("2 + 2 always equals: %d
", sum)
}
}
go run oracle_db.go
2 + 2 always equals: 4
mac osにgoコードを記述し、oracleデータベースへの接続をテストします。
インストールクライアントのダウンロード
instantclient-basic-macos.x64-11.2.0.4.0 instantclient-sdk-macos.x64-11.2.0.4.0 download http://www.oracle.com/technetwork/topics/intel-macsoft-096467.html
解凍
2つのファイルを解凍し、1つのディレクトリにマージします./Users/nagatyase/instantclient_11_2
/Users/nagatyase/instantclient_11_2/sdk
コピー、リンク
cd /Users/nagatyase/instantclient_11_2
cp libclntsh.dylib.11.1 libclntsh.dylib
ln libclntsh.dylib /usr/lib/libclntsh.dylib
ln libocci.dylib.11.1 /usr/lib/libocci.dylib
ln libociei.dylib /usr/lib/libociei.dylib
ln libnnz11.dylib /usr/lib/libnnz11.dylib
リンク失敗、実行
sudo chown -R $(whoami)
sudo chown -R $(whoami) /usr/lib
pkg-configのダウンロード
download https://lists.freedesktop.org/archives/pkg-config/2017-March/001084.html Guide to pkg-config https://people.freedesktop.org/~dbn/pkg-config-guide.html#faqまたはbrewでインストールsudo brew install pkg-config
./configure --with-internal-glib
make
sudo make install
新しいoci 8.pc
内容は以下の通りprefixdir=/Users/nagatyase/instantclient_11_2
libdir=${prefixdir}
includedir=${prefixdir}/sdk/include
Name: OCI
Description: Oracle database driver
Version: 12.2
Libs: -L${libdir} -lclntsh
Cflags: -I${includedir}
oci8.PC位置/Users/nagatyase/instantclient_11_2
環境変数の設定
PKG_CONFIG_PATH=/Users/nagatyase/instantclient_11_2
LD_LIBRARY_PATH=/Users/nagatyase/instantclient_11_2
テストの実行
go get github.com/mattn/go-oci8
作成oracle_db.go package main
import (
"fmt"
_ "github.com/mattn/go-oci8"
"database/sql"
)
func main() {
db, err := sql.Open("oci8", "username/pwd@ip:1521/dbname")
if err != nil {
fmt.Println("abc", 123, err)
return
}
defer db.Close()
if err = db.Ping(); err != nil {
fmt.Printf("Error connecting to the database: %s
", err)
return
}
rows, err := db.Query("select 2+2 from dual")
if err != nil {
fmt.Println("Error fetching addition")
fmt.Println(err)
return
}
defer rows.Close()
for rows.Next() {
var sum int
rows.Scan(&sum)
fmt.Printf("2 + 2 always equals: %d
", sum)
}
}
go run oracle_db.go
2 + 2 always equals: 4
cd /Users/nagatyase/instantclient_11_2
cp libclntsh.dylib.11.1 libclntsh.dylib
ln libclntsh.dylib /usr/lib/libclntsh.dylib
ln libocci.dylib.11.1 /usr/lib/libocci.dylib
ln libociei.dylib /usr/lib/libociei.dylib
ln libnnz11.dylib /usr/lib/libnnz11.dylib
sudo chown -R $(whoami)
sudo chown -R $(whoami) /usr/lib
sudo brew install pkg-config
./configure --with-internal-glib
make
sudo make install
prefixdir=/Users/nagatyase/instantclient_11_2
libdir=${prefixdir}
includedir=${prefixdir}/sdk/include
Name: OCI
Description: Oracle database driver
Version: 12.2
Libs: -L${libdir} -lclntsh
Cflags: -I${includedir}
PKG_CONFIG_PATH=/Users/nagatyase/instantclient_11_2
LD_LIBRARY_PATH=/Users/nagatyase/instantclient_11_2
go get github.com/mattn/go-oci8
package main
import (
"fmt"
_ "github.com/mattn/go-oci8"
"database/sql"
)
func main() {
db, err := sql.Open("oci8", "username/pwd@ip:1521/dbname")
if err != nil {
fmt.Println("abc", 123, err)
return
}
defer db.Close()
if err = db.Ping(); err != nil {
fmt.Printf("Error connecting to the database: %s
", err)
return
}
rows, err := db.Query("select 2+2 from dual")
if err != nil {
fmt.Println("Error fetching addition")
fmt.Println(err)
return
}
defer rows.Close()
for rows.Next() {
var sum int
rows.Scan(&sum)
fmt.Printf("2 + 2 always equals: %d
", sum)
}
}
go run oracle_db.go
2 + 2 always equals: 4