html显示数据库的数据类型,将HTML插入mysql数据库,显示E类型

将HTML写入数据库时常用的解决方案:

1)只需将数据库字段类型转换为blob,以便它接受二进制数据,然后将HTML编码为二进制(示例如下)。

2) 将数据库字段保留为文本字段,但base64对数据进行编码,这样数据库就不会抱怨非法字符。在# Example for case 1.

# Note that you need to make sure the database field is a blob:

html = '

Welcome!

\n

Some text

'

bin = html.encode()

dbhandle.execute('INSERT INTO script (various fields, binhtml) VALUES (..., bin)')

# When you read back the data, remember to decode.

dbhandle.execute('SELECT binhtml FROM script WHERE...')

resultset = dbhandle.fetchall()

htmlresult = resultset.decode()

# Example for case 2.

# Database field can be a text/varchar type because base64 ensures it will work.

import base64

html = '

Welcome!

\n

Some text

'

# Convert HTML into base64 encoded *text* so it can be stored in text field.

encoded = base64.b64decode(html.encode()).decode()

# Do the database INSERT.

...

# Retrieve the stored text from the database and convert back to HTML

dbhandle.execute('SELECT encodedhtml FROM script WHERE...')

resultset = dbhandle.fetchall()

htmlresult = base64.b64decode(resultset).decode()

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值