使用golang在Mac OS上读取.mdb文件,且使用Apple Silicon架构。

huangapple go评论88阅读模式
英文:

Read .mdb file using golang on mac os with apple silicon architecture

问题

我在使用golang在macOS上的苹果芯片上读取.mdb文件时遇到了问题。

这是我使用的源代码:

package main

import (
	"log"
	_ "runtime/cgo"

	accessdbwe "github.com/bennof/AccessDBwE"
	_ "github.com/mattn/go-adodb"
)

func main() {
	_, err := accessdbwe.Open("adodb", "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=PetStore2002.mdb;")
	if err != nil {
		log.Fatalf("无法打开mdb文件:%T, %q", err, err)
	}

	println("成功连接到mdb")
}

当代码运行时,err变量不是nil,但err.String()为空。所以我对错误是什么感到非常困惑。

我可以在Windows笔记本上成功运行相同的代码。我的代码中是否有遗漏的部分?

注意:我已经使用mdbtools、mdb viewer和ms access在Windows上检查了我的.mdb文件,文件是有效的。

这是我的go版本:

go version go1.20.2 darwin/arm64
英文:

I had an issue while reading .mdb file in golang on macOS with apple silicon.

This is the source code that I use:

package main

import (
	"log"
	_ "runtime/cgo"

	accessdbwe "github.com/bennof/AccessDBwE"
	_ "github.com/mattn/go-adodb"
)

func main() {
	_, err := accessdbwe.Open("adodb", "Provider=Microsoft.ACE.OLEDB.12.0;Data Source=PetStore2002.mdb;")
	if err != nil {
		log.Fatalf("failed to open mdb file: %T, %q", err, err)
	}

	println("success ping mdb")
}

When the code run, the err variable is not nil but err.String() is empty. So I'm very confused about what is the error.

I can run the same code successfully on a windows laptop. Is there anything missed in my code?

Note: I've checked my .mdb file using mdbtools, mdb viewer and ms access on windows and the file is valid.

This my go version:

go version go1.20.2 darwin/arm64

答案1

得分: 4

TL;DR

截至目前,github.com/mattn/go-adodb 包不支持在非Windows系统上读取 .mdb 文件。

为什么在非Windows系统上不支持?

github.com/mattn/go-adodb 是一个“符合内置 database/sql 接口的 Microsoft ADODB 驱动程序”,它依赖于 github.com/go-ole/go-ole 与数据库进行通信。

底层包 github.com/go-ole/go-ole 提供了“使用共享库而不是 cgo 的 Windows COM 的 Go 绑定”,目前不支持非Windows系统。以下是该包的 README.md 文件中的说明:

> 目前也无法在 Linux 上测试该库,因为 COM API 是特定于 Windows 的,目前无法在 Linux 上运行 COM 服务器或连接到远程 COM 服务器。

为什么 err.String() 是空的?

关于空错误消息,这是因为错误是一个 ole.OleError。在描述为空的情况下,它调用 errstr(int(v.hr)) 将错误代码转换为字符串。在非Windows系统上,errstr 函数 只返回一个空字符串。

你可以使用调试器查找错误的详细信息。很可能错误代码是 0x80004001,即 E_NOTIMPL: 未实现(参见 Common HRESULT Values)。

使用golang在Mac OS上读取.mdb文件,且使用Apple Silicon架构。

英文:

TL;DR

Reading .mdb file on non-Windows is not supported by the package github.com/mattn/go-adodb as of now.

Why it's not supported on non-Windows?

github.com/mattn/go-adodb is a "Microsoft ADODB driver conforming to the built-in database/sql interface", which depends on github.com/go-ole/go-ole to communicate with the database.

The underlying package github.com/go-ole/go-ole provides "Go bindings for Windows COM using shared libraries instead of cgo", which does not support non-Windows as of now. Here is the statement from the README.md file of the package:

> It is also not currently possible to test the library on Linux, since COM API is specific to Windows and it is not currently possible to run a COM server on Linux or even connect to a remote COM server.

Why err.String() is empty?

Regarding the empty error message, that's because the error is an ole.OleError. In the case when the description is empty, it calls errstr(int(v.hr)) to convert error code to string. And on non-Windows, the errstr func just returns an empty string.

You can use a debugger to find out the details of the error. It's most likely that the error code is 0x80004001, which is E_NOTIMPL: Not implemented (see Common HRESULT Values).

使用golang在Mac OS上读取.mdb文件,且使用Apple Silicon架构。

huangapple
  • 本文由 发表于 2023年4月15日 08:22:41
  • 转载请务必保留本文链接:https://go.coder-hub.com/76019761.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定