0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

🐬【MySQL】外字を含むレコードのINSERT方法

Posted at

やりたいこと

今時外字を扱うなんてと言われたらそうでもあるが扱う必要が出てきた場合
sqlファイルにてSQLを書いて実行することで外字の文字列をINSERTできる

やったこと

hogeデータベースにテストテーブルであるgaiji_test_tableを作成

create_table.sql
CREATE TABLE `gaiji_test_table` (
  `id` int(11) NOT NULL AUTO_INCREMENT,
  `gaiji` varchar(100) NOT NULL,
  PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8mb4

🔵ファイルからSQLの実行

SQLファイルを作成し任意のディレクトリに格納
(今回は/test/test/gaiji.sql)

gaiji_insert.sql
INSERT INTO gaiji_test_table (gaiji) VALUES ('');

[root@localhost ~]# mysql hoge < /test/test/gaiji.sql 
mysql> select * from gaiji_test_table;
+----+-------+
| id | gaiji |
+----+-------+
|  1 |      |
+----+-------+
1 row in set (0.00 sec)

❌ターミナルでそのまま実行した場合

外字が消えてしまう

mysql> INSERT INTO gaiji_test_table (gaiji) VALUES ('');
Query OK, 1 row affected (0.02 sec)

mysql> select * from gaiji_test_table;
+----+-------+
| id | gaiji |
+----+-------+
|  1 |      |
|  2 |       |
+----+-------+
2 rows in set (0.00 sec)
0
0
0

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?