douliang2935 2019-09-04 08:28
浏览 288

如何查询mysql二进制数据

I have tried to query mysql database table data but am getting the byte data after trying many instances to convert the byte data to the hexadecimal values. Any help about how can i get the hexadecimal values from the database.

I created the table below as

CREATE TABLE `subscriber_profile` (
   ....
  `msin` binary(5) NOT NULL,
  `msisdn` bigint(16) NOT NULL,
  `k` binary(16) DEFAULT NULL,
  `opc` binary(16) DEFAULT NULL,
  `sqn` binary(6) DEFAULT NULL,
   ...
  PRIMARY KEY (`mcc`,`mnc`,`msin`)
) ENGINE=MyISAM AUTO_INCREMENT=3 DEFAULT CHARSET=utf8;

And the data in the database as

INSERT INTO subscriber_profile VALUES (...,358507777001/msisdn/,0x000102030405060708090A0B0C0D0E0F/k/,0xBC2BCE2A23BE2FAE32E4F1B4546004F7/opc/,...);

Am querying the table using msisdn as parameter. Also is using byte in struct field right?

type SubscriberProfile struct {
        ...
    Msisdn int    `json:"msisdn"`
    K      []byte `json:"k"`
    Opc    []byte `json:"opc"`
}

func GetPara(msisdn int) []SubscriberProfile {
    db := dbConn()
    selDB, err := db.Query("SELECT msisdn, k, opc FROM subscriber_profile WHERE msisdn=?;", msisdn)
    if err != nil {
        panic(err.Error())
    }
    av := SubscriberProfile{}
    res := []SubscriberProfile{}
    for selDB.Next() {
        var msisdn int
        var k, opc []byte
        err = selDB.Scan(&msisdn, &k, &opc)
        if err != nil {
            panic(err.Error())
        }

                av.Msisdn = msisdn
        av.K = k
        av.Opc = opc
        res = append(res, av)
    }
    return res
}

I have tried to use hex.EncodeToString(k) but could not get the right result. 

var data []SubscriberProfile
    data = GetPara(358507777001)

    fmt.Println(data)

output:
[{0 0 0 358507777001 [0 1 2 3 4 5 6 7 8 9 10 11 12 13 14 15] [188 43 206 42 35 190 47 174 50 228 241 180 84 96 4 247]}]

Am expecting an output [{0 0 0 358507777001 000102030405060708090A0B0C0D0E0F BC2BCE2A23BE2FAE32E4F1B4546004F7 0 ...}]

Any help?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 关于#Java#的问题,如何解决?
    • ¥15 加热介质是液体,换热器壳侧导热系数和总的导热系数怎么算
    • ¥15 想问一下树莓派接上显示屏后出现如图所示画面,是什么问题导致的
    • ¥100 嵌入式系统基于PIC16F882和热敏电阻的数字温度计
    • ¥15 cmd cl 0x000007b
    • ¥20 BAPI_PR_CHANGE how to add account assignment information for service line
    • ¥500 火焰左右视图、视差(基于双目相机)
    • ¥100 set_link_state
    • ¥15 虚幻5 UE美术毛发渲染
    • ¥15 CVRP 图论 物流运输优化