This is a creation in Article, where the information may have evolved or changed.
Recent project needs, need to operate access, formerly with VC + + OLE access, network with Ace Library, feel very large ... Decide to try it with go
This https://github.com/weigj/go-odbc is the most used on the Internet.
The installation method is as follows:
ODBC database driver for GOINSTALL:CD $GOPATH/srcgit clone git://github.com/weigj/go-odbc.git odbccd odbcgo Install
A lot of holes were encountered during the test.
The 1th time when you run go install,
Pit Daddy 1: Find gcc, oh, speculation cgo to link ODBC32 dll, need GCC compiler environment, thanks to the elder brother Cocos2d-x when the computer has installed a huge Cygwin, so start Cygwin Terminal, jump to ODBC directory,
The 2nd time when you run go install,
Pit Daddy 2: Prompt for access limit. But in the console directly running Gcc-v is present, the feeling is gcc this is the symbolic link of Linux, speculation go install may not use Cygwin environment settings, a bit of conflict, so delete the GCC symbol link, Renaming the real Gcc-4.exe to Gcc.exe,
The 3rd time when you run go install,
Pit Daddy 3: Hint can not find the function symbol of ODBC, your sister, have to fuck source code, read source Odbc.go, found import has a macro definition,
#ifdef __mingw32__
#include <windef.h>
#else
typedef void* HANDLE;
#endif
Originally Odbc.go compiler relies on mingw environment, so download MinGW, after installation, the MinGW Bin directory added to the system path, compiled successfully.
Go run the following example code,
package mainimport (" Database/sql "" Fmt "_" Odbc/driver ") func Main () {conn, err: = SQL. Open ("ODBC", "Driver={microsoft Access driver (*.mdb)};d Bq=d:\\test.mdb") if err! = Nil {fmt. PRINTLN ("Connecting Error") Return}defer Conn. Close () stmt, err: = conn. Prepare ("SELECT * from Test")//alter TABLE TB ALTER COLUMN AA longif Err! = Nil {fmt. Println ("Query Error") Return}defer stmt. Close () row, err: = stmt. Query () if err! = Nil {fmt. Print (Err) fmt. Println ("Query Error") Return}defer row. Close () for row. Next () {var ID stringvar sequencenumber intvar valuecode stringif Err: = row. Scan (&id, &sequencenumber, &valuecode); Err = = Nil {fmt. Println (ID, SequenceNumber, Valuecode)}}fmt. Printf ("%s\n", "Finish") return}
Pit Daddy 4: Incredibly hintFatal error:malloc/free-deadlock[signal 0xc0000005 code=0x1 addr=0x2f0 pc=0x419258]goroutine 1 [syscall]:[fp= 0X10815FC] Return () c:/users/admini~1/appdata/local/temp/2/bindist465310315/go/src/pkg/runtime/asm_386.s:472[fp= 0X1081624] Runtime.cgocall (0x470a23, 0x1081630) c:/users/admini~1/appdata/local/temp/2/bindist465310315/go/src/ pkg/runtime/cgocall.c:162 +0x10a[fp=0x1081630] Odbc._cfunc_sqlfreehandle (0x400003, 0xb115e8, 0x1081650) C:/DOCUME~1 /admini~1/locals~1/temp/go-build619492171/odbc/_obj/_cgo_defun.c:144 +0x31[fp=0x1081640] ODBC. (*statement). Free (0x2f860140) odbc.go:745 +0x32[fp=0x1081648] ODBC. (*statement). Close (0x2f860140) odbc.go:752 +0x28[fp=0x1081650] Odbc/driver. (*STMT). Close (0x2f860148, 0x2f8601c8, 0x405a2b) e:/go/src/odbc/driver/sql.go:107 +0x2a[fp=0x108168c] Database/sql. (*db). Noteunuseddriverstatement (0x2f884300, 0x2f8821e0, 0x2f882240, 0x2f860148) c:/users/admini~1/appdata/local/ temp/2/bindist465310315/go/src/pkg/database/sql/sql.go:554 +0x17a[fp=0x10816d0] Database/sql. (*Stmt). Finalclose (0x2f8862d0, 0x2f8601b8, 0x0) c:/users/admini~1/appdata/local/temp/2/bindist465310315/go/src/pkg/ database/sql/sql.go:1264 +0x84[fp=0x10816e0] Database/sql.func 002 (0x2f884310, 0x2f894720) C:/Users/ADMINI~1/ appdata/local/temp/2/bindist465310315/go/src/pkg/database/sql/sql.go:372 +0x2c[fp=0x1081700] Database/sql. (*db). REMOVEDEP (0x2f884300, 0x2f894720, 0x2f8862d0, 0x49c740, 0x2f8862d0, ...) c:/users/admini~1/appdata/local/temp/2/bindist465310315/go/src/pkg/database/sql/sql.go:351 +0x78[fp=0x1081720] Database/sql. (*STMT). Close (0x2f8862d0, 0x0, 0x0) c:/users/admini~1/appdata/local/temp/2/bindist465310315/go/src/pkg/database/sql/ sql.go:1259 +0x117[fp=0x10817c4] Main.main () e:/go/src/testaccessdb/testdb.go:12 +0XBF[FP=0X10817DC] Runtime.main () c:/users/admini~1/appdata/local/temp/2/bindist465310315/go/src/pkg/runtime/proc.c:182 +0X8E[FP=0X10817E0] Runtime.goexit () c:/users/admini~1/appdata/local/temp/2/bindist465310315/go/src/pkg/runtime/proc.c : 1223goroutine 2 [syscall]:exit Status 2
This depressed, Google has a half-day there is no solution, found Golang issue there is this problem, but the goods said the latest go version has been solved, but brother is the latest version ah, handy even submit this bug to Golang, decided to give up ...
But brother's research spirit is shining, illuminating my future, repeatedly n times, gradually delete the test code, and finally found
stmt. Query ()
This function is deleted by pass!
So carefully read the above stack spam found that the problem may be the free handle odbc._cfunc_sqlfreehandle multiple times, suspect that the Github.com/weigj/go-odbc.git library comes with a bug, not go problem, So using the already widely known artifact Kung Fu routine log Dafa, at the beginning and end of the free function plus log, go run again found that the free () function was called two times ...
So the elder brother modifies
Func (stmt *statement) free () {
C.sqlfreehandle (c.sql_handle_stmt, Stmt.handle)
}
For
Func (stmt *statement) free () {
If Stmt.handle! = Nil {
C.sqlfreehandle (c.sql_handle_stmt, Stmt.handle)
Stmt.handle = Nil
}
}
The expected test passes ... Error hint no, only shiny finish ...
Aftercare: The author has not updated two years, and the Code snippets on the web point to this library ... So my brother submitted a complete code to fix the bug, please refer to Https://github.com/philsong/golang_samples
Problem solved, feel this library is not very mature, but write tool utility can make use of.