2018年2月7日 星期三

docker mysql utf8

Ref:https://github.com/docker-library/docs/blob/93eaf4f47c52c4b65d1cd31a5dcaa257848e5f13/mysql/README.md#configuration-without-a-cnf-file

ASUS X450J / Windows 10 /  Docker version 17.12.0-ce, build c97c6d6

1. Excel file save quiz1.csv and re-save as utf-8
cols.
id, Ans, QS, A1, A2, A3, A4, IM1, OK1
2.
docker run --name mysql2 -e MYSQL_ROOT_PASSWORD=password -d mysql --character-set-server=utf8mb4 --collation-server=utf8mb4_unicode_ci
3. copy quiz1.csv into container
docker cp quiz1.csv acc:/var/lib/mysql-files/quiz1.csv
4. Chrome MySQL Admin
5.
CREATE DATABASE quizs;
use quizs;
6.
CREATE TABLE quiz1 (id INT);
ALTER TABLE quiz1 ADD COLUMN Ans INT;
ALTER TABLE quiz1 ADD COLUMN QS VARCHAR(512);
ALTER TABLE quiz1 ADD COLUMN A1 VARCHAR(256);
ALTER TABLE quiz1 ADD COLUMN A2 VARCHAR(256);
ALTER TABLE quiz1 ADD COLUMN A3 VARCHAR(256);
ALTER TABLE quiz1 ADD COLUMN A4 VARCHAR(256);
ALTER TABLE quiz1 ADD COLUMN IM1 VARCHAR(256);
ALTER TABLE quiz1 ADD COLUMN OK1 VARCHAR(256);
7. Import data
LOAD DATA INFILE '/var/lib/mysql-files/quiz1.csv'
INTO TABLE quiz1
FIELDS TERMINATED BY ','
ENCLOSED BY '"'
LINES TERMINATED BY '\n'
IGNORE 1 ROWS;
8. C# code
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;

namespace newMySQL
{
    using System.Data;
    using MySql.Data.MySqlClient;
    class Program
    {
        static void Main(string[] args)
        {
            string 連線字串 = "Server=localhost;Database=quizs;Uid=root;pwd=password;";
            MySqlConnection 連線物件 = new MySqlConnection(連線字串);
            DataTable 資料表 = new DataTable();
            string SQL = "Select * From quiz1";
            連線物件.Open();
            MySqlDataAdapter 配接器 = new MySqlDataAdapter(SQL, 連線物件);
            配接器.Fill(資料表);
            連線物件.Close();
            //Console.WriteLine(資料表.Rows.Count);
            for (int i=0; i<資料表.Rows.Count; i++)
            {
                for (int j=0; j<8; j++)
                Console.Write(資料表.Rows[i].ItemArray[j].ToString());

                Console.WriteLine();
            }
            Console.ReadLine();
        }
    }
}
9. Results



沒有留言:

張貼留言