package ms2mysql
import ( 
    "bytes" 
    "golang.org/x/text/encoding/simplifiedchinese" 
    "golang.org/x/text/transform" 
    "io/ioutil" 
) 
func Decode(s []byte) ([]byte, error) { 
    I := bytes.NewReader(s)
    O := transform.NewReader(I, simplifiedchinese.GBK.NewDecoder())
    d, e := ioutil.ReadAll(O)
    if e != nil {
        return nil, e
    }
    return d, nil
}

win7 64位下试了好几个其他方式,结果都不行,也就这个可以,记录下来

package main

import (
    "fmt"
    "reflect"
    "unsafe"

    "ms2mysql/lib"

    "github.com/go-xorm/core"
    "github.com/go-xorm/xorm"
    _ "github.com/lunny/godbc"
)

var engine *xorm.Engine

func BytesToString(b []byte) string {
    bh := (*reflect.SliceHeader)(unsafe.Pointer(&b))
    sh := reflect.StringHeader{bh.Data, bh.Len}
    return *(*string)(unsafe.Pointer(&sh))
}

func StringToBytes(s string) []byte {
    sh := (*reflect.StringHeader)(unsafe.Pointer(&s))
    bh := reflect.SliceHeader{sh.Data, sh.Len, 0}
    return *(*[]byte)(unsafe.Pointer(&bh))
}
func main() {
    var err error
    engine, err := xorm.NewEngine("odbc", "driver={SQL Server};Server=127.0.0.1;Database=test;uid=sa;pwd=123456;")
    if err != nil {
        fmt.Println(err)
        return
    }
    if err := engine.Ping(); err != nil {
        fmt.Println(err)
    }
    engine.ShowSQL(true)
    engine.Logger().SetLevel(core.LOG_DEBUG)
    //encode:=mahonia.NewEncoder("utf-8")
    sql := "SELECT * FROM \"USERINFO\";"
    rowArray, _ := engine.Query(sql)
    for _, row := range rowArray {
        for colname, colvalue := range row {
            v, _ := ms2mysql.Decode(colvalue)
            value := BytesToString(v)
            fmt.Println(colname, value)
        }
    }
}

 也许可以这样连接access文件(64位win7系统测试不成功,也不想测试了,感觉微软的东西用32位系统应该可以,这里作为记录,或许以后用得到)如下:

package main

import (
    "database/sql"
    "fmt"

    "github.com/go-ole/go-ole"
    "github.com/go-ole/go-ole/oleutil"
    _ "github.com/mattn/go-adodb"
)

func createMdb(f string) error {
    unk, err := oleutil.CreateObject("ADOX.Catalog")
    if err != nil {
        return err
    }
    cat, err := unk.QueryInterface(ole.IID_IDispatch)
    if err != nil {
        return err
    }
    _, err = oleutil.CallMethod(cat, "Create", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+f+";")
    if err != nil {
        return err
    }
    return nil
}

func main() {
    ole.CoInitialize(0)

    f := "E:\\access\\x.mdb"

    /*os.Remove(f)

    err := createMdb(f)
    if err != nil {
        fmt.Println("create mdb", err)
        return
    }*/

    db, err := sql.Open("adodb", "Provider=Microsoft.Jet.OLEDB.4.0;Data Source="+f+";")
    if err != nil {
        fmt.Println("open", err)
        return
    }
    fmt.Println(db)
    /*
        _, err = db.Exec("create table foo (id int not null primary key, name text not null, created datetime not null)")
        if err != nil {
            fmt.Println("create table", err)
            return
        }

        tx, err := db.Begin()
        if err != nil {
            fmt.Println(err)
            return
        }
        stmt, err := tx.Prepare("insert into foo(id, name, created) values(?, ?, ?)")
        if err != nil {
            fmt.Println("insert", err)
            return
        }
        defer stmt.Close()

        for i := 0; i < 100; i++ {
            _, err = stmt.Exec(i, fmt.Sprintf("xxx", i), time.Now())
            if err != nil {
                fmt.Println("exec", err)
                return
            }
        }
        tx.Commit()*/

    rows, err := db.Query("select UserID, DeviceID from UserACPrivilege")
    if err != nil {
        fmt.Println("select", err)
        return
    }
    defer rows.Close()

    for rows.Next() {
        var id int
        var name string
        err = rows.Scan(&id, &name)
        if err != nil {
            fmt.Println("scan", err)
            return
        }
        fmt.Println(id, name)
    }
}

 

posted on 2016-04-21 19:36  浊浊然  阅读(1437)  评论(0编辑  收藏  举报