Skip to content

Commit

Permalink
[KAN-65] 전체적으로 필드 이름 변경, category 매핑테이블로 변경 (#17)
Browse files Browse the repository at this point in the history
  • Loading branch information
sinkyoungdeok authored May 20, 2024
1 parent 7e52a1e commit eeb100c
Show file tree
Hide file tree
Showing 5 changed files with 126 additions and 99 deletions.
2 changes: 1 addition & 1 deletion mysql_batch/create_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -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)
Expand All @@ -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)

37 changes: 21 additions & 16 deletions mysql_batch/create_table_list.py
Original file line number Diff line number Diff line change
@@ -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),
Expand All @@ -15,45 +15,50 @@
) 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,
`user_id` VARCHAR(255) NOT NULL,
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;
"""
"""
14 changes: 7 additions & 7 deletions mysql_batch/delete_table_list.py
Original file line number Diff line number Diff line change
@@ -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`;
"""
109 changes: 58 additions & 51 deletions mysql_batch/insert.py
Original file line number Diff line number Diff line change
@@ -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`,
Expand All @@ -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,
Expand All @@ -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']
))
63 changes: 39 additions & 24 deletions mysql_batch/main.py
Original file line number Diff line number Diff line change
@@ -1,50 +1,65 @@
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)
cursor = conn.cursor()
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()
conn.close()

0 comments on commit eeb100c

Please sign in to comment.