diff --git a/README.md b/README.md index 1d3302b..b9575af 100644 --- a/README.md +++ b/README.md @@ -2,13 +2,24 @@ Example Python codes that do the processes between MySQL database and Excel spreadsheet files. +https://www.youtube.com/watch?v=xXDR9rxVfA8 + ## Setup database table - -*Structure* + +*products table structure* + + +*product_notes table structure* + + +*categories table structure* + + +*hashtags table structure* - -*Sample data* + +*products_hashtags table structure* ## Install Python 3 and pipenv diff --git a/export_data.py b/export_data.py index a92ed2a..16e9ad7 100644 --- a/export_data.py +++ b/export_data.py @@ -12,13 +12,14 @@ password="password1234", database='golf_want_to_buy' ) - -# - ส่งคําสั่ง SQL ไปให้ MySQL ทําการโหลดข้อมูล -# - Python จะรับข้อมูลทั้งหมดมาเป็น List ผ่านคําสั่ง fetchall() cursor = db.cursor() + +# - โหลดข้อมูลสินค้า พร้อมประเภทสินค้า sql = ''' - SELECT * - FROM products; + SELECT p.id AS id, p.title AS title, p.price AS price, c.title AS category + FROM products AS p + LEFT JOIN categories AS c + ON p.category_id = c.id; ''' cursor.execute(sql) products = cursor.fetchall() @@ -27,7 +28,7 @@ # - สร้างไฟล์ใหม่ สร้างชีท และใส่แถวสําหรับเป็นหัวข้อตาราง workbook = Workbook() sheet = workbook.active -sheet.append(['ID', 'ชื่อสินค้า', 'ราคา', 'ต้องการมากๆ', 'วันที่บันทึก']) +sheet.append(['ID', 'ชื่อสินค้า', 'ราคาสินค้า', 'ประเภทสินค้า']) # - ใส่ข้อมูลทีละอัน เพิ่มลงไปทีละแถว for p in products: @@ -35,4 +36,8 @@ sheet.append(p) # - Export ไฟล์ Excel -workbook.save(filename="exported.xlsx") \ No newline at end of file +workbook.save(filename="exported.xlsx") + +# ปิดการเชื่้อมต่อ Database +cursor.close() +db.close() \ No newline at end of file diff --git a/import_data.py b/import_data.py index 0762ae4..9e0d9b8 100644 --- a/import_data.py +++ b/import_data.py @@ -1,21 +1,13 @@ -# Import ข้อมูลทุกแถวจากไฟล์ Excel (.xlsx) เข้าสู่ Database MySQL -# โดยข้อมูลจากไฟล์ Excel จะเริ่มต้นตรงแถวที่ 2 +# Import ข้อมูลสินค้าและประเภทสินค้าจากไฟล์ Excel (.xlsx) เข้าสู่ Database MySQL import mysql.connector from openpyxl import load_workbook # Excel # - โหลดไฟล์ และโหลดชีทที่เปิดอยู่ -workbook = load_workbook('imported.xlsx') +workbook = load_workbook('imported_02.xlsx') sheet = workbook.active -# - เก็บข้อมูล (values_only คือ แบบดิบๆ) ทีละแถวไว้ใน List -# - เริ่มต้นจากแถวที่ 2 ไปจนถึงแถวสุดท้าย -values = [] -for row in sheet.iter_rows(min_row=2, values_only=True): - print(row) - values.append(row) - # Database # - เชื่อมต่อ Database (เปลี่ยนค่า Connection เป็นของเครื่องตัวเองเน่อ) db = mysql.connector.connect( @@ -25,16 +17,73 @@ password="password1234", database='golf_want_to_buy' ) - -# - ส่งคําสั่ง SQL ไปให้ MySQL ทําการเพิ่มข้อมูล -# - ใช้ executemany() เพื่อเพิ่มข้อมูลหลายอัน cursor = db.cursor() -sql = ''' - INSERT INTO products (title, price, is_necessary) - VALUES (%s, %s, %s); + +# Let's go +# - โหลดข้อมูลประเภทสินค้าทั้งหมด +sql_select_categories = ''' + SELECT * + FROM categories +''' +cursor.execute(sql_select_categories) +categories = cursor.fetchall() + +# - เปรียบเทียบข้อมูลประเภทสินค้า +# - อันไหนยังไม่มีใน Database ให้เพิ่มลงไปใน List +categories_values = [] +for row in sheet.iter_rows(min_row=2, values_only=True): + is_new = True + category = row[3] + + for c in categories: + if category == c[1]: + is_new = False + break + + if is_new: + print((category, )) + categories_values.append((category, )) + +# - เพิ่มประเภทสินค้าใหม่ลง Database (ถ้ามี) +if len(categories_values)> 0: + sql_insert_categories = ''' + INSERT INTO categories (title) + VALUES (%s) + ''' + cursor.executemany(sql_insert_categories, categories_values) + db.commit() + print('เพิ่มประเภทสินค้าจํานวน ' + str(cursor.rowcount) + ' แถว') +else: + print('ไม่มีประเภทสินค้าใหม่มาเพิ่ม') + +# - โหลดข้อมูลประเภทสินค้าทั้งหมด อีกครั้ง +cursor.execute(sql_select_categories) +categories = cursor.fetchall() + +# - เชื่อมต่อ category_id กับสินค้าใหม่ของเรา แล้วเพิ่มลงไปใน List +products_values = [] +for row in sheet.iter_rows(min_row=2, values_only=True): + category_title = row[3] + category_id = None + + for c in categories: + if category_title == c[1]: + category_id = c[0] + break + + product = (row[0], row[1], row[2], category_id) + print(product) + products_values.append(product) + +# - เพิ่มสินค้าลง Database +sql_insert_products = ''' + INSERT INTO products (title, price, is_necessary, category_id) + VALUES (%s, %s, %s, %s); ''' -cursor.executemany(sql, values) +cursor.executemany(sql_insert_products, products_values) db.commit() +print('เพิ่มสินค้าจํานวน ' + str(cursor.rowcount) + ' แถว') -# - สรุปจํานวนข้อมูลที่เพิ่มไป -print('เพิ่มข้อมูลจํานวน ' + str(cursor.rowcount) + ' แถว') \ No newline at end of file +# ปิดการเชื่อมต่อ Database +cursor.close() +db.close() \ No newline at end of file diff --git a/imported.xlsx b/imported.xlsx deleted file mode 100644 index 2aed21c..0000000 Binary files a/imported.xlsx and /dev/null differ diff --git a/imported_02.xlsx b/imported_02.xlsx new file mode 100644 index 0000000..2c6325d Binary files /dev/null and b/imported_02.xlsx differ diff --git a/snapshots/db-table-data.png b/snapshots/db-table-data.png deleted file mode 100644 index c57d45a..0000000 Binary files a/snapshots/db-table-data.png and /dev/null differ diff --git a/snapshots/db-table-structure.png b/snapshots/db-table-structure.png deleted file mode 100644 index 21888b2..0000000 Binary files a/snapshots/db-table-structure.png and /dev/null differ