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
- 핀포인트
- jsonMarshaller
- Ingress Controller Fake
- jar 배포
- ㅉ때
- fake jwt
- pinpoint
- intellij
- RedirectService
- nGinder
- formik
- 논블록킹 성능
- Armeria
- LPOS
- save/update
- 월급루팡 일지
- OIDC
- hbase 저장공간 설정
- pinpoint 2.5.3
- 노드간 통신
- 애자일 싫타
- 플루터
- 개발 어렵당.ㅠ
- UnsupportedOperationException
- R2DBC Paging
- Loki 로그
- 티스토리챌린지
- 오블완
- reids
- 7879
Archives
- Today
- Total
대머리개발자
파일썬 DB 읽기 본문
728x90
AI - RAG좀 해보려니.. 파일썬을 해야 하고.. 파일썬을 보니 막막했는데.
이용 하다 보면 정말 심플하다.
왜 인기가 있는 언어인지..바로 납득이
정말 심플하다 딱 2 덩위로 끝난다.
물론 디펜더시는 설치해야쥬
당근 ORM도 있으나 일단 네이티브 쿼리로 가보자!!
pip install pymysql mysqlclient
1. SQL 커넥터
import pymysql
from contextlib import contextmanager
class MySQLConnector:
def __init__(self, host, port, user, password, db_name):
self.config = {
"host": host,
"port": port,
"user": user,
"password": password,
"database": db_name,
"cursorclass": pymysql.cursors.DictCursor
}
@contextmanager
def get_connection(self):
connection = pymysql.connect(**self.config)
try:
yield connection
finally:
connection.close()
: 데코레이터 기능도 좋네. 심택스도 군더더기 없고
2. SQL 패치
class DataReader:
def __init__(self, db_connector):
self.db_connector = db_connector
def fetch_query(self, query, params=None):
with self.db_connector.get_connection() as connection:
with connection.cursor() as cursor:
cursor.execute(query, params)
result = cursor.fetchall()
return result
## 테스트
from db.DataReader import DataReader
from db.MySQLConnector import MySQLConnector
db = MySQLConnector(
host="localhost",
port=3306,
user="root",
password="111111",
db_name=""
)
reader = DataReader(db)
query = "SELECT id, uid, email FROM oauth2.user WHERE uid=%s"
users = reader.fetch_query(query, params="B0CBD9059116813739DA5B85FE75A26C")
for user in users:
print(f"{user['id']} | {user['uid']} | {user['email']}")
## 결과
문득 JSP 시절.. ResultSet 친구들이 생각난다.
그 시절.. 그 또한 혁신이였지만 세월 앞에 장사없네 ㅋㅋ
그리고 스프링 프레임워크 이런것들이 얼마나 대단한 친구였는지 세삼 다시 알게되었다.
늘 먼저 앞장 서서 한땀 한땀 개발하신 선배님들 존경하고 감사합니다.
728x90