【问题标题】:Java MYSQL byte of each char每个 char 的 Java MYSQL 字节
【发布时间】:2019-06-03 05:25:49
【问题描述】:

您好,抱歉,如果这篇文章很愚蠢,但我需要知道我是否有这样的 Java 字符串。

final String string = "myNastyString";
for(int i=0;i<string.length();i++){
    System.out.println((int)string.charAt(i));
}

我想知道 char 的 int 值或 char 本身在 MySQL 中将使用多少字节。

请多多关照,非常感谢。是的,我做了一些研究。

类似的东西。

51 3 would use X bytes in a mysqlTable{X}
32   would use X bytes in a mysqlTable{X}
67 C would use X bytes in a mysqlTable{X}
100 d would use X bytes in a mysqlTable{X}
115 s would use X bytes in a mysqlTable{X}
32   would use X bytes in a mysqlTable{X}
70 F would use X bytes in a mysqlTable{X}
114 r would use X bytes in a mysqlTable{X}
233 é would use X bytes in a mysqlTable{X}
65533 � would use X bytes in a mysqlTable{X}
68 D would use X bytes in a mysqlTable{X}
233 é would use X bytes in a mysqlTable{X}
65533 � would use X bytes in a mysqlTable{X}
82 R would use X bytes in a mysqlTable{X}
105 i would use X bytes in a mysqlTable{X}
99 c would use X bytes in a mysqlTable{X}
32   would use X bytes in a mysqlTable{X}
67 C would use X bytes in a mysqlTable{X}
104 h would use X bytes in a mysqlTable{X}
111 o would use X bytes in a mysqlTable{X}
112 p would use X bytes in a mysqlTable{X}
105 i would use X bytes in a mysqlTable{X}
110 n would use X bytes in a mysqlTable{X}
32   would use X bytes in a mysqlTable{X}
40 ( would use X bytes in a mysqlTable{X}
77 M would use X bytes in a mysqlTable{X}
97 a would use X bytes in a mysqlTable{X}
115 s would use X bytes in a mysqlTable{X}
116 t would use X bytes in a mysqlTable{X}
101 e would use X bytes in a mysqlTable{X}
114 r would use X bytes in a mysqlTable{X}
112 p would use X bytes in a mysqlTable{X}
105 i would use X bytes in a mysqlTable{X}
101 e would use X bytes in a mysqlTable{X}
99 c would use X bytes in a mysqlTable{X}
101 e would use X bytes in a mysqlTable{X}
115 s would use X bytes in a mysqlTable{X}
41 ) would use X bytes in a mysqlTable{X}

我的意思是每个值 aka char 将在 mysql 中使用多少字节,因为我正在使用 latin1_swedish_ci 排序规则我需要进行验证,以防任何字符不适合我的表

我想知道 myString 中的字符何时会在 MYSQl 表中消耗超过 1 个字节

【问题讨论】:

  • 在我看来,你只是有点令人困惑,因为你问 Java MySQL 将使用多少字节作为字符串。 MySQL 使用的字节数取决于 DB 中的字段编码
  • 嗨,请查看我编辑的问题,谢谢。

标签: java mysql


【解决方案1】:

来自 MySQL 参考:

https://dev.mysql.com/doc/refman/8.0/en/char.html

如果您声明为“CHAR”,则无论字符串长多少个字符,都使用相同的数字。 相反,您使用取决于指定长度的“VARCHAR”。

例如字符串:

  • char(13) 中的“myNastyString”使用 13 个字节
  • char(20) 中的“myNastyString”使用 20 字节
  • varchar(13) 中的“myNastyString”使用 13 个字节
  • varchar(20) 中的“myNastyString”使用 13 个字节

【讨论】:

  • 嗨 matteo 但我想知道每个字符会消耗多少字节我的意思是 m 会使用 1 y 也会使用 1 但我想知道 myString 中的字符何时会消耗超过 1 个字节
【解决方案2】:

我的意思是每个值 aka char 将在 mysql 中使用多少字节,因为我正在使用 latin1_swedish_ci 排序规则我需要进行验证,以防任何字符不适合我的表

MySQL“latin1”是windows-1252的modified version,意味着它包括windows-1252中的所有字符,还定义了windows-1252未定义的少数字符的映射:

对于 cp1252 中的“未定义”条目,MySQL 将 0x81 转换为 Unicode 0x0081,0x8d 转换为 0x008d,0x8f 转换为 0x008f,0x90 转换为 0x0090,0x9d 转换为 0x009d。

我不希望 Java 直接支持“MySQL latin1”,因为它不是标准字符集。所以对于每个字符,你可以检查它是否是

  • 在 U+0000 - U+007F (ASCII) 范围内
  • 在 U+00A0 - U+00FF 范围内(其中 cp1252 和 ISO latin-1 重合)
  • windows-1252 中的一个字符映射到 0x80 - 0x9F 范围内(参见 wikipedia page)
  • U+0081、U+008D、U+008F、U+0090 或 U+009D

【讨论】:

  • 嗨 Joni,谢谢队友,你能举一个在 latin1 中使用超过 1 个字节的任何字符的例子
  • 没有这个字符,拉丁1是单字节编码。它支持的所有 256 个字符都用一个字节编码
  • ISO-8859-1,Latin-1,因为它只有 256 个值:java 会将其他 Unicode 字符转换为占位符 ?。通常占位符是�,如上所述。也许有用:string.getBytes("Windows-1252")- 对于那些?s。
【解决方案3】:

您的数据库中每个字符的字节数不取决于您的字符串如何存储在 java 或任何其他写入您的数据库的客户端中。这取决于为您的数据库、您的表或特定列定义的字符集。一旦字符串被 DB 接收到,它就会被转换为 DB/Table/column 定义的字符集。所以只是为了回答你的问题:charset latin1 每个字符总是包含 1 个字节。 BTW latin1 更广为人知的是 ISO-8859-1,它绝对是一个非常标准的字符集,并且绝对受到 java 的支持。查看关于字符集的信息here。

我还建议切换到支持所有语言的所有字符的 Unicode 字符集。常见的是 UTF-8(可能为每个字符分配不同数量的字节(如果我没记错的话是 1-3)或 UTF-16(每个字符总是 2 个字节)。

在 Java 方面分析您的字符串并诊断一些与字符集相关的问题,我建议开源库 MgntUtils(由我编写),它有一个实用程序类 StringUnicodeEncoderDecoder。该类提供将任何 String 转换为 Unicode 序列的静态方法,反之亦然。非常简单实用。要转换字符串,您只需:

String codes = StringUnicodeEncoderDecoder.encodeStringToUnicodeSequence(myString);

例如一个字符串“Hello World”将被转换成

"\u0048\u0065\u006c\u006c\u006f\u0020 \u0057\u006f\u0072\u006c\u0064"

它适用于任何语言。这是解释有关图书馆的所有细节的文章的链接:MgntUtils。寻找副标题“字符串 Unicode 转换器”。这篇文章为您提供了 Maven Central 的链接,您可以在其中获取工件和 github,您可以在其中获取项目本身。该库附带编写良好的 javadoc 和源代码。

【讨论】:

    猜你喜欢
    • 2016-05-22
    • 2019-08-12
    • 2010-10-16
    • 2010-10-06
    • 2011-07-02
    • 1970-01-01
    • 2013-07-13
    • 1970-01-01
    • 2022-11-24
    相关资源
    最近更新 更多