当前位置:网站首页> MySql中的longtext字段的返回问题及解决
MySql中的longtext字段的返回问题及解决
2022-07-05 18:57:00 【1024问】
MySql中longtext字段的返回
如下图所示
解决方法
Mysql中Text字段的范围
汉字在utf8mb4中占用几个字符
MySql中longtext字段的返回最近开发中用到了longtext这种字段。在mysql中该字段的最大长度为4G
如下图所示

开发中遇到的一个问题就是。例如有个article表,然后我们的页面要将数据以列表的形式展示到前端(只显示几个字段,如作者,标题等等,例如放到table中显示多条记录),但是是将该表中的所有信息都查出来,然后当用户点击某条记录的时候,会跳到详情页,在显示出详细的信息。
这样当数据量比较多的时候,或者文本的内容比较大的时候,就出现问题了。
打开页面,页面就会一直加载,数据量越大,加载时间就越长,然后才会显示数据列表。这会严重影响使用效果。
解决方法当然是sql语句的问题了,当像上面这样查询整个列表的时候,可以不查询longtext这个字段,将其他的字段查询出来。然后当用户点击某条数据时,再根据该条数据的id到数据库去单查这条数据,这时再将longtext给查出来即可。
说到这里,还要说一种情况,就是有时候从数据库中查到的数据封装到实体类中,怎么也取不到某个字段的值,就是null。这个时候要看看sql语句,返回的结果集中是否将该字段封装并且映射到该类对应的字段上。
一般情况下都是结果集中没有封装该字段的原因。
Mysql中Text字段的范围text:存储可变长度的非Unicode数据,最大长度为2^31-1个字符。
text列不能有默认值,存储或检索过程中,不存在大小写转换,后面如果指定长度,不会报错误,但是这个长度是不起作用的,意思就是你插入数据的时候,超过你指定的长度还是可以正常插入。
mysql中text 最大长度为65,535(2的16次方–1)字符的TEXT列。
如果你觉得text长度不够,可以选择
MEDIUMTEXT最大长度为16,777,215。
LONGTEXT最大长度为4,294,967,295
Text主要是用来存放非二进制的文本,如论坛帖子,题目,或者百度知道的问题和回答之类。
需要弄清楚的是text 和 char varchar blob这几种类型的区别。
mysql 修改 text字段长度_mysql的text字段长度?mysql数据库中text字段长度不够的问题…
类型是可变长度的字符串,最多65535个字符;
可以把字段类型改成MEDIUMTEXT(最多存放16777215个字符)或者LONGTEXT(最多存放4294967295个字符).
汉字在utf8mb4中占用几个字符MySQL supports 4 TEXT field types (TINYTEXT, TEXT, MEDIUMTEXT and LONGTEXT) and this post looks at the maximum length of each of these field types.
MyISAM tables in MySQL have a maximum size of a row of 65,535 bytes, so all the data in a row must fit within that limit. However, the TEXT types are stored outside the table itself and only contribute 9 to 12 bytes towards that limit. (For more information about this refer to the MySQL Manual - Data Storage Requirements chapter). TEXT data types are also able to store much more data than VARCHAR and CHAR text types so TEXT types are what you need to use when storing web page or similar content in a database. The maximum amount of data that can be stored in each data type is as follows: TINYTEXT256 bytes
TEXT65,535 bytes~64kb
MEDIUMTEXT 16,777,215 bytes~16MB
LONGTEXT4,294,967,295 bytes~4GB
In most circumstances the TEXT type is probably sufficient, but if you are coding a content management system it’s probably best to use the MEDIUMTEXT type for longer pages to ensure there are no issues with data size limits.
答:3个
MySQL官方手册中对于utf8mb4的解释是

现有数据库版本默认的utf8都为utf8mb3,注意其中所说的"相同的长度"。
所以在utf8mb4下,英文占用1个字节,一般汉字占3个字节,emoji表情占4个字节。
关于代码插入emoji表情报错,请检查 表编码 和 数据库连接 编码是否同时为utf8mb4。
结论:数据库和表都设置为utf8mb4。
以上为个人经验,希望能给大家一个参考,也希望大家多多支持软件开发网。
边栏推荐
- 自动化测试的好处
- Low code practice of xtransfer, a cross-border payment platform: how to integrate with other medium-sized platforms is the core
- What is text mining? "Suggested collection"
- 详解SQL中Groupings Sets 语句的功能和底层实现逻辑
- You can have both fish and bear's paw! Sky wing cloud elastic bare metal is attractive!
- 国内低代码开发平台靠谱的都有哪些?
- C language makes it easy to add, delete, modify and check the linked list "suggested collection"
- Emqx 5.0 officially released: a single cluster supports 100million mqtt connections
- 面试官:Redis 过期删除策略和内存淘汰策略有什么区别?
- 中文版Postman?功能真心强大!
猜你喜欢

Tianyi cloud understands enterprise level data security in this way

The basic grammatical structure of C language

Advanced application of C # language

Interviewer: what is the difference between redis expiration deletion strategy and memory obsolescence strategy?

Windows Oracle open remote connection Windows Server Oracle open remote connection

为什么 BI 软件都搞不定关联分析?带你分析分析

企业数字化转型之路,从这里开始

公司破产后,黑石们来了

14、用户、组和权限(14)

Shang Silicon Valley Shang preferred project tutorial release
随机推荐
从外卖点单浅谈伪需求
彻底理解为什么网络 I/O 会被阻塞?
图扑软件数字孪生 | 基于 BIM 技术的可视化管理系统
Word finds red text word finds color font word finds highlighted formatted text
开源 SPL 消灭数以万计的数据库中间表
JS solution force deduction daily question (12) - 556 Next larger element III (2022-7-3)
中文版Postman?功能真心强大!
Is the performance evaluation of suppliers in the fastener industry cumbersome? Choose the right tool to easily counter attack!
2022 Alibaba Android advanced interview questions sharing, 2022 Alibaba hand Taobao Android interview questions
企业数字化转型之路,从这里开始
Go语言 | 03 数组、指针、切片用法
Summary of six points of MySQL optimization
2022 the most complete Tencent background automation testing and continuous deployment practice in the whole network [10000 words]
ROS installation error sudo: rosdep: command not found
Startup and shutdown of CDB instances
2022最新大厂Android面试真题解析,Android开发必会技术
5年经验Android程序员面试27天,2022程序员进阶宝典
自动化测试的好处
Decision tree and random forest
华为让出的高端市场,小米12S靠徕卡能抢到吗?