使用sijms/go-ora 链接oracle server

sijms/go-ora 是纯golang 编写的oracle 驱动(目前不太稳定)但是已经很不错了,以下是参考试用

参考代码

package main
import (
    "database/sql"
    "flag"
    "fmt"
    _ "github.com/sijms/go-ora"
    "log"
)
var (
    username   = flag.String("uname", "scott", "oracle username")
    password   = flag.String("password", "tiger", "oracle password")
    oraclehost = flag.String("oraclehost", "dbhost", "oracle database host")
    oracleport = flag.Int("oracleport", 1521, "oracle database port")
    dbname     = flag.String("dbname", "orclpdb1", "oracle database name")
)
func main() {
    flag.Parse()
    osqlInfo := fmt.Sprintf("oracle://%s:%s@%s:%d/%s", *username, *password, *oraclehost, *oracleport, *dbname)
    db, err := sql.Open("oracle", osqlInfo)
    if err != nil {
        log.Fatalf("connect oracle db error: %s:", err.Error())
    }
    rows, err := db.Query("select to_char(sysdate,'yyyy-mm-dd hh24:mi:ss') AS name from dual")
    if err != nil {
        fmt.Println("exec query error:", err.Error())
    }
    for rows.Next() {
        var name string
        rows.Scan(&name)
        fmt.Println("fetch item:")
        fmt.Println(name)
    }
}

运行说明

go run main.go -uname <username> -password <password> -oraclehost <ip>  -dbname <db>

容器集成

这个就很简单了,直接用最小镜像处理

FROM golang:1.15-alpine AS build-env
WORKDIR /go/src/app
RUN  /bin/sed -i 's,http://dl-cdn.alpinelinux.org,https://mirrors.aliyun.com,g' /etc/apk/repositories
ENV  GO111MODULE=on
ENV  GOPROXY=https://goproxy.cn
COPY . .
RUN apk update && apk add git \
    && go build
FROM alpine:latest
WORKDIR /app
RUN  /bin/sed -i 's,http://dl-cdn.alpinelinux.org,https://mirrors.aliyun.com,g' /etc/apk/repositories
RUN apk update && apk add ca-certificates && rm -rf /var/cache/apk/*
COPY --from=build-env /go/src/app/mygolang-oracle /app/mygolang-oracle
ENTRYPOINT [ "/app/mygolang-oracle" ]

说明

目前sijms/go-ora不是很稳定,但是至少很不错了,是一个值得试用的纯golang oracle驱动

参考资料

https://github.com/sijms/go-ora
https://github.com/rongfengliang/godror-golang-learning/tree/v2

posted on 2020-12-09 17:29  荣锋亮  阅读(2116)  评论(0编辑  收藏  举报

导航