search

Home  >  Q&A  >  body text

python - mysql怎么插入字节型数据?

1.我通过python,向mysql插入类似b'\x33\x00\x78\xff'的数据。mysql的字段类型是varchar,插入之后发现,数据少了转义字符. 而且也不知道把数据重新读出来后,怎么把它从字符串变回字节类型的数据。

sql = "insert into myapp_userinfo (info) values (\"%s\")" % b'\x01\x00\x00\x00...'
xx.execute(sql)
...


2
varchar类型被我穿成blob,然后尝试继续插入,还是不行。

python读回来的结果

我希望数据能够原模原样的写进和读出来
刚接触python和mysql,这个问题弄了很久,有懂的人请回答下,谢谢!

大家讲道理大家讲道理2777 days ago1053

reply all(4)I'll reply

  • 伊谢尔伦

    伊谢尔伦2017-04-18 10:24:43

    You can try to serialize the data and store it in the database, then transfer it once when you retrieve it. Try this

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:24:43

    It has something to do with the database encoding of the host. Just change all system encodings to utf-8

    My database is encoded as follows:

    reply
    0
  • 黄舟

    黄舟2017-04-18 10:24:43

    Mysql doesn’t have a data type specifically designed to store binary? BINARY and VARBINARY types

    reply
    0
  • PHP中文网

    PHP中文网2017-04-18 10:24:43

    You can consider encrypting the password into binary through AES and then base64_encode转成字符串存储到密码字段中,读取时base64_decodedecoding it.

    reply
    0
  • Cancelreply