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

Skip to content

Commit a0abf05

Browse files
committed
More export exercises #1
1 parent a91b675 commit a0abf05

File tree

6 files changed

+96
-12
lines changed

6 files changed

+96
-12
lines changed

files/exported_01.xlsx

5.5 KB
Binary file not shown.

files/exported_02.xlsx

5.32 KB
Binary file not shown.

files/exported_03.xlsx

4.95 KB
Binary file not shown.

scripts/export_data_01.py

Lines changed: 13 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,5 @@
1-
# Export ข้อมูลทุกแถวจาก Database MySQL ออกมาเป็นไฟล์ Excel (.xlsx)
1+
# Export ข้อมูลจาก Database MySQL ออกมาเป็นไฟล์ Excel (.xlsx)
2+
# เป็นการ Export ข้อมูลทุกแถว
23

34
import mysql.connector
45
from openpyxl import Workbook
@@ -26,14 +27,14 @@ def run():
2627

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

scripts/export_data_02.py

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,40 @@
1+
# Export ข้อมูลจาก Database MySQL ออกมาเป็นไฟล์ Excel (.xlsx)
2+
# เป็นการ Export ข้อมูลทุกแถว แต่เอาเฉพาะข้อมูลชื่อสินค้า, ราคาสินค้า
3+
4+
import mysql.connector
5+
from openpyxl import Workbook
6+
7+
def run():
8+
# Database
9+
# - เชื่อมต่อ Database (เปลี่ยนค่า Connection เป็นของเครื่องตัวเองเน่อ)
10+
db = mysql.connector.connect(
11+
host="localhost",
12+
port=3306,
13+
user="root",
14+
password="password1234",
15+
database='golf_want_to_buy'
16+
)
17+
18+
# - ส่งคำสั่ง SQL ไปให้ MySQL ทำการโหลดข้อมูล
19+
# - Python จะรับข้อมูลทั้งหมดมาเป็น List ผ่านคำสั่ง fetchall()
20+
cursor = db.cursor()
21+
sql = '''
22+
SELECT title, price
23+
FROM products;
24+
'''
25+
cursor.execute(sql)
26+
products = cursor.fetchall()
27+
28+
# Excel
29+
# - สร้างไฟล์ใหม่ สร้างชีท และใส่แถวสำหรับเป็นหัวข้อตาราง
30+
workbook = Workbook()
31+
sheet = workbook.active
32+
sheet.append(['ชื่อสินค้า', 'ราคา'])
33+
34+
# - ใส่ข้อมูลทีละอัน เพิ่มลงไปทีละแถว
35+
for p in products:
36+
print(p)
37+
sheet.append(p)
38+
39+
# - Export ไฟล์ Excel
40+
workbook.save(filename="./files/exported_02.xlsx")

scripts/export_data_03.py

Lines changed: 43 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,43 @@
1+
# Export ข้อมูลจาก Database MySQL ออกมาเป็นไฟล์ Excel (.xlsx)
2+
# เป็นการ Export ข้อมูลทุกแถว แต่เอาเฉพาะข้อมูลชื่อสินค้า, ราคาสินค้า, วันที่บันทึก
3+
# และต้องทำการแสดงวันที่บันทึก ในรูปแบบ "25 November 2021"
4+
5+
import mysql.connector
6+
from openpyxl import Workbook
7+
8+
def run():
9+
# Database
10+
# - เชื่อมต่อ Database (เปลี่ยนค่า Connection เป็นของเครื่องตัวเองเน่อ)
11+
db = mysql.connector.connect(
12+
host="localhost",
13+
port=3306,
14+
user="root",
15+
password="password1234",
16+
database='golf_want_to_buy'
17+
)
18+
19+
# - ส่งคำสั่ง SQL ไปให้ MySQL ทำการโหลดข้อมูล
20+
# - Python จะรับข้อมูลทั้งหมดมาเป็น List ผ่านคำสั่ง fetchall()
21+
cursor = db.cursor()
22+
sql = '''
23+
SELECT title, price, created_at
24+
FROM products;
25+
'''
26+
cursor.execute(sql)
27+
products = cursor.fetchall()
28+
29+
# Excel
30+
# - สร้างไฟล์ใหม่ สร้างชีท และใส่แถวสำหรับเป็นหัวข้อตาราง
31+
workbook = Workbook()
32+
sheet = workbook.active
33+
sheet.append(['ชื่อสินค้า', 'ราคา', 'วันที่บันทึก'])
34+
35+
# - ใส่ข้อมูลทีละอัน เพิ่มลงไปทีละแถว
36+
for p < 5C6E span class=pl-c1>in products:
37+
p_real = list(p)
38+
p_real[2] = p_real[2].strftime('%d %B %Y')
39+
print(p_real)
40+
sheet.append(p_real)
41+
42+
# - Export ไฟล์ Excel
43+
workbook.save(filename="./files/exported_03.xlsx")

0 commit comments

Comments
 (0)
0