How to store emoji in MySQL database table - mysql

I am using MySQL(5.6) database and want to store emoji in the database table. When I try to insert any emoji I am not getting any error but instead of emoji one question mark(?) is getting inserted. The data type of column is longtext. Below are the character set at column and table level:
Table - CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Column - charset utf8mb4;

Related

Convert database to utf8

Hello I'm trying to convert my database, one table and field to utf using this script
-- Write a script that converts hbtn_0c_0 database to UTF8
-- (utf8mb4, collate utf8mb4_unicode_ci) in your MySQL server.
-- You need to convert all of the following to UTF8:
-- Database hbtn_0c_0
-- Table first_table
-- Field name in first_table
ALTER DATABASE
`hbtn_0c_0`
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
USE `hbtn_0c_0`;
ALTER TABLE
`first_table`
CONVERT TO CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
ALTER TABLE
`first_table`
CHANGE `name`
VARCHAR(256)
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
But I have a SQL error. Please help me
black_genius#genius:~/Documents/ALX_Task/alx-higher_level_programming/0x0D-SQL_introduction$ cat 100-move_to_utf8.sql | mysql -hlocalhost -uroot -p
Enter password:
ERROR 1064 (42000) at line 22: You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near 'VARCHAR(256)
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci' at line 4
I'm using mysql version v8.0.31 on ubuntu 22.10
When you change a column, you need to provide the old name and the new name, even if the name is the same. See the syntax in the documentation:
CHANGE [COLUMN] old_col_name new_col_name column_definition
In your case it should be
ALTER TABLE
`first_table`
CHANGE `name` `name`
VARCHAR(256)
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
Demonstration: . db-fiddle.com/f/qFuvFqP5PEGbsc8F8DfiaN/0
You don't need to change the individual column if you use ALTER TABLE ... CONVERT TO CHARACTER SET .... That ALTER TABLE automatically converts all string-based columns.
The documentation describes:
To change the table default character set and all character columns
(CHAR, VARCHAR, TEXT) to a new character set, use a statement like
this:
ALTER TABLE tbl_name CONVERT TO CHARACTER SET charset_name;
The statement also changes the collation of all character columns.
Paul Spiegel's answer about the CHANGE COLUMN syntax is correct; that syntax allows you to change a column's name, so you need to specify the column name twice.
An alternative is to use MODIFY COLUMN instead of CHANGE COLUMN. This allows you to change the column type and options, including character set, but not to change the column name. So there's no need to include the column name twice.
ALTER TABLE
`first_table`
MODIFY `name`
VARCHAR(256)
CHARACTER SET utf8mb4
COLLATE utf8mb4_unicode_ci;
But again, in your example, there's no need to use either CHANGE COLUMN or MODIFY COLUMN. The character set conversion should be achieved by using the CONVERT TO CHARACTER SET action.

Incorrect string value: \'\\xC3\' for column \'description\' at row 1 in mySql

Here, In my table, I've one column name as description.
As per my error, I've tried many solutions from SO to change the collation type.
I've tried below collection
1) utf8mb4_unicode_ci
2) utf8_general_ci
Here, SHOW FULL COLUMNS FROM your_table;
Can anyone know what is the right collation for \'\\xC3\' this type of string?
To support full UTF-8 Unicode like for example emojis in your case it is the character À you should use utf8mb4 and utf8mb4_unicode_ci utf8 is outdated.
You can find a full explanation at https://mathiasbynens.be/notes/mysql-utf8mb4.
You can check the current collations of your table like this:
SHOW FULL COLUMNS FROM your_table;
I assume your description column has type TEXT otherwise you might need to change the type.
To alter the table default character set you can use:
ALTER TABLE your_table CONVERT TO CHARACTER SET utf8mb4;
But this does not change the collation of your column.
To change the collation of your column you should use:
ALTER TABLE your_table MODIFY description TEXT CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
Try this first
ALTER TABLE your_database_name.your_table CONVERT TO CHARACTER SET utf8
OR If above solution won't work then do the following after connecting to your database
SET NAMES 'utf8';
SET CHARACTER SET utf8;

Not able to insert emojis in mysql database

I was getting an error on inserting emoji through my android app in mysql db that :
java.sql.SQLException: Incorrect string value: '\xF0\x9F\x98\x8D\xF0\x9F...' for column 'question_text' at row 1
so i searched for it and found out that i had to Change the character set and collation properties of the databases, tables, and columns to use utf8mb4 instead of utf8.
Well i thought of applying to just one column first so i executed the query:
ALTER TABLE question MODIFY question_text varchar(100)
CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
But got the same error , than i thought of applying to the entire database so i executed the query:
ALTER DATABASE my_database CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
but still getting the same error . Can someone explain to me what i could be missing?

SQL converting column from Latin1 to UTF8

I am trying to convert a specific column in a table on my DB from latin1 character set with collation latin1_swedish_ci to utf8 with collation utf8_unicode_ci.
COLUMN: description, type: longtext, default not null
I tried the following commands on the column:
ALTER TABLE sample MODIFY description LONGBLOB NOT NULL ;
ALTER TABLE sample MODIFY description LONGTEXT CHARACTER SET utf8 NOT NULL COLLATE utf8_unicode_ci;
I also tried to alter the encoding WITHOUT changing to binary first. But the characters ended up being re-encoded incorrectly by the server.
And keep getting an error regarding some characters:
Error Code: 1366. Incorrect string value: '\x92t hav...' for column 'longDesc' at row 803
It seems like some of the character in my table aren't converting correctly.
How can I fix this issue?
\x92 implies that you have latin1 in the table now. The second ALTER is claiming that the bytes are in utf8 encoding. Hence, the error message.
Case 1: You need to change the LONGTEXT to utf8 because you plan to add rows with text that cannot be encoded in latin1.
For this case, ALTER TABLE sample CONVERT TO CHARACTER SET utf8; -- converts all CHAR/TEXT columns in the table.
ALTER TABLE sample MODIFY description ... CHARACTER SET utf8; -- converts the one column.
Case 2: The rest of the system is thinking utf8 and is confused by this column.
Well, I don't think it is confused. Conversions happen as needed.

Saving tweets in MySql throws "Incorrect string value: '\xF0\x9F\x92\xB2\xF0\x9F"

I am trying to save tweets into MySql db, most of the time it works fine, but when tweet's like the ones given below come,
Example 1
Example 2
I get the following exception from MySql,
java.sql.BatchUpdateException: Incorrect string value: '\xF0\x9F\x92\xB2\xF0\x9F...' for column 'twtText' at row 1
How can we handle such texts.
This works for me. Changing the character set in MySql
ALTER TABLE TableName MODIFY COLUMN ColumnName varchar(255) CHARACTER SET utf8mb4 COLLATE utf8mb4_general_ci NOT NULL;
Try changing your column's charset to the value reflecting the charset of the strings you want to insert.
Example:
ALTER TABLE database.table MODIFY COLUMN col VARCHAR(255) CHARACTER SET utf8 COLLATE utf8_general_ci NOT NULL;
Besides setting the collation on the database table / column, we also had to set on the client, in the app, the collation with:
SET NAMES 'utf8mb4';
before the actual statement .
Similar errors: Incorrect string value: '\xF0\x9F\x8E\xB6\xF0\x9F...' MySQL
I got the same issure and solveld it.
The cause of the error is that the string contains emoticons.
set your mysql column's charset to utf8mb4 and Collation to utf8mb4_general_ci
set your connection string of charset to utf8mb4 like charset=utf8mb4
ok, test it