我正在使用 mysql 表来存储货币符号 unicode 但是当我将它返回到我的 dto 中的 android 应用程序时,它显示相同的 unicode 字符串(\u20B9)而不是货币符号(₹).
早些时候,我使用它进行硬编码,如 dto.setCurrencyCode("\u20B9")
,它工作正常,但如果我从 mysql 表中获取它并在 dto 中设置,它就无法工作。
mysql 表字符集是这样的-
+--------------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| Field | Type | Collation | Null | Key | Default | Extra | Privileges | Comment |
+--------------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
| id | bigint(20) | NULL | NO | PRI | NULL | auto_increment | select,insert,update,references | |
| apiUrl | varchar(255) | utf8_general_ci | YES | | NULL | | select,insert,update,references | |
| countryCode | varchar(255) | utf8_general_ci | YES | | NULL | | select,insert,update,references | |
| currencyCode | varchar(255) | utf8_general_ci | YES | | NULL | | select,insert,update,references | |
| locale | varchar(255) | utf8_general_ci | YES | | NULL | | select,insert,update,references | |
| mcc | int(11) | NULL | NO | MUL | NULL | | select,insert,update,references | |
| msisdnLength | int(11) | NULL | NO | | NULL | | select,insert,update,references | |
+--------------+--------------+-----------------+------+-----+---------+----------------+---------------------------------+---------+
表格条目就像-
mysql> select * from MccDetails;
+----+-------------------------------------+-------------+--------------+--------+-----+--------------+
| id | apiUrl | countryCode | currencyCode | locale | mcc | msisdnLength |
+----+-------------------------------------+-------------+--------------+--------+-----+--------------+
| 1 | https://localhost:8080/api/ | +91 | \u20B9 | en | 404 | 10 |
| 2 | https://localhost:8080/api/ | +91 | \u20B9 | en | 405 | 10 |
+----+-------------------------------------+-------------+--------------+--------+-----+--------------+
这是我的连接字符串 -jdbc:mysql://127.0.0.1:3306/dbname?autoReconnect=true&useUni code=true&connection Collation=utf8_gener al_ci&characterSetRe sults=utf8
请告诉我 currencyCode
列的数据类型应该是什么以及如何在表中存储 unicode 值 (\u20B9) 或者在客户端服务器架构中处理货币符号的最佳方式是什么.
它显然存储为字符串而不是字符。
你所要做的就是像那样对连接字符串启用正确的编码
jdbc:mysql:///dbname?useUnicode=true&characterEncoding=utf-8"
当您执行此操作时,具有 ₹ 等字符的字符串将正确存储到数据库中。您不必再使用\u20B9 代码
Tôi là một lập trình viên xuất sắc, rất giỏi!