DB2 AS/400 使用 Go 进行查询

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

DB2 AS/400 Querying with Go

问题

我正在尝试将Golang与AS/400 DB2数据库连接,我已经在机器上安装了iSeries Access驱动程序,目前的代码如下:

package main

import (
	"odbc"
)

func main() {
	conn, _ := odbc.Connect("DSN=DSN/SCHEME;UID=USER;PWD=PASS")
	stmt, _ := conn.Prepare("SELECT * FROM SCHEME.TABLE")
	stmt.Execute()
	rows, _ := stmt.FetchAll()
	for i, row := range rows {
		println(i, row)
	}
	stmt.Close()
	conn.Close()
}

我之前使用Python和DSN-Less配置在ODBC驱动程序上成功实现了这一点,类似于以下代码:

con = pyodbc.connect('DRIVER=iSeries Access ODBC Driver;SYSTEM=DSN;UID=USR;PWD=PASS;DBQ=PRUEBA')

我尝试使用odbc、mgodbc和db2cli在Golang中实现,但总是出现无效的内存地址或空指针解引用错误。

任何帮助都将不胜感激,谢谢!

英文:

I'm trying to connect Golang with an AS/400 DB2 Database, i have iSeries Access Driver Installed on the machine, this is the code by now:

package main

import (
	"odbc"
)

func main() {
	conn, _ := odbc.Connect("DSN=DSN/SCHEME;UID=USER;PWD=PASS")
	stmt, _ := conn.Prepare("SELECT * FROM SCHEME.TABLE")
	stmt.Execute()
	rows, _ := stmt.FetchAll()
	for i, row := range rows {
		println(i, row)
	}
	stmt.Close()
	conn.Close()
}

I was able to do this using Python with a DSN-Less configuration on the ODBC driver with something like this:

con = pyodbc.connect('DRIVER=iSeries Access ODBC Driver;SYSTEM=DSN;UID=USR;PWD=PASS;DBQ=PRUEBA')

I tried on Golang with odbc, mgodbc and db2cli, always getting an invalid memory address or nil pointer dereference error.

Any help is appreciated, Thanks!

答案1

得分: 2

更新 -
bitbucket.org/phiggins/db2cli 现在已列在 https://github.com/golang/go/wiki/SQLDrivers


我们正在使用 bitbucket.org/phiggins/db2cli,它基于优秀的 code.google.com/p/odbc 库,一直表现很好!

package main

import (
    "database/sql"
    "log"
    "time"

    _ "bitbucket.org/phiggins/db2cli"
)

func main() {
    db, err := sql.Open("db2-cli", "DATABASE=testdb; HOSTNAME=db2.domain.com; PORT=1234; PROTOCOL=TCPIP; UID=user1; PWD=password1;")
    if err != nil {
        log.Fatalln(err)
    }
    defer db.Close()
    var t time.Time
    row := db.QueryRow("SELECT current date FROM sysibm.sysdummy1;")
    err = row.Scan(&t)
    if err != nil {
        log.Fatalln(err)
    }
    log.Println(t)
}
英文:

Update –
bitbucket.org/phiggins/db2cli is now listed on https://github.com/golang/go/wiki/SQLDrivers


We're using bitbucket.org/phiggins/db2cli, which is based off of the greatcode.google.com/p/odbc library, and it's been working great!

package main

import (
    "database/sql"
    "log"
    "time"

    _ "bitbucket.org/phiggins/db2cli"
)

func main() {
    db, err := sql.Open("db2-cli", "DATABASE=testdb; HOSTNAME=db2.domain.com; PORT=1234; PROTOCOL=TCPIP; UID=user1; PWD=password1;")
    if err != nil {
        log.Fatalln(err)
    }
    defer db.Close()
    var t time.Time
    row := db.QueryRow("SELECT current date FROM sysibm.sysdummy1;")
    err = row.Scan(&t)
    if err != nil {
        log.Fatalln(err)
    }
    log.Println(t)
}

答案2

得分: 1

我设法使它工作了,首先,你需要安装mgodbc Go包:

go get bitbucket.org/miquella/mgodbc

要安装此包,您必须具有带有gcc的MinGW安装(Windows),如果您使用Linux,则应默认安装了gcc。

然后,使用以下代码使其工作:

package main

import (
	_ "bitbucket.org/miquella/mgodbc"
	"fmt"
	"os"
	"database/sql"
)

var ( 
        db *sql.DB 
        checkError = func(err error, num int) { 
                if err != nil { 
                        fmt.Println(err, num) 
                        os.Exit(1)
                }
        }
) 

func main() {
        // 将DBQ的值替换为您的ODBC数据源的名称。
	db, err := sql.Open("mgodbc", "DRIVER=iSeries Access ODBC Driver;SYSTEM=HOSTNAME;UID=USER;PWD=PASS;DBQ=SCHEMA")
	checkError(err,1)
	rows, err := db.Query("SELECT * FROM TABLE")
	checkError(err,2)
	for rows.Next(){
		fmt.Println(rows)
	}
	defer rows.Close()
	defer db.Close()
}

这里唯一缺少的是,sql Go包没有将查询转换为字符串,因此在查询时会得到奇怪的代码,现在我正在寻找解决方法。

英文:

I managed to get it working, first, you need to install mgodbc go package:

go get bitbucket.org/miquella/mgodbc

To install this package you must have a working MinGW installation with gcc on your path (Windows), if you use Linux you should have gcc by installed by default.

Then, using this code i got it working:

package main

import (
	_ "bitbucket.org/miquella/mgodbc"
	"fmt"
	"os"
	"database/sql"
)

var ( 
        db *sql.DB 
        checkError = func(err error, num int) { 
                if err != nil { 
                        fmt.Println(err, num) 
                        os.Exit(1)
                }
        }
) 

func main() {
        // Replace the DBQ value with the name of your ODBC data source.
	db, err := sql.Open("mgodbc", "DRIVER=iSeries Access ODBC Driver;SYSTEM=HOSTNAME;UID=USER;PWD=PASS;DBQ=SCHEMA")
	checkError(err,1)
	rows, err := db.Query("SELECT * FROM TABLE")
	checkError(err,2)
	for rows.Next(){
		fmt.Println(rows)
	}
	defer rows.Close()
	defer db.Close()
}

The only thing missing here, is that sql Go package doesn't cast the Query to String, then you get weird codes when querying, now i'm looking to solve that.

答案3

得分: 0

我正在使用Go语言和https://bitbucket.org/phiggins/db2cli。这个Go库用于DB2,使用了DB2 CLI驱动程序,我在phiggins的页面上找到了这个驱动程序,并按照他的说明进行了配置:http://public.dhe.ibm.com/ibmdl/export/pub/software/data/db2/drivers/odbc_cli/

以下是我的代码:

package main

import (
	"fmt"
	_ "bitbucket.org/phiggins/db2cli"
	"database/sql"
	"log"
	"time"
)

func main() {

	// JDBC连接字符串 = jdbc:as400://127.0.0.1/;libraries=MYCUSTOM,hhpgm,trk35null,GX,TRK35B5ptf,TRK35B5PG,IFWMS,INTERTRK;user=MYUSER;password=MYPASS;naming=system;block criteria="0"
	db, err := sql.Open("db2-cli", "DATABASE=MYCUSTOM; HOSTNAME=127.0.0.1; UID=MYUSER; PWD=MYPASS;")
	if err != nil {
		log.Fatalln(err)
	}
	defer db.Close()
	var t time.Time
	row := db.QueryRow("SELECT current date FROM SYSIBM.SYSDUMMY1;")
	err = row.Scan(&t)
	if err != nil {
		log.Fatalln(err)
	}
	log.Println(t)
}

但是我无法连接。我收到的错误是:

$ go run main.go 
2016/10/27 18:07:46 SQLDriverConnect: {08001} [IBM][CLI Driver] SQL30081N  A communication error has been detected. Communication protocol being used: "TCP/IP".  Communication API being used: "SOCKETS".  Location where the error was detected: "127.0.0.1".  Communication function detecting the error: "connect".  Protocol specific error code(s): "61", "*", "*".  SQLSTATE=08001

exit status 1

我在Mac上,所以我确定"protocol specific error code" 61是"#define ECONNREFUSED 61 /* Connection refused */",根据这个链接:http://opensource.apple.com//source/xnu/xnu-1456.1.26/bsd/sys/errno.h

然而,我可以使用SQL客户端和JDBC驱动程序连接。所以,问题是,我如何将JDBC连接字符串转换为DB2 CLI驱动程序所期望的格式?

  • JDBC连接字符串:jdbc:as400://127.0.0.1/;libraries=MYCUSTOM,hhpgm,trk35null,GX,TRK35B5ptf,TRK35B5PG,IFWMS,INTERTRK;user=MYUSER;password=MYPASS;naming=system;block criteria="0"
  • CLI连接字符串:"HOSTNAME=127.0.0.1; UID=MYUSER; PWD=MYPASS;"
英文:

I'm using Go + https://bitbucket.org/phiggins/db2cli.
This go lib for DB2 is using the DB2 CLI driver, that I found here and configured as described on phiggins page: http://public.dhe.ibm.com/ibmdl/export/pub/software/data/db2/drivers/odbc_cli/

Here is my code:

package main

import (
	"fmt"
	_ "bitbucket.org/phiggins/db2cli"
	"database/sql"
	"log"
	"time"
)

func main() {

	// JDBC conn string = jdbc:as400://127.0.0.1/;libraries=MYCUSTOM,hhpgm,trk35null,GX,TRK35B5ptf,TRK35B5PG,IFWMS,INTERTRK;user=MYUSER;password=MYPASS;naming=system;block criteria="0"
	db, err := sql.Open("db2-cli", "DATABASE=MYCUSTOM; HOSTNAME=127.0.0.1; UID=MYUSER; PWD=MYPASS;")
	if err != nil {
		log.Fatalln(err)
	}
	defer db.Close()
	var t time.Time
	row := db.QueryRow("SELECT current date FROM SYSIBM.SYSDUMMY1;")
	err = row.Scan(&t)
	if err != nil {
		log.Fatalln(err)
	}
	log.Println(t)
}

But I'm not being able to connect.
The error that I'm receiving is:

$ go run main.go 
2016/10/27 18:07:46 SQLDriverConnect: {08001} [IBM][CLI Driver] SQL30081N  A communication error has been detected. Communication protocol being used: "TCP/IP".  Communication API being used: "SOCKETS".  Location where the error was detected: "127.0.0.1".  Communication function detecting the error: "connect".  Protocol specific error code(s): "61", "*", "*".  SQLSTATE=08001

exit status 1

I'm on mac, so I identified that the "protocol specific error code" 61 is "#define ECONNREFUSED 61 /* Connection refused */" according to:
http://opensource.apple.com//source/xnu/xnu-1456.1.26/bsd/sys/errno.h

However, I can connect using a SQL client and a JDBC driver.
So, the question is, how can I convert the JDBC connection string to the format expected by the DB2 CLI driver?

  • JDBC connection string: jdbc:as400://127.0.0.1/;libraries=MYCUSTOM,hhpgm,trk35null,GX,TRK35B5ptf,TRK35B5PG,IFWMS,INTERTRK;user=MYUSER;password=MYPASS;naming=system;block criteria="0"
  • CLI connection string: "HOSTNAME=127.0.0.1; UID=MYUSER; PWD=MYPASS;"

答案4

得分: 0

Vanessa,你可能需要为你的连接字符串定义端口的值。我不确定默认的端口值是什么,所以为了安全起见,将其放在那里。

英文:

Vanessa, you might need to define the value of the port for your connection string. I not sure what is the default port value so just put it in there to be safe.

答案5

得分: 0

你是否尝试过适用于Db2的golang驱动程序?
https://github.com/ibmdb/go_ibm_db

英文:

Have you tried the golang driver available for Db2?
https://github.com/ibmdb/go_ibm_db

huangapple
  • 本文由 发表于 2014年7月25日 00:11:03
  • 转载请务必保留本文链接:https://go.coder-hub.com/24938867.html
匿名

发表评论

匿名网友

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

确定