-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathload_data.py
More file actions
38 lines (27 loc) · 883 Bytes
/
Copy pathload_data.py
File metadata and controls
38 lines (27 loc) · 883 Bytes
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
import sqlite3
from pathlib import Path
import pandas as pd
CSV_PATH = Path(__file__).parent / "SampleSuperstore.csv"
DB_PATH = Path(__file__).parent / "superstore.db"
TABLE_NAME = "sales"
def main():
df = pd.read_csv(CSV_PATH)
conn = sqlite3.connect(DB_PATH)
df.to_sql(TABLE_NAME, conn, if_exists="replace", index=False)
print("=== Nama Kolom ===")
print(list(df.columns))
print("\n=== 5 Baris Pertama ===")
print(df.head())
print("\n=== Total Sales per Region ===")
query = f"""
SELECT Region, SUM(Sales) AS Total_Sales
FROM {TABLE_NAME}
GROUP BY Region
ORDER BY Total_Sales DESC
"""
result = pd.read_sql_query(query, conn)
print(result.to_string(index=False))
conn.close()
print(f"\nData berhasil disimpan ke {DB_PATH} (tabel: {TABLE_NAME})")
if __name__ == "__main__":
main()