From eeb100ccc4b45ffa32ad79a71dd68eb300f09d21 Mon Sep 17 00:00:00 2001 From: Bob Sin Date: Tue, 21 May 2024 00:32:30 +0900 Subject: [PATCH] =?UTF-8?q?[KAN-65]=20=EC=A0=84=EC=B2=B4=EC=A0=81=EC=9C=BC?= =?UTF-8?q?=EB=A1=9C=20=ED=95=84=EB=93=9C=20=EC=9D=B4=EB=A6=84=20=EB=B3=80?= =?UTF-8?q?=EA=B2=BD,=20category=20=EB=A7=A4=ED=95=91=ED=85=8C=EC=9D=B4?= =?UTF-8?q?=EB=B8=94=EB=A1=9C=20=EB=B3=80=EA=B2=BD=20(#17)?= MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit --- mysql_batch/create_table.py | 2 +- mysql_batch/create_table_list.py | 37 ++++++----- mysql_batch/delete_table_list.py | 14 ++-- mysql_batch/insert.py | 109 ++++++++++++++++--------------- mysql_batch/main.py | 63 +++++++++++------- 5 files changed, 126 insertions(+), 99 deletions(-) diff --git a/mysql_batch/create_table.py b/mysql_batch/create_table.py index 643ed92..6ebc487 100644 --- a/mysql_batch/create_table.py +++ b/mysql_batch/create_table.py @@ -5,7 +5,6 @@ def create_table(cursor): # table deletion query cursor.execute(delete_table_restaurants) - #cursor.execute(delete_table_restaurant_likes) // 좋아요 테이블을 삭제 보류 cursor.execute(delete_table_categories) cursor.execute(delete_table_operating_infos) cursor.execute(delete_table_menus) @@ -14,6 +13,7 @@ def create_table(cursor): cursor.execute(create_table_restaurants) cursor.execute(create_table_restaurant_likes) cursor.execute(create_table_categories) + cursor.execute(create_table_restaurant_categories) cursor.execute(create_table_operating_infos) cursor.execute(create_table_menus) diff --git a/mysql_batch/create_table_list.py b/mysql_batch/create_table_list.py index 36e3017..f38f995 100644 --- a/mysql_batch/create_table_list.py +++ b/mysql_batch/create_table_list.py @@ -1,8 +1,8 @@ create_table_restaurants = """ - CREATE TABLE `restaurants` ( + CREATE TABLE IF NOT EXISTS `restaurants` ( `id` BIGINT NOT NULL AUTO_INCREMENT, `name` VARCHAR(64) NOT NULL, - `category_detail` VARCHAR(64) NOT NULL, + `original_categories` VARCHAR(64) NOT NULL, `review_count` INT NOT NULL DEFAULT 0, `like_count` INT NOT NULL DEFAULT 0, `address` VARCHAR(256), @@ -15,7 +15,7 @@ ) ENGINE=InnoDB DEFAULT CHARSET=utf8; """ -create_table_restaurant_likes =""" +create_table_restaurant_likes = """ CREATE TABLE IF NOT EXISTS `restaurant_likes` ( `id` BIGINT NOT NULL AUTO_INCREMENT, `restaurant_id` BIGINT NOT NULL, @@ -23,37 +23,42 @@ PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; """ -create_table_categories =""" +create_table_categories = """ CREATE TABLE IF NOT EXISTS `categories` ( `id` BIGINT NOT NULL AUTO_INCREMENT, - `restaurant_id` BIGINT NOT NULL, `name` VARCHAR(255) NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY unique_restaurant_name (restaurant_id, name) + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; """ -create_table_operating_infos =""" +create_table_restaurant_categories = """ + CREATE TABLE IF NOT EXISTS `restaurant_categories` ( + `id` BIGINT NOT NULL AUTO_INCREMENT, + `restaurant_id` BIGINT NOT NULL, + `category_id` BIGINT NOT NULL, + PRIMARY KEY (`id`) + ) ENGINE=InnoDB DEFAULT CHARSET=utf8; + """ + +create_table_operating_infos = """ CREATE TABLE IF NOT EXISTS `operating_infos` ( `id` BIGINT NOT NULL AUTO_INCREMENT, `restaurant_id` BIGINT NOT NULL, `day` VARCHAR(255) NOT NULL, `info` VARCHAR(255) NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY unique_restaurant_name (restaurant_id, day) + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; """ -create_table_menus =""" +create_table_menus = """ CREATE TABLE IF NOT EXISTS `menus` ( `id` BIGINT NOT NULL AUTO_INCREMENT, `restaurant_id` BIGINT NOT NULL, `name` VARCHAR(255) NOT NULL, - `price` VARCHAR(32) NOT NULL, + `price` INT NOT NULL, `description` VARCHAR(512) NOT NULL, - `is_representative` VARCHAR(32) NOT NULL, + `is_representative` TINYINT(1) NOT NULL, `image_url` VARCHAR(512) NOT NULL, - PRIMARY KEY (`id`), - UNIQUE KEY unique_restaurant_name (restaurant_id, menu_name) + PRIMARY KEY (`id`) ) ENGINE=InnoDB DEFAULT CHARSET=utf8; - """ \ No newline at end of file + """ diff --git a/mysql_batch/delete_table_list.py b/mysql_batch/delete_table_list.py index 5ba3f42..82521d0 100644 --- a/mysql_batch/delete_table_list.py +++ b/mysql_batch/delete_table_list.py @@ -1,16 +1,16 @@ delete_table_restaurants = """ -DROP TABLE IF EXISTS `restaurants`; -""" -delete_table_restaurant_likes= """ -DROP TABLE IF EXISTS `restaurant_likes`; +DELETE FROM `restaurants`; """ delete_table_categories= """ -DROP TABLE IF EXISTS `categories`; +DELETE FROM `categories`; +""" +delete_table_restaurant_categories = """ +DELETE FROM `restaurant_categories`; """ delete_table_operating_infos= """ -DROP TABLE IF EXISTS `operating_infos`; +DELETE FROM `operating_infos`; """ delete_table_menus= """ -DROP TABLE IF EXISTS `menus`; +DELETE FROM `menus`; """ \ No newline at end of file diff --git a/mysql_batch/insert.py b/mysql_batch/insert.py index 77c72d8..26c93b7 100644 --- a/mysql_batch/insert.py +++ b/mysql_batch/insert.py @@ -1,12 +1,9 @@ -import pymysql - def insert_into_restaurants(cursor, restaurant): - - insert_query = """ + insert_query = """ INSERT INTO `restaurants` ( `id`, `name`, - `category_detail`, + `original_categories`, `review_count`, `like_count`, `address`, @@ -20,55 +17,66 @@ def insert_into_restaurants(cursor, restaurant): %s, %s, %s, %s, %s, %s, %s, %s, %s, %s, %s ); """ - - try: - rating = float(restaurant['rating']) - except ValueError: - rating = 0.0 - cursor.execute(insert_query, ( - restaurant['id'], - restaurant['name'], - restaurant['category'], - 0, - 0, - restaurant['address'], - restaurant['number'], - rating , - restaurant['image_url'], - 0, - 0 - )) + try: + rating = float(restaurant['rating']) + except ValueError: + rating = 0.0 + + cursor.execute(insert_query, ( + restaurant['id'], + restaurant['name'], + restaurant['category'], + 0, + 0, + restaurant['address'], + restaurant['number'], + rating, + restaurant['image_url'], + 0, + restaurant['discount_content'] + )) -def insert_into_categories(cursor, restaurant): - - insert_query = """ - INSERT IGNORE INTO categories (restaurant_id, name) +def insert_into_categories(cursor, id, name): + insert_query = """ + INSERT IGNORE INTO categories (id, name) VALUES (%s, %s); """ - - cursor.execute(insert_query, ( - restaurant['id'], - restaurant['custom_category'] - )) + + cursor.execute(insert_query, ( + id, + name + )) + + +def insert_into_restaurant_categories(cursor, restaurant_id, category_id): + insert_query = """ + INSERT IGNORE INTO restaurant_categories (restaurant_id, category_id) + VALUES (%s, %s); + """ + + cursor.execute(insert_query, ( + restaurant_id, + category_id + )) + def insert_into_operating_infos(cursor, operation): - - insert_query = """ + insert_query = """ INSERT IGNORE INTO operating_infos (restaurant_id, day, info) VALUES (%s, %s, %s); """ - - cursor.execute(insert_query, ( - operation['restaurant_id'], - operation['day'], - operation['info'] - )) + + cursor.execute(insert_query, ( + operation['restaurant_id'], + operation['day'], + operation['info'] + )) + def insert_into_menus(cursor, menu): - - insert_query = """ + insert_query = """ INSERT IGNORE INTO menus ( restaurant_id, name, @@ -79,13 +87,12 @@ def insert_into_menus(cursor, menu): ) VALUES (%s, %s, %s, %s, %s, %s); """ - - cursor.execute(insert_query, ( - menu['restaurant_id'], - menu['menu_name'], - menu['price'], - menu['description'], - menu['is_representative'], - menu['image_url'] - )) + cursor.execute(insert_query, ( + menu['restaurant_id'], + menu['menu_name'], + int(menu['price'].replace(',', '')), + menu['description'] if menu['description'] != "설명 없음" else "", + 1 if menu['is_representative'] == '대표' else 0, + menu['image_url'] + )) diff --git a/mysql_batch/main.py b/mysql_batch/main.py index e9230e6..fccfa34 100644 --- a/mysql_batch/main.py +++ b/mysql_batch/main.py @@ -1,16 +1,18 @@ -import pymysql import csv + +import pymysql + from create_table import create_table from insert import * # connect to MySQL server conn = pymysql.connect( - host="127.0.0.1", - port=3306, - user="skku-user", - password="skku-pw", - db="skku", - charset='utf8' + host="127.0.0.1", + port=3306, + user="skku-user", + password="skku-pw", + db="skku", + charset='utf8' ) # table creation query(restaurants, restaurant_likes) @@ -18,33 +20,46 @@ create_table(cursor) conn.commit() +# Insert restaurants, categories +with open('../restaurants.csv', mode='r') as file: + csv_dict = csv.DictReader(file) + categories = dict() + category_id = 1 + restaurants = [] + for restaurant in csv_dict: + restaurants.append(restaurant) + if restaurant['custom_category'] not in categories: + categories[restaurant['custom_category']] = category_id + category_id += 1 + + for c in categories: + insert_into_categories(cursor, categories[c], c) -# Insert restaurants, categories -with open('../restaurants.csv', mode ='r')as file: - csv_dict = csv.DictReader(file) - for restaurant in csv_dict: - insert_into_restaurants(cursor, restaurant) - insert_into_categories(cursor, restaurant) + for restaurant in restaurants: + insert_into_restaurants(cursor, restaurant) + insert_into_restaurant_categories( + cursor, + restaurant['id'], + categories[restaurant['custom_category']] + ) conn.commit() # Insert operating_infos -with open('../operations.csv', mode ='r')as file: - csv_dict = csv.DictReader(file) - for operation in csv_dict: - insert_into_operating_infos(cursor, operation) - +with open('../operations.csv', mode='r') as file: + csv_dict = csv.DictReader(file) + for operation in csv_dict: + insert_into_operating_infos(cursor, operation) conn.commit() # Insert menus -with open('../menus.csv', mode ='r')as file: - csv_dict = csv.DictReader(file) - for menu in csv_dict: - insert_into_menus(cursor, menu) +with open('../menus.csv', mode='r') as file: + csv_dict = csv.DictReader(file) + for menu in csv_dict: + insert_into_menus(cursor, menu) conn.commit() - # close the connection -conn.close() \ No newline at end of file +conn.close()