Using Chinese characters in mysql table - mysql

I'm trying to use Chinese characters in mysql table. On my first try I wrote something like 中文考试 and my table came back with 还有一个考试.
After research, it was suggested that I use the UTF-8 as my character set. I went back to my table and altered the character set with the command
ALTER TABLE posts CONVERT TO CHARACTER SET utf8;
I tried writing Chinese characters again and got the same result - ( 还有一个考试.).
Here is a copy of my table as it exist right now:
How can I get my table to accept Chinese characters?

After research, it was suggested that I use the UTF-8 as my character
set.
You need to make sure your entire chain from the connection, to the database, to the tables is all UTF8 clean. I have a detailed answer to a similar question here.
But in your case, check the actual MySQL server my.cnf file. The following would set the whole chain to UTF-8:
[client]
default-character-set=utf8
[mysql]
default-character-set=utf8
[mysqld]
collation-server = utf8_unicode_ci
init-connect='SET NAMES utf8'
character-set-server = utf8
The my.cnf is the main MySQL configuration file. Depending on your OS it can be located in a few places. For example in Ubuntu 12.04 it can be found here:
/etc/mysql/my.cnf
So figure out where yours is located, open up that file, edit it, restart MySQL & you should be good to go.

Related

How to set default collation for mysql xampp, windows

Hi I ran into this question and could not get an answer. How can you set default, server-wide collation for your MySQL database for XAMPP+Windows.
What I have figured out. Look for my.ini file in your mysql folder, bin subfoler (e.g. E:\xampp\mysql\bin). Backup your my.ini file and open it for edit, then look for following:
character-set-server=utf8mb4
collation-server=utf8mb4_unicode_ci
Here you can change your collation to the desired default value. Save edits and restart MySQL.

I converted my database to utf-8 but my old data is still in windows-1251. How to convert it?

I want to convert my database to utf-8. What I have done up to now is set the server to read utf-8 and the database is converted by using this query:
ALTER DATABASE database_name CHARACTER SET utf8 COLLATE utf8_unicode_ci;
Now all new information is seen and the things that were broken are now fine. The problem is that the old data is seen as �. This, in my opinion, is due to the fact that the old data is written in windows-1251 (I think at least and I am not 100% sure).
I found out that I need to dump the data:
mysqldump -uroot -p database -r utf8.dump
and then import it:
mysql -uroot -p --default-character-set=utf8 database
mysql> SET names 'utf8'
mysql> SOURCE utf8.dump
This is what I saw from here: https://makandracards.com/makandra/595-dumping-and-importing-from-to-mysql-in-an-utf-8-safe-way
The problem is that I have absolutely no idea where and how to do this.
All I have access is to the web hosting control panel and I have not set up anything on my computer. Therefore, I have no idea how to connect the database to the command shell and so on. What next steps should I do to convert the data to utf-8? Please, any detailed explanation would be great due to the fact that this is the first time for me doing something like this.
// I have a Mac and a Windows machine, but not a Linux at the moment.
Thank you!
The charset and collation of the database are the default for any subsequently created tables. The table setting are the defaults for columns.
For each table, so this:
ALTER TABLE table_name CONVERT TO utf8mb4;

MySQL Accented character

Last week I migrated my db in another platform. I did a mysqldump export from phpmyadmin panel and then I imported it in a new platform using the bigdump script.
The sql dump file that I have imported was originally stored in a db that was setting in this manner:
MySQL charset: UTF-8 Unicode (utf8)
MySQL connection collation: utf8_unicode_ci
I exported the db choosing utf8 character set but if I look inside the mysqldump file for every table appears:
DEFAULT CHARSET=latin1
Now I have a problem with the accented letter (like à, è, ò, ì) that are displayed like ò , Ã...etc.
For manage my new db I use MySQL Workbench and if i prompt:
show variables like 'char%';
I see that all the values are set in utf8.
How can I solve the problem? I'm not a Mysql and db expert!
You can try changing the current character set of a table to the original:
alter table TABLE_NAME convert to character set utf8 collate utf8_unicode_ci;
Your tables seem to use latin1, despite the utf8 settings you mentioned. So you have several options here:
Take the created dump as is and send it to the server using the latin1 connection charset (not utf8). This will however create the tables with latin1 charset as they are on the source server.
Convert the dump to utf-8 if you have a tool that can do it. You have to change the charset settings for your tables in the script too, though.
Convert your tables to utf8 and do the dump again.
Combine 1 + 3, but convert your target tables instead. Useful if you cannot change the source tables.
Have you actually tried MySQL Workbench to restore the dump to your new server? I'm quite sure it should be able to handle this case with a latin1 encoded backup. See Server Administration section -> Data Import/Restore.

Telling MySQL connection to use UTF-8 with Django

I've uploaded some data to a MySQL (5.5.15 for osx10.6) database using UTF8 encoding, though for some reason I had to specify its encoding as latin1 when I LOADed it.
I reckon this part is good because when I write to an OUTFILE, my unicode 'nu' characters come out OK in a terminal and in Vim.
However, when I look at them within a MySQL session, and when I try to edit the fields from Django admin, I get mangled characters (latin1?).
So, my question is: how to I tell a MySQL client and (especially) Django to read my database as UTF-8, the way it oughta?
At the command line, I tried
--default_character_set=utf8
and also
'SET NAMES UTF8;'
at the MySQL prompt, but they do not work.
When I look at VARIABLES LIKE 'char%', they're all set to utf8 apart from character_set_server which is latin1. If I set it to utf8.... that doesn't work either.
I'd be grateful if someone could give me some pointers here, especially about how to configure Django to talk to my database properly.
Thanks!
Add in your .cnf:
[mysqld]
character-set-server = utf8
collation-server = utf8_general_ci
skip-character-set-client-handshake

Loading UTF-8 encoded dump into MySQL

I've been pulling my hear out over this problem for a few hours yesterday:
I've a database on MySQL 4.1.22 server with encoding set to "UTF-8 Unicode (utf8)" (as reported by phpMyAdmin). Tables in this database have default charset set to latin2. But, the web application (CMS Made Simple written in PHP) using it displays pages in utf8...
However screwed up this may be, it actually works. The web app displays characters correctly (mostly Czech and Polish are used).
I run: "mysqldump -u xxx -p -h yyy dbname > dump.sql". This gives me an SQL script which:
looks perfect in any editor (like Notepad+) when displaying in UTF-8 - all characters display properly
all tables in the script have default charset set to latin2
it has "/*!40101 SET NAMES latin2 */;" line at the beginning (among other settings)
Now, I want to export this database to another server running on MySQL 5.0.67, also with server encoding set to "UTF-8 Unicode (utf8)". I copied the whole CMS Made Simple installation over, copied the dump.sql script and ran "mysql -h ddd -u zzz -p dbname < dump.sql". After that, all the characters are scrambled when displaying CMSMS web pages.
I tried setting:
SET character_set_client = utf8;
SET character_set_connection = latin2;
And all combinations (just to be safe, even if it doesn't make any sense to me): latin2/utf8, latin2/latin2, utf8/utf8, etc. - doesn't help. All characters still scrambled, however sometimes in a different way :).
I also tried replacing all latin2 settings with utf8 in the script (set names and default charsets for tables). Nothing.
Are there any MySQL experts here who could explain in just a few words (I'm sure it's simple after all) how this whole encoding stuff really works? I read 9.1.4. Connection Character Sets and Collations but found nothing helpful there.
Thanks,
Matt
Did you try adding the --default-character-set=name option, like this:
mysql --default-character-set=utf8 -h ddd -u zzz -p dbname < dump.sql
I had that problem before and it worked after using that option.
Hope it helps!
Ugh... ok, seems I found a solution.
MySQL isn't the culprit here. I did a simple dump and load now, with no changes to the dump.sql script - meaning I left "set names latin2" and tables charsets as they were. Then I switched my original CMSMS installation over to the new database and... it worked correctly. So actually encoding in the database is ok, or at least it works fine with CMSMS installation I had at my old hosting provider (CMSMS apparently does funny things with characters encoding).
To make it work on my new hosting provider, I actually had to add this line to lib/adodb/drivers/adodb-mysql.inc.php in CMSMS installation:
mysql_query('set names latin2',$this->_connectionID);
This is a slightly modified solution from this post. You can find the exact line there as well. So it looks like mysql client configuration issue.
SOLUTION for me:
set this option in your php file, after mysql_connect (or after mysql_select_db)..
mysql_query("SET NAMES 'utf8'");