Skip to content

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit f925ba6

Browse filesBrowse files
committed
Prepare for more exercises
1 parent 775f5fe commit f925ba6
Copy full SHA for f925ba6

File tree

3 files changed

+84
-63
lines changed
Filter options

3 files changed

+84
-63
lines changed

‎export_data.py

Copy file name to clipboardExpand all lines: export_data.py
+32-31Lines changed: 32 additions & 31 deletions
Original file line numberDiff line numberDiff line change
@@ -3,36 +3,37 @@
33
import mysql.connector
44
from openpyxl import Workbook
55

6-
# Database
7-
# - เชื่อมต่อ Database (เปลี่ยนค่า Connection เป็นของเครื่องตัวเองเน่อ)
8-
db = mysql.connector.connect(
9-
host="localhost",
10-
port=3306,
11-
user="root",
12-
password="password1234",
13-
database='golf_want_to_buy'
14-
)
6+
def run():
7+
# Database
8+
# - เชื่อมต่อ Database (เปลี่ยนค่า Connection เป็นของเครื่องตัวเองเน่อ)
9+
db = mysql.connector.connect(
10+
host="localhost",
11+
port=3306,
12+
user="root",
13+
password="password1234",
14+
database='golf_want_to_buy'
15+
)
1516

16-
# - ส่งคำสั่ง SQL ไปให้ MySQL ทำการโหลดข้อมูล
17-
# - Python จะรับข้อมูลทั้งหมดมาเป็น List ผ่านคำสั่ง fetchall()
18-
cursor = db.cursor()
19-
sql = '''
20-
SELECT *
21-
FROM products;
22-
'''
23-
cursor.execute(sql)
24-
products = cursor.fetchall()
17+
# - ส่งคำสั่ง SQL ไปให้ MySQL ทำการโหลดข้อมูล
18+
# - Python จะรับข้อมูลทั้งหมดมาเป็น List ผ่านคำสั่ง fetchall()
19+
cursor = db.cursor()
20+
sql = '''
21+
SELECT *
22+
FROM products;
23+
'''
24+
cursor.execute(sql)
25+
products = cursor.fetchall()
2526

26-
# Excel
27-
# - สร้างไฟล์ใหม่ สร้างชีท และใส่แถวสำหรับเป็นหัวข้อตาราง
28-
workbook = Workbook()
29-
sheet = workbook.active
30-
sheet.append(['ID', 'ชื่อสินค้า', 'ราคา', 'ต้องการมากๆ', 'วันที่บันทึก'])
31-
32-
# - ใส่ข้อมูลทีละอัน เพิ่มลงไปทีละแถว
33-
for p in products:
34-
print(p)
35-
sheet.append(p)
36-
37-
# - Export ไฟล์ Excel
38-
workbook.save(filename="exported.xlsx")
27+
# Excel
28+
# - สร้างไฟล์ใหม่ สร้างชีท และใส่แถวสำหรับเป็นหัวข้อตาราง
29+
# workbook = Workbook()
30+
# sheet = workbook.active
31+
# sheet.append(['ID', 'ชื่อสินค้า', 'ราคา', 'ต้องการมากๆ', 'วันที่บันทึก'])
32+
#
33+
# # - ใส่ข้อมูลทีละอัน เพิ่มลงไปทีละแถว
34+
# for p in products:
35+
# print(p)
36+
# sheet.append(p)
37+
#
38+
# # - Export ไฟล์ Excel
39+
# workbook.save(filename="exported.xlsx")

‎import_data.py

Copy file name to clipboardExpand all lines: import_data.py
+33-32Lines changed: 33 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -4,37 +4,38 @@
44
import mysql.connector
55
from openpyxl import load_workbook
66

7-
# Excel
8-
# - โหลดไฟล์ และโหลดชีทที่เปิดอยู่
9-
workbook = load_workbook('imported.xlsx')
10-
sheet = workbook.active
7+
def run():
8+
# Excel
9+
# - โหลดไฟล์ และโหลดชีทที่เปิดอยู่
10+
workbook = load_workbook('imported.xlsx')
11+
sheet = workbook.active
1112

12-
# - เก็บข้อมูล (values_only คือ แบบดิบๆ) ทีละแถวไว้ใน List
13-
# - เริ่มต้นจากแถวที่ 2 ไปจนถึงแถวสุดท้าย
14-
values = []
15-
for row in sheet.iter_rows(min_row=2, values_only=True):
16-
print(row)
17-
values.append(row)
13+
# - เก็บข้อมูล (values_only คือ แบบดิบๆ) ทีละแถวไว้ใน List
14+
# - เริ่มต้นจากแถวที่ 2 ไปจนถึงแถวสุดท้าย
15+
values = []
16+
for row in sheet.iter_rows(min_row=2, values_only=True):
17+
print(row)
18+
values.append(row)
1819

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

‎main.py

Copy file name to clipboard
+19Lines changed: 19 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,19 @@
1+
import os
2+
from importlib import import_module
3+
4+
def main():
5+
print('พิมพ์ชื่อไฟล์เพื่อสั่งการทำงาน (ตัวอย่าง : "import_data_01", "export_data_02")')
6+
file_name = input('ชื่อไฟล์ : ')
7+
files = [f.replace('.py', '') for f in os.listdir(os.curdir) if 'import_' in f or 'export_' in f]
8+
9+
if file_name not in files:
10+
print('ไม่พบไฟล์ที่ระบุ')
11+
return
12+
13+
print('--- "' + file_name + '" กำลังทำงาน ---')
14+
module = import_module(file_name)
15+
module.run()
16+
print('--- "' + file_name + '" ทำงานเสร็จสิ้น ---')
17+
18+
if __name__ == '__main__':
19+
main()

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.