- pymysql을 활용해 mysql 연동하기
- pymysql 설치
pip install pymysql
- 로컬 DB에 연결
import pymysql
db = pymysql.connect(
host="127.0.0.1",
user="root",
password="skrxk362636!@",
database="test_db3",
charset='utf8'
)
cursor = db.cursor()
- Table 생성 (이 단계 전에 Mysql workbench에서 스키마를 생성해주어야 함)
cursor.execute("CREATE TABLE newsTable (id int primary key not null auto_increment, Topic varchar(100), Period char(100), Title varchar(200))")
- json 파일 가져오기
# json 파일을 가져오는 경로에 문제가 있어 직접 경로 설정
file_path = "C:/Users/xxxxxx/Desktop/Projects/news_crawler/titles.json"
with open(file_path, "r", encoding="utf-8") as file:
topics = json.load(file)
- DB에 커밋(저장해주기)
sql = "INSERT INTO newsTable(Topic, Period, Title) VALUES (%s, %s, %s)"
val = (save_topic, save_period, topics[topic][period][i])
cursor.execute(sql, val)
db.commit()
728x90
'ssung_데이터 엔지니어링 > 4주차_1차 프로젝트' 카테고리의 다른 글
vsCODE git 에러 - Please clean your repository working tree before checkout (0) | 2023.11.10 |
---|---|
git 기초 - window cmd 활용하기 (0) | 2023.11.06 |