Go 1.1.1和ODBC与MSSQL

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

Go 1.1.1 and ODBC with MSSQL

问题

目前已经安装并运行了FreeTDS 0.92.4 / unixODBC 2.3.1,可以连接到MSSQL服务器并执行查询等操作。

我找到了这个Go的ODBC实现列表,并尝试了其中两个:

mgodbc

我得到了一堆弃用警告(根据头文件来看,它们在OSX 10.8及更高版本中被弃用):

cc1: warnings being treated as errors
mgodbc.go: In function 'mSQLColAttribute':
mgodbc.go:31: warning: 'SQLColAttributeW' is deprecated (declared at /usr/include/sqlucode.h:128)
mgodbc.go: At top level:
mgodbc.go:44: warning: 'SQLDisconnect' is deprecated (declared at /usr/include/sql.h:896)
mgodbc.go:51: warning: 'SQLGetDiagRecW' is deprecated (declared at /usr/include/sqlucode.h:233)
mgodbc.go:62: warning: 'SQLGetInfoW' is deprecated (declared at /usr/include/sqlucode.h:273)
mgodbc.go:67: warning: 'SQLBindParameter' is deprecated (declared at /usr/include/sqlext.h:2519)
mgodbc.go:70: warning: 'SQLDriverConnectW' is deprecated (declared at /usr/include/sqlucode.h:336)
mgodbc.go:73: warning: 'SQLSetEnvAttr' is deprecated (declared at /usr/include/sql.h:1120)
mgodbc.go:74: warning: 'SQLFreeHandle' is deprecated (declared at /usr/include/sql.h:942)
mgodbc.go:75: warning: 'SQLSetConnectAttrW' is deprecated (declared at /usr/include/sqlucode.h:245)
mgodbc.go:78: warning: 'SQLGetDiagFieldW' is deprecated (declared at /usr/include/sqlucode.h:223)
mgodbc.go:82: warning: 'SQLRowCount' is deprecated (declared at /usr/include/sql.h:1076)
mgodbc.go:98: warning: 'SQLGetData' is deprecated (declared at /usr/include/sql.h:975)
mgodbc.go:99: warning: 'SQLEndTran' is deprecated (declared at /usr/include/sql.h:902)
mgodbc.go:102: warning: 'SQLCloseCursor' is deprecated (declared at /usr/include/sql.h:831)
mgodbc.go:103: warning: 'SQLPrepareW' is deprecated (declared at /usr/include/sqlucode.h:239)
mgodbc.go:107: warning: 'SQLNumResultCols' is deprecated (declared at /usr/include/sql.h:1058)
mgodbc.go:113: warning: 'SQLAllocHandle' is deprecated (declared at /usr/include/sql.h:799)
mgodbc.go:114: warning: 'SQLExecute' is deprecated (declared at /usr/include/sql.h:921)
mgodbc.go:115: warning: 'SQLFetch' is deprecated (declared at /usr/include/sql.h:924)
mgodbc.go:119: warning: 'SQLNumParams' is deprecated (declared at /usr/include/sqlext.h:2448)

更新

根据go-nuts irc上mac01021的建议,我添加了以下内容:

#pragma GCC diagnostic ignored "-Wdeprecated-declarations"

这样就消除了弃用警告,但仍无法构建与OS X附带的iODBC一起使用的mgodbc。

将上述行替换为:

#cgo darwin CFLAGS: -I/opt/local/include

然后,mgodbc现在可以构建(使用已安装的unixODBC)了,但是在导入时会出现一个小的段错误。

odbc

我得到了构建错误:

# code.google.com/p/odbc/api
api/api.go:13: undefined: SQLSMALLINT
api/api.go:14: undefined: SQLUSMALLINT
api/api.go:15: undefined: SQLUSMALLINT
api/api.go:19: undefined: SQLSMALLINT
api/api.go:20: undefined: SQLUSMALLINT
api/api.go:21: undefined: SQLUSMALLINT
api/api.go:22: undefined: SQLUSMALLINT
api/api.go:23: undefined: SQLUSMALLINT
api/api.go:24: undefined: SQLUSMALLINT
api/api.go:25: undefined: SQLUINTEGER
api/api.go:25: too many errors

更新

感谢@alex提供的cgo信息。我已经使用以下内容修改了api_unix.go

// Copyright 2012 The Go Authors. All rights reserved.
// Use of this source code is governed by a BSD-style
// license that can be found in the LICENSE file.

// +build linux darwin
// +build cgo

package api

// #cgo linux LDFLAGS: -lodbc
// #cgo darwin LDFLAGS: -lodbc
// #cgo darwin CFLAGS: -I /opt/local/include
// #include <sql.h>
// #include <sqlext.h>
import "C"

OS X附带的iODBC有一些被列为弃用的东西(我过去在unixODBC上运行得更好)。

我将-I /opt/local/include添加到CFLAGS中,希望指向unixODBC的头文件,而不是Apple提供的头文件(其中包含弃用警告等)。

运行# go build -x给我:

WORK=/var/folders/z2/k9vxn7gn6395vb3y2qc7_1040000gn/T/go-build784364461
mkdir -p $WORK/code.google.com/p/odbc/api/_obj/
mkdir -p $WORK/code.google.com/p/odbc/
cd /Users/jr/Development/go/src/code.google.com/p/odbc/api
/usr/local/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/code.google.com/p/odbc/api/_obj/ -- -I /opt/local/include -I $WORK/code.google.com/p/odbc/api/_obj/ api_unix.go
/usr/local/go/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/code.google.com/p/odbc/api/_obj/ -I /usr/local/go/pkg/darwin_amd64 -o $WORK/code.google.com/p/odbc/api/_obj/_cgo_defun.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/code.google.com/p/odbc/api/_obj/_cgo_defun.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -print-libgcc-file-name
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I /opt/local/include -I $WORK/code.google.com/p/odbc/api/_obj/ -o $WORK/code.google.com/p/odbc/api/_obj/_cgo_main.o -c $WORK/code.google.com/p/odbc/api/_obj/_cgo_main.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I /opt/local/include -I $WORK/code.google.com/p/odbc/api/_obj/ -o $WORK/code.google.com/p/odbc/api/_obj/_cgo_export.o -c $WORK/code.google.com/p/odbc/api/_obj/_cgo_export.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I /opt/local/include -I $WORK/code.google.com/p/odbc/api/_obj/ -o $WORK/code.google.com/p/odbc/api/_obj/api_unix.cgo2.o -c $WORK/code.google.com/p/odbc/api/_obj/api_unix.cgo2.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/code.google.com/p/odbc/api/_obj/_cgo_.o $WORK/code.google.com/p/odbc/api/_obj/_cgo_main.o $WORK/code.google.com/p/odbc/api/_obj/_cgo_export.o $WORK/code.google.com/p/odbc/api/_obj/api_unix.cgo2.o -lodbc
/usr/local/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/code.google.com/p/odbc/api/_obj/ -dynimport $WORK/code.google.com/p/odbc/api/_obj/_cgo_.o -dynout $WORK/code.google.com/p/odbc/api/_obj/_cgo_import.c
/usr/local/go/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/code.google.com/p/odbc/api/_obj/ -I /usr/local/go/pkg/darwin_amd64 -o $WORK/code.google.com/p/odbc/api/_obj/_cgo_import.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/code.google.com/p/odbc/api/_obj/_cgo_import.c
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/code.google.com/p/odbc/api/_obj/_all.o $WORK/code.google.com/p/odbc/api/_obj/_cgo_export.o $WORK/code.google.com/p/odbc/api/_obj/api_unix.cgo2.o -Wl,-r -nostdlib /usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/x86_64/libgcc.a
/usr/local/go/pkg/tool/darwin_amd64/6g -o $WORK/code.google.com/p/odbc/api/_obj/_go_.6 -p code.google.com/p/odbc/api -D _/Users/jr/Development/go/src/code.google.com/p/odbc/api -I $WORK ./api.go $WORK/code.google.com/p/odbc/api/_obj/_cgo_gotypes.go $WORK/code.google.com/p/odbc/api/_obj/api_unix.cgo1.go
/usr/local/go/pkg/tool/darwin_amd64/pack grcP $WORK $WORK/code.google.com/p/odbc/api.a $WORK/code.google.com/p/odbc/api/_obj/_go_.6 $WORK/code.google.com/p/odbc/api/_obj/_cgo_import.6 $WORK/code.google.com/p/odbc/api/_obj/_cgo_defun.6 $WORK/code.google.com/p/odbc/api/_obj/_all.o
mkdir -p $WORK/code.google.com/p/odbc/_obj/
mkdir -p $WORK/code.google.com/p/
cd /Users/jr/Development/go/src/code.google.com/p/odbc
/usr/local/go/pkg/tool/darwin_amd64/6g -o $WORK/code.google.com/p/odbc/_obj/_go_.6 -p code.google.com/p/odbc -complete -D _/Users/jr/Development/go/src/code.google.com/p/odbc -I $WORK -I /Users/jr/Development/go/pkg/darwin_amd64 ./column.go ./conn.go ./driver.go ./error.go ./handle.go ./odbcstmt.go ./param.go ./result.go ./rows.go ./stats.go ./stmt.go ./tx.go
# code.google.com/p/odbc
./column.go:22: undefined: api.SQLGetData
./column.go:28: undefined: api.SQLBindCol
./column.go:47: undefined: api.SQLDescribeCol
./conn.go:20: undefined: api.SQLAllocHandle
./conn.go:28: undefined: api.SQLDriverConnect
./conn.go:39: undefined: api.SQLDisconnect
./driver.go:26: undefined: api.SQLAllocHandle
./driver.go:34: undefined: api.SQLSetEnvAttr
./driver.go:43: undefined: api.SQLSetEnvAttr
./driver.go:50: undefined: api.SQLSetEnvAttr
./driver.go:50: too many errors

看起来头文件路径包含被正确传递了?
但是似乎链接还不太正确?

对于SQLGetData,我在/opt/local/include/sql.h中看到了与api.go中的//sys SQLGetData...注释相匹配的定义

UPDATE

顶部提到的库:
> brainman http://code.google.com/p/odbc/

现在可以在OSX上作为可获取的go包使用了。甚至有文档可以帮助您开始使用odbc/tds部分。

英文:

Currently have FreeTDS 0.92.4 / unixODBC 2.3.1 up and running, connecting with a MSSQL server and able to execute queries etc.

I've found this list of ODBC implementations for Go, and have tried the two in bold:


mgodbc

I'm getting a bunch of deprecation warnings (looking at the headers they're deprecated for OSX 10.8 and later):

cc1: warnings being treated as errors
mgodbc.go: In function &#39;mSQLColAttribute&#39;:
mgodbc.go:31: warning: &#39;SQLColAttributeW&#39; is deprecated (declared at /usr/include/sqlucode.h:128)
mgodbc.go: At top level:
mgodbc.go:44: warning: &#39;SQLDisconnect&#39; is deprecated (declared at /usr/include/sql.h:896)
mgodbc.go:51: warning: &#39;SQLGetDiagRecW&#39; is deprecated (declared at /usr/include/sqlucode.h:233)
mgodbc.go:62: warning: &#39;SQLGetInfoW&#39; is deprecated (declared at /usr/include/sqlucode.h:273)
mgodbc.go:67: warning: &#39;SQLBindParameter&#39; is deprecated (declared at /usr/include/sqlext.h:2519)
mgodbc.go:70: warning: &#39;SQLDriverConnectW&#39; is deprecated (declared at /usr/include/sqlucode.h:336)
mgodbc.go:73: warning: &#39;SQLSetEnvAttr&#39; is deprecated (declared at /usr/include/sql.h:1120)
mgodbc.go:74: warning: &#39;SQLFreeHandle&#39; is deprecated (declared at /usr/include/sql.h:942)
mgodbc.go:75: warning: &#39;SQLSetConnectAttrW&#39; is deprecated (declared at /usr/include/sqlucode.h:245)
mgodbc.go:78: warning: &#39;SQLGetDiagFieldW&#39; is deprecated (declared at /usr/include/sqlucode.h:223)
mgodbc.go:82: warning: &#39;SQLRowCount&#39; is deprecated (declared at /usr/include/sql.h:1076)
mgodbc.go:98: warning: &#39;SQLGetData&#39; is deprecated (declared at /usr/include/sql.h:975)
mgodbc.go:99: warning: &#39;SQLEndTran&#39; is deprecated (declared at /usr/include/sql.h:902)
mgodbc.go:102: warning: &#39;SQLCloseCursor&#39; is deprecated (declared at /usr/include/sql.h:831)
mgodbc.go:103: warning: &#39;SQLPrepareW&#39; is deprecated (declared at /usr/include/sqlucode.h:239)
mgodbc.go:107: warning: &#39;SQLNumResultCols&#39; is deprecated (declared at /usr/include/sql.h:1058)
mgodbc.go:113: warning: &#39;SQLAllocHandle&#39; is deprecated (declared at /usr/include/sql.h:799)
mgodbc.go:114: warning: &#39;SQLExecute&#39; is deprecated (declared at /usr/include/sql.h:921)
mgodbc.go:115: warning: &#39;SQLFetch&#39; is deprecated (declared at /usr/include/sql.h:924)
mgodbc.go:119: warning: &#39;SQLNumParams&#39; is deprecated (declared at /usr/include/sqlext.h:2448)

Update

Following a suggestion from mac01021, from go-nuts irc, I've added

#pragma GCC diagnostic ignored &quot;-Wdeprecated-declarations&quot;  

This gets rid of the deprecation warnings, but still doesn't build against the included iODBC of OS X

Replacing the above line with:

#cgo darwin CFLAGS: -I/opt/local/include 

and voila, mgodbc builds now (using the installed unixODBC)
although now a nice little segfault occurs upon import =(


odbc

I'm getting build errors:

# code.google.com/p/odbc/api
api/api.go:13: undefined: SQLSMALLINT  
api/api.go:14: undefined: SQLUSMALLINT  
api/api.go:15: undefined: SQLUSMALLINT  
api/api.go:19: undefined: SQLSMALLINT  
api/api.go:20: undefined: SQLUSMALLINT  
api/api.go:21: undefined: SQLUSMALLINT  
api/api.go:22: undefined: SQLUSMALLINT  
api/api.go:23: undefined: SQLUSMALLINT  
api/api.go:24: undefined: SQLUSMALLINT  
api/api.go:25: undefined: SQLUINTEGER  
api/api.go:25: too many errors  

Update

Thanks @alex for the cgo info. I've modified api_unix.go with the below

// Copyright 2012 The Go Authors. All rights reserved.  
// Use of this source code is governed by a BSD-style  
// license that can be found in the LICENSE file.  

// +build linux darwin  
// +build cgo  

package api  

// #cgo linux LDFLAGS: -lodbc  
// #cgo darwin LDFLAGS: -lodbc  
// #cgo darwin CFLAGS: -I /opt/local/include  
// #include &lt;sql.h&gt;  
// #include &lt;sqlext.h&gt;  
import &quot;C&quot;  

The iODBC included with OS X has some things that are listed as deprecated (and I've had better luck with unixODBC in the past)

I've added the -I /opt/local/include to the CFLAGS to, hopefully, point to the unixODBC headers, and not the ones included by Apple (which have the deprecation warnings etc.)

Running # go build -x gives me:

WORK=/var/folders/z2/k9vxn7gn6395vb3y2qc7_1040000gn/T/go-build784364461  
mkdir -p $WORK/code.google.com/p/odbc/api/_obj/  
mkdir -p $WORK/code.google.com/p/odbc/  
cd /Users/jr/Development/go/src/code.google.com/p/odbc/api  
/usr/local/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/code.google.com/p/odbc/api/_obj/ -- -I /opt/local/include -I $WORK/code.google.com/p/odbc/api/_obj/ api_unix.go  
/usr/local/go/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/code.google.com/p/odbc/api/_obj/ -I /usr/local/go/pkg/darwin_amd64 -o $WORK/code.google.com/p/odbc/api/_obj/_cgo_defun.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/code.google.com/p/odbc/api/_obj/_cgo_defun.c  
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -print-libgcc-file-name
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I /opt/local/include -I $WORK/code.google.com/p/odbc/api/_obj/ -o $WORK/code.google.com/p/odbc/api/_obj/_cgo_main.o -c $WORK/code.google.com/p/odbc/api/_obj/_cgo_main.c  
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I /opt/local/include -I $WORK/code.google.com/p/odbc/api/_obj/ -o $WORK/code.google.com/p/odbc/api/_obj/_cgo_export.o -c $WORK/code.google.com/p/odbc/api/_obj/_cgo_export.c  
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -I /opt/local/include -I $WORK/code.google.com/p/odbc/api/_obj/ -o $WORK/code.google.com/p/odbc/api/_obj/api_unix.cgo2.o -c $WORK/code.google.com/p/odbc/api/_obj/api_unix.cgo2.c  
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/code.google.com/p/odbc/api/_obj/_cgo_.o $WORK/code.google.com/p/odbc/api/_obj/_cgo_main.o $WORK/code.google.com/p/odbc/api/_obj/_cgo_export.o $WORK/code.google.com/p/odbc/api/_obj/api_unix.cgo2.o -lodbc
/usr/local/go/pkg/tool/darwin_amd64/cgo -objdir $WORK/code.google.com/p/odbc/api/_obj/ -dynimport $WORK/code.google.com/p/odbc/api/_obj/_cgo_.o -dynout $WORK/code.google.com/p/odbc/api/_obj/_cgo_import.c  
/usr/local/go/pkg/tool/darwin_amd64/6c -F -V -w -I $WORK/code.google.com/p/odbc/api/_obj/ -I /usr/local/go/pkg/darwin_amd64 -o $WORK/code.google.com/p/odbc/api/_obj/_cgo_import.6 -D GOOS_darwin -D GOARCH_amd64 $WORK/code.google.com/p/odbc/api/_obj/_cgo_import.c  
gcc -I . -g -O2 -fPIC -m64 -pthread -fno-common -o $WORK/code.google.com/p/odbc/api/_obj/_all.o $WORK/code.google.com/p/odbc/api/_obj/_cgo_export.o $WORK/code.google.com/p/odbc/api/_obj/api_unix.cgo2.o -Wl,-r -nostdlib /usr/llvm-gcc-4.2/bin/../lib/gcc/i686-apple-darwin11/4.2.1/x86_64/libgcc.a  
/usr/local/go/pkg/tool/darwin_amd64/6g -o $WORK/code.google.com/p/odbc/api/_obj/_go_.6 -p code.google.com/p/odbc/api -D _/Users/jr/Development/go/src/code.google.com/p/odbc/api -I $WORK ./api.go $WORK/code.google.com/p/odbc/api/_obj/_cgo_gotypes.go $WORK/code.google.com/p/odbc/api/_obj/api_unix.cgo1.go  
/usr/local/go/pkg/tool/darwin_amd64/pack grcP $WORK $WORK/code.google.com/p/odbc/api.a $WORK/code.google.com/p/odbc/api/_obj/_go_.6 $WORK/code.google.com/p/odbc/api/_obj/_cgo_import.6 $WORK/code.google.com/p/odbc/api/_obj/_cgo_defun.6 $WORK/code.google.com/p/odbc/api/_obj/_all.o  
mkdir -p $WORK/code.google.com/p/odbc/_obj/  
mkdir -p $WORK/code.google.com/p/  
cd /Users/jr/Development/go/src/code.google.com/p/odbc  
/usr/local/go/pkg/tool/darwin_amd64/6g -o $WORK/code.google.com/p/odbc/_obj/_go_.6 -p code.google.com/p/odbc -complete -D _/Users/jr/Development/go/src/code.google.com/p/odbc -I $WORK -I /Users/jr/Development/go/pkg/darwin_amd64 ./column.go ./conn.go ./driver.go ./error.go ./handle.go ./odbcstmt.go ./param.go ./result.go ./rows.go ./stats.go ./stmt.go ./tx.go  
# code.google.com/p/odbc  
./column.go:22: undefined: api.SQLGetData  
./column.go:28: undefined: api.SQLBindCol  
./column.go:47: undefined: api.SQLDescribeCol  
./conn.go:20: undefined: api.SQLAllocHandle  
./conn.go:28: undefined: api.SQLDriverConnect  
./conn.go:39: undefined: api.SQLDisconnect  
./driver.go:26: undefined: api.SQLAllocHandle  
./driver.go:34: undefined: api.SQLSetEnvAttr  
./driver.go:43: undefined: api.SQLSetEnvAttr  
./driver.go:50: undefined: api.SQLSetEnvAttr  
./driver.go:50: too many errors  

It looks like the header path include is being passed correctly?
But still looks like things are not quite linking correctly?

for SQLGetData I do see a matching definition from the //sys SQLGetData... comment from api.go within /opt/local/include/sql.h


UPDATE

The library mentioned at the top:
> brainman http://code.google.com/p/odbc/

Now works as a go-gettable package on OSX. There's even documentation to get you started with the odbc/tds portions.

答案1

得分: 3

在Windows上:

&gt;go version
go version devel +edd229b63fa4 Wed Jun 26 11:36:18 2013 -0700 windows/amd64
&gt;go get -v code.google.com/p/odbc
code.google.com/p/odbc (下载)
code.google.com/p/odbc/api
code.google.com/p/odbc
&gt;go get -v bitbucket.org/miquella/mgodbc
bitbucket.org/miquella/mgodbc (下载)
bitbucket.org/miquella/mgodbc
&gt;

在Linux上:

$ go version
go version devel +65e2aba21abe Wed Jun 26 13:14:11 2013 -0700 linux/amd64
$ sudo apt-get install unixodbc unixodbc-dev
读取软件包列表... 完成
构建依赖树       
读取状态信息... 完成
unixodbc 已经是最新版本。
unixodbc-dev 已经是最新版本。
0 升级,0 新安装,0 用于删除和 10 未升级。
$ go get -v code.google.com/p/odbc
code.google.com/p/odbc (下载)
code.google.com/p/odbc/api
code.google.com/p/odbc
$ go get -v bitbucket.org/miquella/mgodbc
bitbucket.org/miquella/mgodbc (下载)
bitbucket.org/miquella/mgodbc
$ 

在Darwin上,您似乎遇到了SQL头文件的问题。

/usr/include/sql.h
/usr/include/sqlext.h
/usr/include/sqltypes.h
/usr/include/sqlucode.h

请联系odbc软件包的作者报告这些问题。

更新:

当您告诉我们错误

mgodbc.go:180: cannot convert &amp;state[0] (type *uint16) to type *_Ctype_SQLWCHAR  

您没有告诉我们_Ctype_SQLWCHAR的类型。我们只能猜测!我猜它应该是这样的

package main

func main() {
	type _Ctype_ushort uint16
	type _Ctype_WCHAR _Ctype_ushort
	type _Ctype_SQLWCHAR _Ctype_WCHAR
	var state [6]uint16
	// (*C.SQLWCHAR)(&amp;state[0])
	_ = (*_Ctype_SQLWCHAR)(&amp;state[0])
}

这个代码可以编译通过,没有错误。

更新:

使用系统化的方法解决问题。

阅读有关Go构建约束的文档:Build Constraints, Package build

阅读code.google.com/p/odbc/api中的文件。

code.google.com/p/odbc/api中搜索文件。

$ grep -r &#39;linux&#39; *
api_unix.go:// +build linux
api_unix.go:// #cgo linux LDFLAGS: -lodbc
Makefile:	GOOS=linux ./mksyscall_unix.pl $^ \
mksyscall_unix.pl:// +build linux
mksyscall_unix.pl:// #cgo linux LDFLAGS: -lodbc
zapi_unix.go:// +build linux
zapi_unix.go:// #cgo linux LDFLAGS: -lodbc
$ 

显然,在阅读了api_unix.gozapi_unix.gomksyscall_unix.plMakefile文件之后,当修改了api_unix.go文件时,应该同时修改zapi_unix.go文件。您可能没有这样做。因此,darwin没有包含在zapi_unix.go的构建约束中。您显示为未定义的api包名称在zapi_unix.go中有定义。

api.SQLGetData
api.SQLBindCol  
api.SQLDescribeCol  
api.SQLAllocHandle  
api.SQLDriverConnect  
api.SQLDisconnect  
api.SQLAllocHandle  
api.SQLSetEnvAttr   
英文:

On Windows:

&gt;go version
go version devel +edd229b63fa4 Wed Jun 26 11:36:18 2013 -0700 windows/amd64
&gt;go get -v code.google.com/p/odbc
code.google.com/p/odbc (download)
code.google.com/p/odbc/api
code.google.com/p/odbc
&gt;go get -v bitbucket.org/miquella/mgodbc
bitbucket.org/miquella/mgodbc (download)
bitbucket.org/miquella/mgodbc
&gt;

On Linux:

$ go version
go version devel +65e2aba21abe Wed Jun 26 13:14:11 2013 -0700 linux/amd64
$ sudo apt-get install unixodbc unixodbc-dev
Reading package lists... Done
Building dependency tree       
Reading state information... Done
unixodbc is already the newest version.
unixodbc-dev is already the newest version.
0 upgraded, 0 newly installed, 0 to remove and 10 not upgraded.
$ go get -v code.google.com/p/odbc
code.google.com/p/odbc (download)
code.google.com/p/odbc/api
code.google.com/p/odbc
$ go get -v bitbucket.org/miquella/mgodbc
bitbucket.org/miquella/mgodbc (download)
bitbucket.org/miquella/mgodbc
$ 

On Darwin, you seem to have issues with SQL header files.

/usr/include/sql.h
/usr/include/sqlext.h
/usr/include/sqltypes.h
/usr/include/sqlucode.h

Contact the odbc package authors to report these problems.

UPDATE:

When you tell us about the error

mgodbc.go:180: cannot convert &amp;state[0] (type *uint16) to type *_Ctype_SQLWCHAR  

you don't tell us what the type of _Ctype_SQLWCHAR is. We can only guess! My guess would be that it should look like this

package main

func main() {
	type _Ctype_ushort uint16
	type _Ctype_WCHAR _Ctype_ushort
	type _Ctype_SQLWCHAR _Ctype_WCHAR
	var state [6]uint16
	// (*C.SQLWCHAR)(&amp;state[0])
	_ = (*_Ctype_SQLWCHAR)(&amp;state[0])
}

which compiles with no errors.

UPDATE:

Use a systematic approach to problem solving.

Read about Go build contraints: Build Constraints, Package build.

Read the files in code.google.com/p/odbc/api.

Grep the files in code.google.com/p/odbc/api.

$ grep -r &#39;linux&#39; *
api_unix.go:// +build linux
api_unix.go:// #cgo linux LDFLAGS: -lodbc
Makefile:	GOOS=linux ./mksyscall_unix.pl $^ \
mksyscall_unix.pl:// +build linux
mksyscall_unix.pl:// #cgo linux LDFLAGS: -lodbc
zapi_unix.go:// +build linux
zapi_unix.go:// #cgo linux LDFLAGS: -lodbc
$ 

Clearly, after reading the api_unix.go, zapi_unix.go, mksyscall_unix.pl, and Makefile files, when the api_unix.go file is modified, the zapi_unix.go file should be modified too.

You probably didn't do that. Therefore, darwin is not included in the zapi_unix.go build constraints. The package api names that you show as undefined are defined in zapi_unix.go.

api.SQLGetData
api.SQLBindCol  
api.SQLDescribeCol  
api.SQLAllocHandle  
api.SQLDriverConnect  
api.SQLDisconnect  
api.SQLAllocHandle  
api.SQLSetEnvAttr   

huangapple
  • 本文由 发表于 2013年6月27日 03:28:45
  • 转载请务必保留本文链接:https://go.coder-hub.com/17328798.html
匿名

发表评论

匿名网友

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

确定