Not able to insert emojis in mysql database - mysql

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?

Related

How to store emoji in MySQL database table

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;

Emoji's in mysql turns to question marks

I am trying to insert emoji's into mysql but it turns to question marks, I have changed mysql connection server collation, database collation , table collation and column collation. I used these to change the items
# For each database:
ALTER DATABASE database_name CHARACTER SET = utf8mb4 COLLATE = utf8mb4_unicode_ci;
# For each table:
ALTER TABLE table_name CONVERT TO CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
# For each column:
ALTER TABLE table_name CHANGE column_name column_name VARCHAR(191) CHARACTER SET utf8mb4 COLLATE utf8mb4_unicode_ci;
I have done all these but emoji's in mysql still show question marks. Please what should I do to make mysql show the emojis. Thanks in advance
Little late to answer the question. But I hope it will be useful for others...
Above configuration makes the database tables to store utf8 encoded data. But, the database connection(JDBC) should be able to transfer the utf8 encoded data to client. For that the JDBC connection parameter charset should be set to utf8mb4.
The default encoding for inbound connections isn't set properly. DEFAULT CHARSET will return as utf8 however character_set_server will be something different.
So, Set default-character-set=utf8.

ALTER DATABASE to change COLLATE not working

I am using Django on Bluehost. I created a form for user generated input, but unicode inputs from this form fails to be stored or displayed of characters. So I did a SO and google search that I should change the Collate and Character set of my database. I run this sql
ALTER DATABASE learncon_pywithyou CHARACTER SET utf8 COLLATE utf8_unicode_ci;
from python27 manage.py dbshell, which initiated a mysql shell, what shows on screen is
Query OK, 1 row affected (0.00 sec).
So I assume the problem is solved, but it is not actually. This sql has not done anything, as I later find it in phpMyAdmin provided by Bluehost. All the Varchar fields of all the tables are still in lantin1_swedish_ci collate.
So assume that alter table should work instead. I run this on mysql
alter table mytable character set utf8 collate utf8_unicode_ci;
although on screen it shows Query OK. 4 rows affected, it actually did nothing either, the collate of those fields in mytable did not change at all.
So I finally manually change the fields in phpMyAdmin for mytable and this works, now I am able to insert in this table with unicode and also they display correctly, but I have around 20 tables of such, I don't want to change them one by one manually.
Do we at all have a simple and effective way of changing Collate of each field to store and display correct unicodes?
Changing collation at the database level sets the default for new objects - existing collations will not be changed.
Similarly, at a table level, only new columns (See comment at the bottom) are affected with this:
alter table mytable character set utf8 collate utf8_unicode_ci;
However, to convert the collation of existing columns, you need to add convert to:
alter table mytable convert to character set utf8 collate utf8_unicode_ci;
In addition to #StuartLC ,
For Changing All 20 tables charset and collation use below query, Here world is database name
SELECT
CONCAT("ALTER TABLE ",TABLE_SCHEMA , ".",TABLE_NAME," CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci") AS AlterSQL
FROM information_schema.TABLES
WHERE TABLE_SCHEMA = "world";
The above will prepare all ALTER queries which you need to run.

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

Running common "alter table" command on mysql returns "no database selected" error

I'm trying to fix some Wordpress character encoding problems by going through and altering all tables to use utf8. But the command I'm using doesn't work.
The command:
ALTER TABLE tbl_name CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
The error:
ERROR 1046 (3D000): No database selected
This seems to work for everyone. What am I doing wrong?
you need to specify the database that contains the table you are altering. you can do this in two ways:
1)
run this command before your alter table commang:
USE `database_name`;
ALTER TABLE `tbl_name` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;
or 2)
ALTER TABLE `database_name`.`tbl_name` CONVERT TO CHARACTER SET utf8 COLLATE utf8_general_ci;