1.安装依赖
go get github.com/cengsin/oracle
有可能会出现如下错误:
# github.com/godror/godror cgo: C compiler "gcc" not found: exec: "gcc": executable file not found in %PATH% ?
解决措施:安装 gcc 环境
打开网址:https://sourceforge.net/projects/mingw-w64/files/mingw-w64/
下载MinGW-w64,window64位可以选择下载? x86_64-posix-seh 这个压缩包
?解压压缩包后,大概这个样子
?将 bin 目录完整路径配置在环境变量PATH中
重新执行安装依赖的命令(注意:如果使用IDEA的终端执行命令行时,IDEA需要重启)
go get github.com/cengsin/oracle
依赖安装完成后,大概这个样子
?2.编写代码
package main
import (
"fmt"
"github.com/cengsin/oracle"
"gorm.io/gorm"
"gorm.io/gorm/logger"
"log"
"os"
"time"
)
type Tmp struct {
Id string `json:"id"`
}
func main() {
log.Println("initial database connect……")
db, err := gorm.Open(oracle.Open("username/password@ip:port/orcl"), &gorm.Config{
Logger: logger.New(log.New(os.Stdout, "\r\n", log.LstdFlags), logger.Config{
SlowThreshold: 1 * time.Millisecond,
LogLevel: logger.Info,
Colorful: false,
}),
})
if err != nil {
log.Fatalln(err)
}
var list []Tmp
db.Raw("select * from tmp").Scan(&list)
for _, tmp := range list {
fmt.Printf("%v\n",tmp)
}
}
|