登录
首页 >  Golang >  Go问答

如何解决连接 Oracle 数据库时出现的错误?

来源:stackoverflow

时间:2024-02-06 18:12:24 352浏览 收藏

小伙伴们有没有觉得学习Golang很有意思?有意思就对了!今天就给大家带来《如何解决连接 Oracle 数据库时出现的错误?》,以下内容将会涉及到,若是在学习中对其中部分知识点有疑问,或许看了本文就能帮到你!

问题内容

dbinfo := dbinfo{ 用户名:“管理员”, 密码:“ddbstjrld1!a”, 服务器:“adb.ap-seoul-1.oraclecloud.com”, 端口:“1522”, 服务:“gee9edfb92f3cf6_redglqwayxqefhhf_high.adb.oraclecloud.com”, 钱包位置:“/users/temp1/desktop/wallet_redglzweyxqefhhf”, }

dbstring := fmt.sprintf(`user="%v" password="%v" connectstring="tcps://%s:%s/%s?wallet_location=%s"`, dbinfo.username, dbinfo.password, dbinfo.server, dbinfo.port, dbinfo.service, dbinfo.walletlocation)

db, err := sql.open("godror", dbstring)
if err != nil {
    fmt.println(err.error())
}
defer db.close()

r, err := db.exec(`create table member_table (
    seq        int not null auto_increment,
    mb_id     varchar(20),
    mb_pw    varchar(100),
    address   varchar(100),
    mb_tell    varchar(20),  
     primary key(seq)
   ) engine= myisam charset=utf8;`)

if err != nil {
    fmt.println(err.error())
}

fmt.println(r)

我是第一次使用oracle。我查了手册并按照它操作,但无法解决。

Error running query
ORA-00000: DPI-1047: Cannot locate a 64-bit Oracle Client library: "dlopen(libclntsh.dylib, 0x0001): tried: 'libclntsh.dylib' (no such file), '/usr/local/lib/libclntsh.dylib' (no such file), '/usr/lib/libclntsh.dylib' (no such file), '/Users/temp1/project/oracleDatabase/libclntsh.dylib' (no such file)". See https://oracle.github.io/odpi/doc/installation.html#macos for help
temp@temp-MacBookPro oracleDatabase %

通过dbeaver连接时,工作正常,但是在golang中使用上述代码连接时,返回以下错误。

我已经苦苦挣扎了好几天了,如果你给我一个简单的例子,我想我可以边分析边学习,你能帮我吗?


正确答案


更新:正如 christopher jones 所指出的,ngoracle 已被弃用,因为的商标问题。替换为 godror

要使用它,请运行:

go get github.com/godror/godror@latest

然后安装 oracle 客户端库。 按照其文档建立连接。

根据 godror 文档,您必须注意:

godror 是 cgo 包。如果您想使用 godror 构建您的应用程序,您 需要 gcc(c 编译器)。

根据提到的错误,您需要导入 oracle 驱动程序 [`goracle`][3],以便 `database/sql` 才能使用 oracle 数据库。
import (
    "database/sql"

    // Import the Oracle driver
    _ "gopkg.in/goracle.v2"
)

p.s.:我建议在问题陈述中添加引发该错误的行号,以便可以轻松调试。

以上就是《如何解决连接 Oracle 数据库时出现的错误?》的详细内容,更多关于的资料请关注golang学习网公众号!

声明:本文转载于:stackoverflow 如有侵犯,请联系study_golang@163.com删除
相关阅读
更多>
最新阅读
更多>
课程推荐
更多>