Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(batch) be KAN-80: 좋아요 기능을 위한 DB 생성 코드 업데이트 #19

Closed
wants to merge 7 commits into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions mysql_batch/create_table.py
Original file line number Diff line number Diff line change
Expand Up @@ -5,16 +5,15 @@ 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_restaurant_categories)
cursor.execute(delete_table_operating_infos)
cursor.execute(delete_table_menus)

# table creation query
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)

31 changes: 13 additions & 18 deletions mysql_batch/create_table_list.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,50 +15,45 @@
) 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,
`user_id` BIGINT 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,
`name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
"""

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`)
`name` VARCHAR(255) NOT NULL,
PRIMARY KEY (`id`),
UNIQUE KEY unique_restaurant_name (restaurant_id, name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
"""

create_table_operating_infos = """
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`)
PRIMARY KEY (`id`),
UNIQUE KEY unique_restaurant_name (restaurant_id, day)
) 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` INT NOT NULL,
`description` VARCHAR(512) NOT NULL,
`is_representative` TINYINT(1) NOT NULL,
`is_representative` BOOLEAN NOT NULL,
`image_url` VARCHAR(512) NOT NULL,
PRIMARY KEY (`id`)
PRIMARY KEY (`id`),
UNIQUE KEY unique_restaurant_name (restaurant_id, name)
) ENGINE=InnoDB DEFAULT CHARSET=utf8;
"""
16 changes: 15 additions & 1 deletion mysql_batch/delete_table_list.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,20 @@

delete_table_restaurants = """

DROP TABLE IF EXISTS `restaurants`;
"""
delete_table_restaurant_likes= """
DROP TABLE IF EXISTS `restaurant_likes`;
"""
delete_table_categories= """
DROP TABLE IF EXISTS `categories`;
"""
delete_table_operating_infos= """
DROP TABLE IF EXISTS `operating_infos`;
"""
delete_table_menus= """
DROP TABLE IF EXISTS `menus`;
=======
DELETE FROM `restaurants`;
"""
delete_table_categories= """
Expand All @@ -13,4 +28,3 @@
"""
delete_table_menus= """
DELETE FROM `menus`;
"""
104 changes: 51 additions & 53 deletions mysql_batch/insert.py
Original file line number Diff line number Diff line change
@@ -1,5 +1,8 @@
import pymysql

def insert_into_restaurants(cursor, restaurant):
insert_query = """

insert_query = """
INSERT INTO `restaurants` (
`id`,
`name`,
Expand All @@ -18,60 +21,55 @@ def insert_into_restaurants(cursor, restaurant):
);
"""

cursor.execute(insert_query, (
restaurant['id'],
restaurant['name'],
restaurant['category'],
0,
0,
restaurant['address'],
restaurant['number'],
0,
restaurant['image_url'],
0,
restaurant['discount_content']
))

try:
rating = float(restaurant['rating'])
except ValueError:
rating = 0.0

cursor.execute(insert_query, (
restaurant['id'],
restaurant['name'],
restaurant['custom_category'],
0,
0,
restaurant['address'],
restaurant['number'],
rating ,
restaurant['image_url'],
0,
0
))


def insert_into_categories(cursor, id, name):
insert_query = """
INSERT IGNORE INTO categories (id, name)
def insert_into_categories(cursor, restaurant):

insert_query = """
INSERT IGNORE INTO categories (restaurant_id, name)
VALUES (%s, %s);
"""

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
))


cursor.execute(insert_query, (
restaurant['id'],
restaurant['custom_category']
))

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 @@ -82,12 +80,12 @@ def insert_into_menus(cursor, menu):
)
VALUES (%s, %s, %s, %s, %s, %s);
"""

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']
))
cursor.execute(insert_query, (
menu['restaurant_id'],
menu['menu_name'],
int(menu['price'].replace(",","")),
menu['description'],
(menu['is_representative']=="대표"),
menu['image_url']
))
63 changes: 23 additions & 40 deletions mysql_batch/main.py
Original file line number Diff line number Diff line change
@@ -1,65 +1,48 @@
import csv

import pymysql

import csv
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)

for restaurant in restaurants:
insert_into_restaurants(cursor, restaurant)
insert_into_restaurant_categories(
cursor,
restaurant['id'],
categories[restaurant['custom_category']]
)
# 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)

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()
7 changes: 7 additions & 0 deletions mysql_batch/read_csv.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,7 @@
import csv

with open('restaurants.csv', mode ='r')as file:
csv_dict = csv.DictReader(file)
for row in csv_dict:
print(row)
break
Loading
Loading