티스토리 뷰
먼저 PyMySQL을 설치 한다.
$ pip install PyMySQL
Collecting PyMySQL
Downloading PyMySQL-1.0.2-py3-none-any.whl (43 kB)
|████████████████████████████████| 43 kB 6.2 MB/s
Installing collected packages: PyMySQL
Successfully installed PyMySQL-1.0.2
아래는 PyMySQL을 사용하여 MySQL DB에 데이터를 추가하고 조회하는 예제이다.
import pymysql
try:
conn = pymysql.connect(
host='127.0.0.1',
user='user',
passwd='passwd',
db='dbname',
charset='utf8',
cursorclass=pymysql.cursors.DictCursor)
cursor = conn.cursor()
# 데이터 추가
cursor.execute("""
INSERT INTO users (id, name)
VALUES (%s, %s)
""", ('gdhong', '홍길동'))
conn.commit()
# 데이터 조회
cursor.execute("""
SELECT id, name
FROM users
WHERE id = %s
""", ('gdhong'))
result = cur.fetchall()
finally:
cursor.close()
conn.close()
'프로그래밍 > Python' 카테고리의 다른 글
[Windows10] pipenv 설정 (0) | 2022.01.06 |
---|---|
[python3] postgresql 예제 (0) | 2022.01.04 |
[Python3] 원격 명령 실행 및 파일 다운로드 (0) | 2022.01.04 |
CentOS7에서 psycopg2 설치 오류 해결 (0) | 2021.09.02 |
댓글
공지사항
최근에 올라온 글
최근에 달린 댓글
- Total
- Today
- Yesterday
Blog is powered by
Tistory / Designed by
Tistory