如何设置项目以使用odbc和mssql驱动程序?

时间:2014-06-24 01:59:11

标签: sql-server go odbc package

我试图使用odbc驱动程序并且我遇到错误:

.\main.go:5: imported and not used: "code.google.com/p/odbc/api"  
.\main.go:72: undefined: Driver  
.\main.go:76: undefined: 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  
    ......  

1 个答案:

答案 0 :(得分:2)

.\main.go:5: imported and not used: "code.google.com/p/odbc/api"  
.\main.go:72: undefined: Driver  
.\main.go:76: undefined: 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: imported and not used: "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())
}
  

The Go Programming Language Specification

     

Import declarations

     

要仅为其副作用(初始化)导入包,请使用   空白标识符作为显式包名称:

import _ "lib/math"