PyMySQL란?

PyMySQL은 Python에서 MySQL 데이터베이스를 사용하기 위한 라이브러리

PyMySQL을 사용하면 Python 코드를 통해 MySQL 데이터베이스에 접속하고, 데이터를 조회, 삽입, 수정, 삭제할 수 있습니다.

PyMySQL documentation — PyMySQL 0.7.2 documentation

https://github.com/PyMySQL/PyMySQL

1. PyMySQL 설치

PyMySQL은 pip를 통해 쉽게 설치할 수 있습니다. 터미널이나 명령 프롬프트에서 다음 명령어를 실행합니다:

pip install pymysql

2. PyMySQL을 사용한 데이터베이스 접속

PyMySQL을 사용하여 MySQL 데이터베이스에 접속하는 기본적인 코드 구조는 다음과 같습니다:

import pymysql

# 데이터베이스 연결 설정
connection = pymysql.connect(host='localhost',
                             user='root',
                             password='oz-password',
                             db='classicmodels',
                             charset='utf8mb4',
                             cursorclass=pymysql.cursors.DictCursor)

try:
    # 커서 생성
    with connection.cursor() as cursor:
        # SQL 쿼리 실행
        sql = "YOUR_SQL_QUERY"
        cursor.execute(sql)

        # 결과 받아오기
        result = cursor.fetchall()
        print(result)

finally:
    # 데이터베이스 연결 종료
    connection.close()