如何设置项目以使用ODBC和MSSQL驱动程序进行工作?

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

How to setup project to work with odbc and mssql drivers?

问题

我正在尝试使用ODBC驱动程序,但是遇到了错误:

.\main.go:5: 导入但未使用:"code.google.com/p/odbc/api"
.\main.go:72: 未定义:Driver
.\main.go:76: 未定义:Driver

我尝试使用另一个驱动程序:go get github.com/denisenkom/go-mssqldb,但遇到了相同类型的问题。这就是为什么我怀疑我没有正确设置环境,但无法找到问题所在的原因。

我的环境如下:

set GOARCH=amd64
set GOBIN=
set GOCHAR=6
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Go\Projects
set GORACE=
set GOROOT=C:\Go
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set TERM=dumb
set CC=gcc
set GOGCCFLAGS=-g -O2 -m64 -mthreads
set CXX=g++
set CGO_ENABLED=1

我使用go get code.google.com/p/odbc安装了ODBC。运行测试并成功完成:

C:\Go\Projects\src\code.google.com\p\odbc>go test -mssrv=.\sqlexp -v -run=MS ... --- PASS: TestMSSQLUTF16ToUTF8 (0.00 seconds) === RUN TestMSSQLExecStoredProcedure --- PASS: TestMSSQLExecStoredProcedure (0.01 seconds) PASS ok code.google.com/p/odbc 0.574s

我的代码如下(从mssql_test.go中复制):

package main
import (
    "code.google.com/p/odbc/api"
    "database/sql"
    "flag"
    "fmt"
    "os"
    "runtime"
    "testing"
    "time"
)

func mssqlConnect() (db *sql.DB, stmtCount int, err error) {
    .........
    return db, db.Driver().(*Driver).Stats.StmtCount, nil
}
func closeDB(t *testing.T, db *sql.DB, shouldStmtCount, ignoreIfStmtCount int) {
    s := db.Driver().(*Driver).Stats
    ......
}

请注意,我只提供了翻译的部分,不包括代码部分。

英文:

I'm trying to use the odbc driver and I'm getting errors:

.\main.go:5: imported and not used: "code.google.com/p/odbc/api"  
.\main.go:72: undefined: Driver  
.\main.go:76: undefined: Driver

I tried to work with another driver: go get github.com/denisenkom/go-mssqldb but ran unto the same type of problem.
That's why I suspect I didn't set up the environment right but unable to find the issue.


#My environment:#

set GOARCH=amd64
set GOBIN=
set GOCHAR=6
set GOEXE=.exe
set GOHOSTARCH=amd64
set GOHOSTOS=windows
set GOOS=windows
set GOPATH=C:\Go\Projects 
set GORACE=
set GOROOT=C:\Go 
set GOTOOLDIR=C:\Go\pkg\tool\windows_amd64
set TERM=dumb
set CC=gcc
set GOGCCFLAGS=-g -O2 -m64 -mthreads
set CXX=g++
set CGO_ENABLED=1

I installed odbc using go get code.google.com/p/odbc
Ran test and it finished fine:
C:\Go\Projects\src\code.google.com\p\odbc>go test -mssrv=.\sqlexp -v -run=MS
...
--- PASS: TestMSSQLUTF16ToUTF8 (0.00 seconds)
=== RUN TestMSSQLExecStoredProcedure
--- PASS: TestMSSQLExecStoredProcedure (0.01 seconds)
PASS
ok code.google.com/p/odbc 0.574s

My Code: (copied from mssql_test.go):

package main  
import (  
    "code.google.com/p/odbc/api"  
    "database/sql"       
    "flag"  
    "fmt"  
    "os"  
    "runtime"      
    "testing"  
    "time"  
)  

func mssqlConnect() (db *sql.DB, stmtCount int, err error) {  
    .........  
    return db, db.Driver().(*Driver).Stats.StmtCount, nil  
}  
func closeDB(t *testing.T, db *sql.DB, shouldStmtCount, ignoreIfStmtCount int) {  
    s := db.Driver().(*Driver).Stats  
    ......  

答案1

得分: 2

.\main.go:5: 导入但未使用: "code.google.com/p/odbc/api"
.\main.go:72: 未定义: Driver
.\main.go:76: 未定义: Driver

删除 import

    "code.google.com/p/odbc/api"

添加 import

    "code.google.com/p/odbc"

*Driver 替换为 *odbc.Driver。例如,

    return db, db.Driver().(*odbc.Driver).Stats.StmtCount, nil

    s := db.Driver().(*odbc.Driver).Stats

./main.go:5: 导入但未使用: "github.com/denisenkom/go-mssqldb" as mssql

import 改为

    _ "github.com/denisenkom/go-mssqldb"

例如,

package main

import (
	"database/sql"
	_ "github.com/denisenkom/go-mssqldb"
)

func main() {
	conn, err := sql.Open("mssql", makeConnStr())
}

Go编程语言规范

导入声明

如果仅为了其副作用(初始化)而导入一个包,请使用空白标识符作为显式包名:

import _ "lib/math"
英文:
.\main.go:5: imported and not used: "code.google.com/p/odbc/api"  
.\main.go:72: undefined: Driver  
.\main.go:76: undefined: Driver

Delete import

    "code.google.com/p/odbc/api"

Add import

    "code.google.com/p/odbc" 

Replace *Driver with *odbc.Driver. For example,

    return db, db.Driver().(*odbc.Driver).Stats.StmtCount, nil

and

    s := db.Driver().(*odbc.Driver).Stats

./main.go:5: imported and not used: "github.com/denisenkom/go-mssqldb" as mssql

Change import to

    _ "github.com/denisenkom/go-mssqldb"

For example,

package main

import (
	"database/sql"
	_ "github.com/denisenkom/go-mssqldb"
)

func main() {
	conn, err := sql.Open("mssql", makeConnStr())
}

> The Go Programming Language Specification
>
> Import declarations
>
> To import a package solely for its side-effects (initialization), use
> the blank identifier as explicit package name:
>
> import _ "lib/math"

huangapple
  • 本文由 发表于 2014年6月24日 09:59:11
  • 转载请务必保留本文链接:https://go.coder-hub.com/24377250.html
匿名

发表评论

匿名网友

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

确定