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
- 지도시각화
- github2FA
- wordembedding
- VScodeNotResponding
- Cast
- session-basedRecommendation
- DIF_SR
- Python
- numpy.bool
- MySQL
- Convert
- jsonlines
- ExplicitData
- iterrows
- json
- MatrixFactorization
- LatentFactorModel
- 텐서플로자격증
- pandas
- BloombergMarketConcepts
- TensorflowDeveloperCertificate
- decimal error
- sshtunnel
- Visualization
- implicitData
- Colab
- str.replace
- vscode
- jsonl
- gluonnlp
Archives
- Today
- Total
garret
[Python, MySQL] Python에서 DB 접속해 Data 다운받기 1 본문
DB에 있는 데이터를 python으로 불러와서 바로 작업(ML,DL 등등)하고 싶을 때 할 수 있는 방법.
해당 작업은 Vscode의 주피터 파일을 열어 진행하였다.
pymysql 라이브러리 설치
!pip install pymysql
필요한 라이브러리 임포트
import pymysql.cursors
import pandas as pd
DB 접속
# DB 접속
connection = pymysql.connect(host='호스트명',
port=포트번호 ,
db='db명',
user='user명',
password='pw입력',
charset='utf8',
cursorclass= pymysql.cursors.DictCursor)
참고로 포트번호는 int 타입으로 넣어야 한다.
데이터 Dataframe 형식으로 다운로드
cursor = connection.cursor()
# 데이터를 불러오기 위한 SQL문
sql ="select * from 스키마명.테이블명"
# 데이터 읽는 과정
cursor.execute(sql)
result = cursor.fetchall()
# DataFrame으로 변환
df = pd.DataFrame(result)
df.head()
sql에서 쿼리문을 작성하면 쿼리문 형식에 맞는 데이터를 불러올 수 있다.
'Programming > Python' 카테고리의 다른 글
[Python] sys.stdout.write()와 print() 차이 (0) | 2023.05.23 |
---|---|
[Python] sys.stdin.readline() (0) | 2023.05.17 |
[Python] JSON 파일 만들기 (0) | 2023.03.17 |
[Python] Pandas iterrows(), items() (0) | 2023.03.17 |
[Python, MySQL] Python에서 DB 접속해 Data 다운받기 2 (0) | 2023.02.15 |