8000 More import exercises #1 · potchangelo/python-mysql-excel@04c2929 · GitHub
[go: up one dir, main page]

Skip to content

Commit 04c2929

Browse files
committed
More import exercises #1
1 parent f925ba6 commit 04c2929

File tree

6 files changed

+70
-24
lines changed

6 files changed

+70
-24
lines changed
File renamed without changes.

files/imported_02.xlsx

5.96 KB
Binary file not shown.

main.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,14 +4,14 @@
44
def main():
55
print('พิมพ์ชื่อไฟล์เพื่อสั่งการทำงาน (ตัวอย่าง : "import_data_01", "export_data_02")')
66
file_name = input('ชื่อไฟล์ : ')
7-
files = [f.replace('.py', '') for f in os.listdir(os.curdir) if 'import_' in f or 'export_' in f]
7+
files = [f.replace('.py', '') for f in os.listdir(os.curdir + '/scripts') if 'import_' in f or 'export_' in f]
88

99
if file_name not in files:
1010
print('ไม่พบไฟล์ที่ระบุ')
1111
return
1212

1313
print('--- "' + file_name + '" กำลังทำงาน ---')
14-
module = import_module(file_name)
14+
module = import_module('scripts.' + file_name)
1515
module.run()
1616
print('--- "' + file_name + '" ทำงานเสร็จสิ้น ---')
1717

export_data.py renamed to scripts/export_data_01.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -36,4 +36,4 @@ def run():
3636
# sheet.append(p)
3737
#
3838
# # - Export ไฟล์ Excel
39-
# workbook.save(filename="exported.xlsx")
39+
# workbook.save(filename="./files/exported_01.xlsx")

import_data.py renamed to scripts/import_data_01.py

Lines changed: 21 additions & 21 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
def run():
88
# Excel
99
# - โหลดไฟล์ และโหลดชีทที่เปิดอยู่
10-
workbook = load_workbook('imported.xlsx')
10+
workbook = load_workbook('./files/imported_01.xlsx')
1111
sheet = workbook.active
1212

1313
# - เก็บข้อมูล (values_only คือ แบบดิบๆ) ทีละแถวไว้ใน List
@@ -19,23 +19,23 @@ def run():
1919

2020
# Database
2121
# - เชื่อมต่อ Database (เปลี่ยนค่า Connection เป็นของเครื่องตัวเองเน่อ)
22-
# db = mysql.connector.connect(
23-
# host="localhost",
24-
# port=3306,
25-
# user="root",
26-
# password="password1234",
27-
# database='golf_want_to_buy'
28-
# )
29-
#
30-
# # - ส่งคำสั่ง SQL ไปให้ MySQL ทำการเพิ่มข้อมูล
31-
# # - ใช้ executemany() เพื่อเพิ่มข้อมูลหลายอัน
32-
# cursor = db.cursor()
33-
# sql = '''
34-
# INSERT INTO products (title, price, is_necessary)
35-
# VALUES (%s, %s, %s);
36-
# '''
37-
# cursor.executemany(sql, values)
38-
# db.commit()
39-
#
40-
# # - สรุปจำนวนข้อมูลที่เพิ่มไป
41-
# print('เพิ่มข้อมูลจำนวน ' + str(cursor.rowcount) + ' แถว')
22+
db = mysql.connector.connect(
23+
host="localhost",
24+
port=3306,
25+
user="root",
26+
password="password1234",
27+
database='golf_want_to_buy'
28+
)
29+
30+
# - ส่งคำสั่ง SQL ไปให้ MySQL ทำการเพิ่มข้อมูล
31+
# - ใช้ executemany() เพื่อเพิ่มข้อมูลหลายอัน
32+
cursor = db.cursor()
33+
sql = '''
34+
INSERT INTO products (title, price, is_necessary)
35+
VALUES (%s, %s, %s);
36+
'''
37+
cursor.executemany(sql, values)
38+
db.commit()
39+
40+
# - สรุปจำนวนข้อมูลที่เพิ่มไป
41+
print('เพิ่มข้อมูลจำนวน ' + str(cursor.rowcount) + ' แถว')

scripts/import_data_02.py

Lines changed: 46 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
# Import ข้อมูลเฉพาะแถวที่มีข้อมูลครบถ้วนจากไฟล์ Excel (.xlsx) เข้าสู่ Database MySQL
2+
# โดยข้อมูลจากไฟล์ Excel จะเริ่มต้นตรงแถวที่ 2
3+
4+
import mysql.connector
5+
from openpyxl import load_workbook
6+
7+
def run():
8+
# Excel
9+
# - โหลดไฟล์ และโหลดชีทที่เปิดอยู่
10+
workbook = load_workbook('./files/imported_02.xlsx')
11+
sheet = workbook.active
12+
13+
# - ต้องทำการตรวจสอบข้อมูลในแต่ละแถวก่อน
14+
# - จากนั้นค่อยเก็บข้อมูลจากแถวที่ข้อมูลครบถ้วนไว้ใน List
15+
# - เริ่มต้นจากแถวที่ 2 ไปจนถึงแถวสุดท้าย
16+
values = []
17+
for row in sheet.iter_rows(min_row=2, values_only=True):
18+
p = row[:3]
19+
if p[0] is None or p[1] is None or p[2] is None:
20+
continue
21+
22+
print(p)
23+
values.append(p)
24+
25+
# Database
26+
# - เชื่อมต่อ Database (เปลี่ยนค่า Connection เป็นของเครื่องตัวเองเน่อ)
27+
db = mysql.connector.connect(
28+
host="localhost",
29+
port=3306,
30+
user="root",
31+
password="password1234",
32+
database='golf_want_to_buy'
33+
)
34+
35+
# - ส่งคำสั่ง SQL ไปให้ MySQL ทำการเพิ่มข้อมูล
36+
# - ใช้ executemany() เพื่อเพิ่มข้อมูลหลายอัน
37+
cursor = db.cursor()
38+
sql = '''
39+
INSERT INTO products (title, price, is_necessary)
40+
VALUES (%s, %s, %s);
41+
'''
42+
cursor.executemany(sql, values)
43+
db.commit()
44+
45+
# - สรุปจำนวนข้อมูลที่เพิ่มไป
46+
print('เพิ่มข้อมูลจำนวน ' + str(cursor.rowcount) + ' แถว')

0 commit comments

Comments
 (0)
0