File size: 1,300 Bytes
50efdc6
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
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
"""
src/db/setup_sqlite.py
Kaggle SAP Dataset CSV โ†’ SQLite DB ์ดˆ๊ธฐํ™” ์Šคํฌ๋ฆฝํŠธ
"""

import sqlite3
import pandas as pd
from pathlib import Path

DB_PATH = "data/sap_erp.db"
RAW_DIR = "data/raw"

TABLE_CSV_MAP = {
    "VBAK": "vbak.csv",   # ์˜์—… ์˜ค๋” ํ—ค๋”
    "VBAP": "vbap.csv",   # ์˜์—… ์˜ค๋” ์•„์ดํ…œ
    "VBEP": "vbep.csv",   # ๋‚ฉํ’ˆ ์ผ์ •
    "VBUK": "vbuk.csv",   # ์˜ค๋” ์ƒํƒœ (ํ—ค๋”)
    "VBUP": "vbup.csv",   # ์˜ค๋” ์ƒํƒœ (์•„์ดํ…œ)
    "MAKT": "makt.csv",   # ์ž์žฌ ํ…์ŠคํŠธ
    "MARD": "mard.csv",   # ์žฌ๊ณ  ์ •๋ณด
}


def setup_db(raw_dir: str = RAW_DIR, db_path: str = DB_PATH) -> None:
    """CSV ํŒŒ์ผ์„ ์ฝ์–ด SQLite์— ํ…Œ์ด๋ธ”๋กœ ๋กœ๋“œ (๊ธฐ์กด ํ…Œ์ด๋ธ” ๋ฎ์–ด์“ฐ๊ธฐ)"""
    Path(db_path).parent.mkdir(parents=True, exist_ok=True)
    conn = sqlite3.connect(db_path)

    for table_name, csv_file in TABLE_CSV_MAP.items():
        csv_path = Path(raw_dir) / csv_file
        if not csv_path.exists():
            print(f"[SKIP] {csv_file} not found")
            continue
        df = pd.read_csv(csv_path)
        df.to_sql(table_name, conn, if_exists="replace", index=False)
        print(f"[OK] {table_name}: {len(df)} rows loaded")

    conn.close()
    print(f"[DONE] SQLite DB created: {db_path}")


if __name__ == "__main__":
    setup_db()