Notice
Recent Posts
Recent Comments
Link
| 일 | 월 | 화 | 수 | 목 | 금 | 토 |
|---|---|---|---|---|---|---|
| 1 | 2 | 3 | 4 | |||
| 5 | 6 | 7 | 8 | 9 | 10 | 11 |
| 12 | 13 | 14 | 15 | 16 | 17 | 18 |
| 19 | 20 | 21 | 22 | 23 | 24 | 25 |
| 26 | 27 | 28 | 29 | 30 |
Tags
- deepstream
- 리눅스
- SQLD
- tao_toolkit
- 네트워크
- 딥러닝
- Python
- 딥스트림
- nouveau
- 도커
- yolov7
- IOU
- 비디오미리보기
- 타오툴킷
- pyMySQL
- Linux
- C
- 파이썬
- 백준
- Spring
- 스프링
- nfs mount
- 정처기
- dkms
- 알고리즘
- 주피터 노트북
- C++
- 도커 컨테이너
- mAP@.5
- docker
Archives
- Today
- Total
한 번만 더 해보자
[Python] pymysql에서 스키마 변경하기 본문
상황
하나의 db connection으로 여러개의 스키마를 변경해서 조회하고 싶다
코드
아래 코드를 이용해서 스키마를 변경할 수 있다
conn.select_db('db_B')
import pymysql
conn = pymysql.connect(
user='user',
passwd='passwd',
host='host',
db='db_A', # 초기 데이터베이스 설정
charset='utf8mb4',
cursorclass=pymysql.cursors.DictCursor,
)
conn.ping(reconnect=True)
cursor = conn.cursor(pymysql.cursors.DictCursor)
# db_A에서 table_A 조회
cursor.execute("SELECT * FROM table_A ")
result_A = cursor.fetchall()
print("Result from db_Adatabase:")
print(result_A)
# 다른 데이터베이스로 전환 (db_B 데이터베이스로 변경)
conn.select_db('db_B')
# db_B 데이터베이스에서 table_B 조회
cursor.execute("SELECT * FROM table_B")
result_B = cursor.fetchall()
print("Result from table_B database:")
print(result_B)
# 연결 종료
cursor.close()
conn.close()
반응형