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 八爪鱼爬数据为什么自己停了
    • ¥15 交替优化波束形成和ris反射角使保密速率最大化
    • ¥15 树莓派与pix飞控通信
    • ¥15 自动转发微信群信息到另外一个微信群
    • ¥15 outlook无法配置成功
    • ¥30 这是哪个作者做的宝宝起名网站
    • ¥60 版本过低apk如何修改可以兼容新的安卓系统
    • ¥25 由IPR导致的DRIVER_POWER_STATE_FAILURE蓝屏
    • ¥50 有数据,怎么建立模型求影响全要素生产率的因素
    • ¥50 有数据,怎么用matlab求全要素生产率