1.下载odbc驱动
下载后安装psqlodbc_x64.msi
安装成功后可在odbc数据源中看到以下内容
2.测试odbc驱动
在用户dsn中选择添加
输入对应的连接内容点击test
如果显示以下内容则表示驱动正常可使用odbc连接高斯数据库
3.go 语言编写
其中依赖包需要使用命令安装
go get github.com/alexbrainman/odbc
go get github.com/axgle/mahonia
go get github.com/go-xorm/xorm
go get xorm.io/core
1)如果出现 golang.org/x/ 下包下载不下来的问题
https://studygolang.com/articles/19051?fr=sidebar
https://studygolang.com/articles/24075?fr=sidebar
package mainimport ("fmt"_ "github.com/alexbrainman/odbc" // google's odbc driver"github.com/axgle/mahonia""github.com/go-xorm/xorm""xorm.io/core"
)type Address struct {Addressid int64 `xorm:"addressid"`Address1 string `xorm:"address1"`Address2 string `xorm:"address2"`City string `xorm:"city"`Postalcode string `xorm:"postalcode"`
}// 字符串解码函数,处理中文乱码
func ConvertToString(src string, srcCode string, tagCode string) string {srcCoder := mahonia.NewDecoder(srcCode)srcResult := srcCoder.ConvertString(src)tagCoder := mahonia.NewDecoder(tagCode)_, cdata, _ := tagCoder.Translate([]byte(srcResult), true)result := string(cdata)return result
}func main() {engine, err := xorm.NewEngine("odbc", "Driver={PostgreSQL Unicode(x64)};Server=XXXXXX; Port=8000;UID=dbadmin;PWD=XXXXXXX; Database=gaussdb;Pooling=true;Min Pool Size=1")if err != nil {fmt.Println("new engine got error:", err)return}engine.ShowSQL(true) //控制台打印出生成的SQL语句;engine.Logger().SetLevel(core.LOG_DEBUG)if err := engine.Ping(); err != nil {fmt.Println("ping got error:", err)return}// 1) sql查询results, err := engine.Query("select * from table_name")if err != nil {fmt.Println("查询出错:", err)return}for i, e := range results {fmt.Printf("%v\t", i)for k, v := range e {fmt.Printf("%v=%v\t", k, ConvertToString(string(v), "utf-8", "utf-8"))}fmt.Printf("\n")}fmt.Println("*******************************")
}
运行结果