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 07d25f8

Browse filesBrowse files
committed
More export examples
1 parent 9ab3918 commit 07d25f8
Copy full SHA for 07d25f8

File tree

4 files changed

+131
-3
lines changed
Filter options

4 files changed

+131
-3
lines changed

‎scripts/export_data_06.py

Copy file name to clipboardExpand all lines: scripts/export_data_06.py
+3-2Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,10 +16,11 @@ def run():
1616
database='golf_want_to_buy'
1717
)
1818

19-
# - โหลดข้อมูลสินค้าที่รวม Hashtags ทั้งหมดมาด้วย
19+
# - โหลดข้อมูลสินค้าที่รวมแฮชแท็กทั้งหมดมาด้วย
2020
cursor = db.cursor()
2121
sql = '''
22-
SELECT p.id AS id, p.title AS title, p.price AS price, GROUP_CONCAT(ph.hashtag SEPARATOR ' ') AS hashtags
22+
SELECT p.id AS id, p.title AS title, p.price AS price,
23+
GROUP_CONCAT(ph.hashtag SEPARATOR ' ') AS hashtags
2324
FROM products AS p
2425
LEFT JOIN (
2526
SELECT ph1.product_id AS product_id, h1.hashtag AS hashtag

‎scripts/export_data_07.py

Copy file name to clipboardExpand all lines: scripts/export_data_07.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,7 @@ def run():
1616
database='golf_want_to_buy'
1717
)
1818

19-
# - โหลดข้อมูลสินค้าที่รวม Hashtags ทั้งหมดมาด้วย
19+
# - โหลดข้อมูลสินค้าที่รวมประเภทสินค้า และแฮชแท็กทั้งหมดมาด้วย
2020
cursor = db.cursor()
2121
sql = '''
2222
SELECT p.id AS id, p.title AS title, p.price AS price,

‎scripts/export_data_08.py

Copy file name to clipboard
+58Lines changed: 58 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,58 @@
1+
# Export ข้อมูลจาก Database MySQL ออกมาเป็นไฟล์ Excel (.xlsx)
2+
# เป็นการ Export ข้อมูลสินค้าทุกแถว
3+
# รวมประเภทสินค้า, แฮชแท็กทั้งหมด, และโน้ตของสินค้าแต่ละอัน มาแสดงด้วย
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+
# - โหลดข้อมูลสินค้า ที่รวมประเภทสินค้า, แฮชแท็ก, โน้ต มาด้วย
20+
cursor = db.cursor()
21+
sql = '''
22+
SELECT p.id AS id, p.title AS title, p.price AS price,
23+
c.title AS category, GROUP_CONCAT(ph.hashtag SEPARATOR ' ') AS hashtags,
24+
pn.notes AS notes
25+
FROM products AS p
26+
LEFT JOIN categories AS c
27+
ON p.category_id = c.id
28+
LEFT JOIN (
29+
SELECT ph1.product_id AS product_id, h1.hashtag AS hashtag
30+
FROM products_hashtags AS ph1
31+
LEFT JOIN hashtags AS h1
32+
ON ph1.hashtag_id = h1.id
33+
) AS ph
34+
ON p.id = ph.product_id
35+
LEFT JOIN product_notes AS pn
36+
ON p.id = pn.product_id
37+
GROUP BY p.id
38+
'''
39+
cursor.execute(sql)
40+
products = cursor.fetchall()
41+
42+
# Excel
43+
# - สร้างไฟล์ใหม่ สร้างชีท และใส่แถวสำหรับเป็นหัวข้อตาราง
44+
workbook = Workbook()
45+
sheet = workbook.active
46+
sheet.append(['ID', 'ชื่อสินค้า', 'ราคา', 'ประเภทสินค้า', 'แฮชแท็ก', 'โน้ต'])
47+
48+
# - ใส่ข้อมูลทีละอัน เพิ่มลงไปทีละแถว
49+
for p in products:
50+
print(p)
51+
sheet.append(p)
52+
53+
# - Export ไฟล์ Excel
54+
workbook.save(filename="./files/exported_08.xlsx")
55+
56+
# ปิดการเชื่อมต่อ Database
57+
cursor.close()
58+
db.close()

‎scripts/export_data_09.py

Copy file name to clipboard
+69Lines changed: 69 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,69 @@
1+
# Export ข้อมูลจาก Database MySQL ออกมาเป็นไฟล์ Excel (.xlsx)
2+
# เป็นการ Export ข้อมูลสินค้าทุกแถว รวมประเภทสินค้าด้วย มาอยู่ในชีทแรก
3+
# และ Export ข้อมูลประเภทสินค้าทุกแถว พร้อมนับจำนวนสินค้า มาอยู่ในชีทที่สอง
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+
cursor = db.cursor()
19+
20+
# - โหลดข้อมูลสินค้าที่รวมประเภทสินค้ามาด้วย
21+
sql = '''
22+
SELECT p.id AS id, p.title AS title, p.price AS price, c.title AS category
23+
FROM products AS p
24+
LEFT JOIN categories AS c
25+
ON p.category_id = c.id
26+
'''
27+
cursor.execute(sql)
28+
products = cursor.fetchall()
29+
30+
# - โหลดข้อมูลประเภทสินค้า พร้อมนับจำนวนสินค้าด้วย
31+
sql = '''
32+
SELECT c.id AS id, c.title AS title, COUNT(p.id) AS products_count
33+
FROM categories AS c
34+
LEFT JOIN products AS p
35+
ON c.id = p.category_id
36+
GROUP BY c.id
37+
'''
38+
cursor.execute(sql)
39+
categories = cursor.fetchall()
40+
41+
# Excel
42+
# - สร้างไฟล์ใหม่
43+
workbook = Workbook()
44+
45+
# - สร้างชีทสำหรับสินค้า
46+
products_sheet = workbook.active
47+
products_sheet.title = 'สินค้า'
48+
products_sheet.append(['ID', 'ชื่อสินค้า', 'ราคา', 'ประเภทสินค้า'])
49+
50+
# - ใส่ข้อมูลสินค้าทีละอัน เพิ่มลงไปทีละแถว
51+
for p in products:
52+
print(p)
53+
products_sheet.append(p)
54+
55+
# - สร้างชีทสำหรับประเภทสินค้า
56+
categories_sheet = workbook.create_sheet('ประเภทสินค้า', 1)
57+
categories_sheet.append(['ID', 'ประเภทสินค้า', 'จำนวนสินค้า'])
58+
59+
# - ใส่ข้อมูลประเภทสินค้าทีละอัน เพิ่มลงไปทีละแถว
60+
for c in categories:
61+
print(c)
62+
categories_sheet.append(c)
63+
64+
# - Export ไฟล์ Excel
65+
workbook.save(filename="./files/exported_09.xlsx")
66+
67+
# ปิดการเชื่อมต่อ Database
68+
cursor.close()
69+
db.close()

0 commit comments

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