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 9ab3918

Browse filesBrowse files
committed
More examples
1 parent 57c9c8c commit 9ab3918
Copy full SHA for 9ab3918

File tree

3 files changed

+57
-3
lines changed
Filter options

3 files changed

+57
-3
lines changed

‎scripts/export_data_06.py

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

19-
# - ส่งคำสั่ง SQL ไปให้ MySQL ทำการโหลดข้อมูล
20-
# - Python จะรับข้อมูลทั้งหมดมาเป็น List ผ่านคำสั่ง fetchall()
19+
# - โหลดข้อมูลสินค้าที่รวม Hashtags ทั้งหมดมาด้วย
2120
cursor = db.cursor()
2221
sql = '''
2322
SELECT p.id AS id, p.title AS title, p.price AS price, GROUP_CONCAT(ph.hashtag SEPARATOR ' ') AS hashtags

‎scripts/export_data_07.py

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

‎scripts/import_data_04.py

Copy file name to clipboardExpand all lines: scripts/import_data_04.py
+1-1Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,7 @@ def run():
1919
port=3306,
2020
user="root",
2121
password="password1234",
22-
database='golf_want_to_buy_completed'
22+
database='golf_want_to_buy'
2323
)
2424
cursor = db.cursor()
2525

0 commit comments

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