dbsetting.js 2.22 KB
Newer Older
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
import fs from "fs";
import mysql from "mysql";
import dotenv from "dotenv";

dotenv.config();

// DB Connection
const db = mysql.createConnection({
  host: process.env.MYSQL_HOST || "localhost",
  user: process.env.MYSQL_USER || "root",
  password: process.env.MYSQL_PASSWORD,
  database: process.env.MYSQL_DATABASE || "EUE",
});

const inputDo = (code, name) => {
  name = name.replace(/\s/g, "");
  let q = `INSERT INTO LOCDO (CODE,DONAME) VALUES (${code},'${name}');`;
  db.query(q, (err, result) => {
    if (err) {
      console.log(err);
      return;
    }
    console.log("Result : " + result);
  });
};

const inputSi = (code, name, doCode) => {
  name = name.replace(/\s/g, "");
  let q = `INSERT INTO LOCSIGUNGU (CODE,DOCODE,SGGNAME) VALUES (${code},${doCode},'${name}');`;
  db.query(q, (err, result) => {
    if (err) {
      console.log(err);
      return;
    }
    console.log("Result : " + result);
  });
};

const inputDong = (code, name, doCode, siCode) => {
  name = name.replace(/\s/g, "");
  let q = `INSERT INTO LOCINFO (CODE,DOCODE,SGGCODE,EMDNAME) VALUES (${code},${doCode},${siCode},'${name}');`;
  db.query(q, (err, result) => {
    if (err) {
      console.log(err);
      return;
    }
    console.log("Result : " + result);
  });
};

const setDB = () => {
  // DB Connect
  db.connect((err) => {
    if (err) {
      console.log(err);
      return;
    }
    console.log("connected as id" + db.threadId);
  });

  // File Read
  let originData = fs.readFileSync("data/admAddressCode.csv", "utf8");

  // Separate Data & Input Data
  let sepData = originData.split("\r\n");
  let setDoCode = new Set();
  let setSiCode = new Set();

  sepData.forEach((line) => {
    line = line.replace(/\s/g, "");
    let addr = line.split(",");

    const doCode = Number(addr[0]);
    if (!setDoCode.has(doCode)) {
      const doName = addr[1];
      inputDo(doCode, doName);
      setDoCode.add(doCode);
    }

    const siCode = Number(addr[2]);
    if (!setSiCode.has(siCode)) {
      const siName = addr[3];
      inputSi(siCode, siName, doCode);
      setSiCode.add(siCode);
    }

    const dongCode = Number(addr[4]);
    const dongName = addr[5];
    inputDong(dongCode, dongName, doCode, siCode);
  });

  // Connection Close
  db.end();
};

setDB();