|
| 1 | +# Import ข้อมูลจากไฟล์ Excel (.xlsx) เข้าสู่ Database MySQL |
| 2 | +# เป็นการ Import ข้อมูลทุกแถวมาใส่ใน products |
| 3 | +# แต่จะเอาข้อมูลแฮชแท็ก แยกไปใส่ใน hashtags (ถ้ายังไม่มี) |
| 4 | +# และที่สำคัญ ต้องเชื่อม products และ hashtags เข้ากันให้เรียบร้อย |
| 5 | + |
| 6 | +import mysql.connector |
| 7 | +from openpyxl import load_workbook |
| 8 | + |
| 9 | +def run(): |
| 10 | + # Excel |
| 11 | + # - โหลดไฟล์ และโหลดชีทที่เปิดอยู่ |
| 12 | + workbook = load_workbook('./files/imported_06.xlsx') |
| 13 | + sheet = workbook.active |
| 14 | + |
| 15 | + # Database |
| 16 | + # - เชื่อมต่อ Database (เปลี่ยนค่า Connection เป็นของเครื่องตัวเองเน่อ) |
| 17 | + db = mysql.connector.connect( |
| 18 | + host="localhost", |
| 19 | + port=3306, |
| 20 | + user="root", |
| 21 | + password="password1234", |
| 22 | + database='golf_want_to_buy' |
| 23 | + ) |
| 24 | + cursor = db.cursor() |
| 25 | + |
| 26 | + # - โหลดข้อมูลแฮชแท็กทั้งหมด |
| 27 | + sql = ''' |
| 28 | + SELECT * |
| 29 | + FROM hashtags; |
| 30 | + ''' |
| 31 | + cursor.execute(sql) |
| 32 | + hashtags = cursor.fetchall() |
| 33 | + |
| 34 | + # - เอาแฮชแท็กใหม่ มาใส่ใน List |
| 35 | + hashtags_values = [] |
| 36 | + for row in sheet.iter_rows(min_row=2, values_only=True): |
| 37 | + hashtags_all_string = row[3] |
| 38 | + hashtags_list = hashtags_all_string.split(' ') |
| 39 | + |
| 40 | + for hashtag_excel in hashtags_list: |
| 41 | + is_new = True |
| 42 | + for hashtag_table in hashtags: |
| 43 | + if hashtag_excel == hashtag_table[1]: |
| 44 | + is_new = False |
| 45 | + break |
| 46 | + for hashtag_value in hashtags_values: |
| 47 | + if hashtag_excel == hashtag_value[0]: |
| 48 | + is_new = False |
| 49 | + break |
| 50 | + |
| 51 | + if is_new: |
| 52 | + print((hashtag_excel,)) |
| 53 | + hashtags_values.append((hashtag_excel,)) |
| 54 | + |
| 55 | + # - เพิ่มข้อมูลแฮชแท็ก (ถ้ามีอันใหม่) |
| 56 | + if len(hashtags_values) > 0: |
| 57 | + sql = ''' |
| 58 | + INSERT INTO hashtags (hashtag) |
| 59 | + VALUES (%s); |
| 60 | + ''' |
| 61 | + cursor.executemany(sql, hashtags_values) |
| 62 | + db.commit() |
| 63 | + print('เพิ่มแฮชแท็กจำนวน ' + str(cursor.rowcount) + ' แถว') |
| 64 | + else: |
| 65 | + print('ไม่มีแฮชแท็กใหม่มาเพิ่ม') |
| 66 | + |
| 67 | + # - ใส่สินค้าทั้งหมดลง List |
| 68 | + products_values = [] |
| 69 | + for row in sheet.iter_rows(min_row=2, values_only=True): |
| 70 | + product = (row[0], row[1], row[2]) |
| 71 | + print(product) |
| 72 | + products_values.append(product) |
| 73 | + |
| 74 | + # - เพิ่มข้อมูลสินค้า |
| 75 | + sql = ''' |
| 76 | + INSERT INTO products (title, price, is_necessary) |
| 77 | + VALUES (%s, %s, %s); |
| 78 | + ''' |
| 79 | + cursor.executemany(sql, products_values) |
| 80 | + db.commit() |
| 81 | + print('เพิ่มสินค้าจำนวน ' + str(cursor.rowcount) + ' แถว') |
| 82 | + |
| 83 | + # - ดึง ID ของสินค้าแรกที่เพิ่มไปเมื่อกี๊ มาเตรียมตัวใช้ |
| 84 | + first_product_id = cursor.lastrowid |
| 85 | + |
| 86 | + # - โหลดข้อมูลแฮชแท็กทั้งหมด (หลังบันทึกล่าสุดไปแล้ว) |
| 87 | + sql = ''' |
| 88 | + SELECT * |
| 89 | + FROM hashtags; |
| 90 | + ''' |
| 91 | + cursor.execute(sql) |
| 92 | + hashtags = cursor.fetchall() |
| 93 | + |
| 94 | + # - เชื่อมสินค้ากับแฮชแท็กเข้าด้วยกัน |
| 95 | + products_hashtags_values = [] |
| 96 | + for (p_id, row) in enumerate(sheet.iter_rows(min_row=2, values_only=True), first_product_id): |
| 97 | + hashtags_all_string = row[3] |
| 98 | + hashtags_list = hashtags_all_string.split(' ') |
| 99 | + |
| 100 | + for hashtag_excel in hashtags_list: |
| 101 | + hashtag_id = None |
| 102 | + for hashtag_table in hashtags: |
| 103 | + if hashtag_excel == hashtag_table[1]: |
| 104 | + hashtag_id = hashtag_table[0] |
| 105 | + break |
| 106 | + print((p_id, hashtag_id)) |
| 107 | + products_hashtags_values.append((p_id, hashtag_id)) |
| 108 | + |
| 109 | + # - ใส่ข้อมูลลงในตารางเชื่อมโยง products_hashtags |
| 110 | + sql = ''' |
| 111 | + INSERT INTO products_hashtags (product_id, hashtag_id) |
| 112 | + VALUES (%s, %s); |
| 113 | + ''' |
| 114 | + cursor.executemany(sql, products_hashtags_values) |
| 115 | + db.commit() |
| 116 | + print('เพิ่มการเชื่อมโยงจำนวน ' + str(cursor.rowcount) + ' แถว') |
| 117 | + |
| 118 | + # ปิดการเชื่อมต่อ Database |
| 119 | + cursor.close() |
| 120 | + db.close() |
0 commit comments