Skip to content

Commit 07968e9

Browse files
committed
MySQL 2 import example
1 parent 506b3ac commit 07968e9

File tree

2 files changed

+100
-0
lines changed

2 files changed

+100
-0
lines changed

files/imported_04.xlsx

5.78 KB
Binary file not shown.

scripts/import_data_04.py

Lines changed: 100 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,100 @@
1+
# Import ข้อมูลจากไฟล์ Excel (.xlsx) เข้าสู่ Database MySQL
2+
# เป็นการ Import ข้อมูลทุกแถวมาใส่ใน products
3+
# แต่จะเอาข้อมูลประเภทสินค้า แยกไปใส่ใน categories (ถ้ายังไม่มี)
4+
# และที่สำคัญ ต้องเชื่อม products และ categories เข้ากันให้เรียบร้อย
5+
6+
import mysql.connector
7+
from openpyxl import load_workbook
8+
9+
def run():
10+
# Database
11+
# - เชื่อมต่อ Database (เปลี่ยนค่า Connection เป็นของเครื่องตัวเองเน่อ)
12+
db = mysql.connector.connect(
13+
host="localhost",
14+
port=3306,
15+
user="root",
16+
password="password1234",
17+
database='golf_want_to_buy_completed'
18+
)
19+
cursor = db.cursor()
20+
21+
# Database
22+
# - โหลดข้อมูล categories
23+
sql = '''
24+
SELECT *
25+
FROM categories;
26+
'''
27+
cursor.execute(sql)
28+
categories = cursor.fetchall()
29+
30+
# Excel
31+
# - โหลดไฟล์ และโหลดชีทที่เปิดอยู่
32+
workbook = load_workbook('./files/imported_04.xlsx')
33+
sheet = workbook.active
34+
35+
# - เอา Category ใหม่มาใส่ใน List
36+
categories_values = []
37+
for row in sheet.iter_rows(min_row=2, values_only=True):
38+
is_new = True
39+
category = row[3]
40+
41+
for c in categories:
42+
if category == c[1]:
43+
is_new = False
44+
break
45+
46+
if is_new:
47+
print((category,))
48+
categories_values.append((category,))
49+
50+
# Database
51+
# - เพิ่มข้อมูลประเภทสินค้า (ถ้ามีอันใหม่)
52+
if len(categories_values) > 0:
53+
sql = '''
54+
INSERT INTO categories (title)
55+
VALUES (%s);
56+
'''
57+
cursor.executemany(sql, categories_values)
58+
db.commit()
59+
print('เพิ่มประเภทสินค้าจำนวน ' + str(cursor.rowcount) + ' แถว')
60+
else:
61+
print('ไม่มีประเภทสินค้าใหม่มาเพิ่ม')
62+
63+
# Database
64+
# - โหลดข้อมูลประเภทสินค้า หลังบันทึกล่าสุดไปแล้ว
65+
sql = '''
66+
SELECT *
67+
FROM categories;
68+
'''
69+
cursor.execute(sql)
70+
categories = cursor.fetchall()
71+
72+
# - ใส่ category_id ให้สินค้าแต่ละอย่าง
73+
products_values = []
74+
for row in sheet.iter_rows(min_row=2, values_only=True):
75+
category_title = row[3]
76+
category_id = 'NULL'
77+
78+
for c in categories:
79+
if category_title == c[1]:
80+
category_id = c[0]
81+
break
82+
83+
product = (row[0], row[1], row[2], category_id)
84+
print(product)
85+
products_values.append(product)
86+
87+
# Database
88+
# - เพิ่มข้อมูลสินค้า
89+
cursor = db.cursor()
90+
sql = '''
91+
INSERT INTO products (title, price, is_necessary, category_id)
92+
VALUES (%s, %s, %s, %s);
93+
'''
94+
cursor.executemany(sql, products_values)
95+
db.commit()
96+
print('เพิ่มสินค้าจำนวน ' + str(cursor.rowcount) + ' แถว')
97+
98+
# ปิดการเชื่อมต่อ Database
99+
cursor.close()
100+
db.close()

0 commit comments

Comments
 (0)