일단 이것의 용도에 대해 이야기 해보자
그래서 우리는 google 엑셀을 이용하기로 하였다 .
프로젝트 최상위 경로에서 db_upload 를 만들어 거기에 google 엑셀에서 다운받은
csv 파일을 넣는다.
cd ~/(프로젝트 파일명)
mkdir db_upload
cd db_upload
❯ ll
total 44K
-rw-rw-r-- 1 ubuntu ubuntu 186 4월 29 17:20 Category.csv
-rw-rw-r-- 1 ubuntu ubuntu 64 4월 29 17:21 CategoryDestination.csv
-rw-rw-r-- 1 ubuntu ubuntu 50 4월 29 17:21 City.csv
-rw-rw-r-- 1 ubuntu ubuntu 1.9K 4월 29 18:53 Convenience.csv
-rw-rw-r-- 1 ubuntu ubuntu 3.6K 5월 1 21:15 db.py
-rw-rw-r-- 1 ubuntu ubuntu 417 4월 29 17:21 Destination.csv
-rw-rw-r-- 1 ubuntu ubuntu 90 4월 29 17:21 District.csv
-rw-rw-r-- 1 ubuntu ubuntu 3.4K 4월 29 17:21 ProductConvenience.csv
-rw-rw-r-- 1 ubuntu ubuntu 3.3K 5월 1 21:34 Product.csv
-rw-rw-r-- 1 ubuntu ubuntu 3.4K 4월 29 17:22 ProductImage.csv
-rw-rw-r-- 1 ubuntu ubuntu 106 4월 29 17:22 ServiceCategory.csv
하나의 csv 파일을 열어보자
vim city.csv
id,name 1,서울
2,부산
3,대구
4,강원도
이제 이것을 돌릴 파일을 만들자
vim db.py
import os
import django
import sys
import csv
BASE_DIR = os.path.dirname(os.path.dirname(os.path.abspath(__file__)))
sys.path.append(BASE_DIR)
os.chdir("..")
os.environ.setdefault("DJANGO_SETTINGS_MODULE", "wagrano.settings") # wagrano 부분만 나의 디비 이름과 맞춰주면 된다.
django.setup()
from products.models import *
with open('./db_upload/Category.csv', newline='') as csvfile:
csv_reader = csv.DictReader(csvfile)
for data in csv_reader:
Category.objects.create(
name = data['name'],
image_url = data['image_url']
)
with open('./db_upload/Destination.csv', newline='') as csvfile:
csv_reader = csv.DictReader(csvfile)
for data in csv_reader:
Destination.objects.create(
name = data['name']
)
with open('./db_upload/CategoryDestination.csv', newline='') as csvfile:
csv_reader = csv.DictReader(csvfile)
for data in csv_reader:
CategoryDestination.objects.create(
category_id = data['category_id'],
destination_id = data['destination_id'],
)
with open('./db_upload/City.csv', newline='') as csvfile:
csv_reader = csv.DictReader(csvfile)
for data in csv_reader:
City.objects.create(
name =data['name'],
)
with open('./db_upload/District.csv', newline='') as csvfile:
csv_reader = csv.DictReader(csvfile)
for data in csv_reader:
District.objects.create(
name = data['name'],
city_id = data['city_id'],
)
with open('./db_upload/Product.csv', newline='') as csvfile:
csv_reader = csv.DictReader(csvfile)
for data in csv_reader:
Product.objects.create(
name = data['name'],
star_rating = data['star_rating'],
rating = data['rating'],
description = data['description'],
address = data['address'],
latitude = data['latitude'],
longitude = data['longitude'],
price = data['price'],
category_id = data['category_id'],
destination_id = data['destination_id'],
city_id = data['city_id'],
district_id = data['district_id'],
)
with open('./db_upload/ProductImage.csv', newline='') as csvfile:
csv_reader = csv.DictReader(csvfile)
for data in csv_reader:
ProductImage.objects.create(
image_url = data['image_url'],
product_id = data['product_id'],
)
with open('./db_upload/ServiceCategory.csv', newline='') as csvfile:
csv_reader = csv.DictReader(csvfile)
for data in csv_reader:
ServiceCategory.objects.create(
name = data['name'],
)
with open('./db_upload/Convenience.csv', newline='') as csvfile:
csv_reader = csv.DictReader(csvfile)
for data in csv_reader:
Convenience.objects.create(
name = data['name'],
service_category_id = data['service_category_id'],
)
with open('./db_upload/ProductConvenience.csv', newline='') as csvfile:
csv_reader = csv.DictReader(csvfile)
for data in csv_reader:
ProductConvenience.objects.create(
product_id = data['product_id'],
convenience_id = data['convenience_id'],
)
print("SUCCESS")
python db.py 로 파일 실행
제대로 되었다면 SUCCESS가 뜨며 db 테이블을 확인해보자
mysql -u root -p
password
show databases;
use wagrano;
show tables;
show select * from products;