How to store Euro Symbol in mysql database?

2020-02-11 02:47发布

问题:

How to store symbol for Euro currency in MySQL Database ?

回答1:

Create a column with a character set that supports the Euro character.

CREATE TABLE MyTable
(
    MyEuroColumn VARCHAR(5)
      CHARACTER SET utf8
      COLLATE utf8_general_ci 
);

If your editor doesn't support Unicde, you can insert it like;

select concat('Five ', _ucs2 0x20AC, ' please!')

You can also store the Euro symbol in other character sets:

UTF-8                  0xE282AC
UCS-16                 0x20AC
ISO-8859-15 (latin-9)  0xA4


回答2:

If the table or the column is set to use utf8 encoding, something like this works fine:

insert into test values (1, '€ <- euro');

Also, add this to your connection string in the client to make sure the connection uses UTF8:

CharSet=UTF8;

The table I used for testing follow:

CREATE TABLE  `g`.`test` (
  `id` int(11) NOT NULL auto_increment,
  `name` varchar(10) character set utf8 NOT NULL,
  PRIMARY KEY  (`id`)
) ENGINE=MyISAM AUTO_INCREMENT=2 DEFAULT CHARSET=utf8


回答3:

While I also recommend UTF-8, I wanted to add some clarification of why you may be having trouble storing the Euro symbol in other encodings.

The Euro symbol was added as a patch to existing Windows and Mac legacy encodings circa 1997. In Windows-1252, the Windows latin-1 character set, the codepoint chosen was 0x80, and MacRoman chose a different location. That's all fine, but many applications specify iso-8859-1 encoding in their MySql database Schema for Latin 1-General text, or mark their HTML output in web applications with iso-8859-1. Iso-8859-1 was never, to my knowledge, officially updated to map a codepoint for the Euro symbol. The latin1 encoding that does support the Euro is the infrequently used iso-8859-15.

So, if you are using iso-8859-1 encodings in your schema, you will have undefined, or worse, behavior.



回答4:

I don't know what your really want but there is no problem in storing it into a VARCHAR with utf-8 encoding



标签: mysql symbols