본문 바로가기
언어 정리/python_lib,일급함수

pymysql ( + cursor , fetch, )

by 알 수 없는 사용자 2022. 7. 30.

정리 부족 함수가 더 다양함

그냥 이런게 이런식으로 있다 정도만 알고 커서 개념을 쓰려면 다른글이나 내부코드 더 참고 해서 볼것

예를들어 fetchall(), fetchone(), 커서obj.execute( XXXXX) XXX 인자로 다양하게 사용가능함


보기전에 봐두면 좋을 듯 

https://wonhyeok1994.tistory.com/163

 

POOL + Cursor

참조 : https://wintness.tistory.com/93 [펌] Cursor 란??  CURSOR 란?  오라클에서 CURSOR란 시스템 글로벌 영역의 공유 풀 내에 저장공간을 사용하여 사용자가 SQL 문을 실행시키면 결과값을 저장공간에 가지..

wonhyeok1994.tistory.com

SQL 상에서 커서 쓰는 법


키워드

pymysqlpool

ConnectionPool(풀사이즈,풀이름,??)

get_connection()

cursor(저장된공간 cursor_data_type)

execut()   : FETCH 명령인듯

 


원문

config = { '호스트명' : '호스트', '유저' : 'XX', '비번':'XX','디비':'XX' ~~ }  이런식으로 )
pool = pymysqlpool.ConnectionPool(size=self.pool_size, name=self.pool_name, **self.config)

_connection = self.pool.get_connection()
_pconn = _connection

_cursor = _pconn.cursor(pymysql.cursors.DictCursor)

param_dict_request = {}
param_dict_request['param_goal_id'] = 'aa_goal_1'  # 사용할커서메모리data
string_file_str = ""
string_file_str = string_file_str + " SELECT T1._아이디 "
string_file_str = string_file_str + " , T1._정보_1 "
string_file_str = string_file_str + " , T1._목표위치 "
string_file_str = string_file_str + " , T1._위치정보 "
string_file_str = string_file_str + " FROM DB명.TB이름 T1 "
string_file_str = string_file_str + " where T1._아이디 = (SELECT T3._아이디 FROM DB명.TB이름 T3 ) "
string_file_str = string_file_str + " and T1._목표위치 = %(param_goal_id)s "  # 사용할커서메모리data
string_file_str = string_file_str + " order by T1._정렬컬럼 asc "
cursor.execute(string_file_str, param_dict_request)

dict_check = cursor.fetchone()

if _pconn is not None:
    _pconn.close()
if _cursor is not None:
    _cursor.close()
    
return dict_check

 

 

1. Create ConnectionPool 과정

ConnectionPool 생성 ( self.config = { '호스트명' : '호스트', '유저' : 'XX', '비번':'XX','디비':'XX' ~~ }  이런식으로 )
self.pool = pymysqlpool.ConnectionPool(size=self.pool_size, name=self.pool_name, **self.config)

 

 

2. ConnectionPool obj class 획득

생선한 ConnectionPool 가져오기
_connection = self.pool.get_connection()
_pconn = _connection

 

 

3_1. DECLARE CURSOR  : 커서 선언 ( 파이썬 dict타입의 커서공간 선언 ) 

생성한 ConnectionPool 에 명시적 커서 생성 + 파이썬 dict에 맞는 커서 클래스를 선언
_cursor = _pconn.cursor(pymysql.cursors.DictCursor)

dbapi2.pyi 의 코드

3_2. 선언한 커서에 대입할 명령어 구문 생성 후 , cursor 에 data저장 execute! (( OPEN은 내부적으로 알아서 처리됨 ))

cursor.execute(string_file_str, param_dict_request)

param_dict_request = {}
param_dict_request['param_goal_id'] = 'aa_goal_1'  # 사용할커서메모리data

string_file_str = ""

string_file_str = string_file_str + " SELECT T1._아이디 "
string_file_str = string_file_str + " , T1._정보_1 "
string_file_str = string_file_str + " , T1._목표위치 "
string_file_str = string_file_str + " , T1._위치정보 "
string_file_str = string_file_str + " FROM DB명.TB이름 T1 "
string_file_str = string_file_str + " where T1._아이디 = (SELECT T3._아이디 FROM DB명.TB이름 T3 ) "
string_file_str = string_file_str + " and T1._목표위치 = %(param_goal_id)s "  # 사용할커서메모리data
string_file_str = string_file_str + " order by T1._정렬컬럼 asc "

# 커서 실행!
cursor.execute(string_file_str, param_dict_request)

3_3. + cursor.execute( XX ) 추가설명 : 

추가설명: simple 예시
cursor.execute("SELECT * FROM t1 WHERE id = %s", (5,))
이부분을
bb['param_bb'] = 5
aa = "SELECT * FROM t1 WHERE id = %(param_bb)s "
cursor.execute(aa, bb)
이런식으로 쓴것

 

 

4. FETCH                       : FETCH ~ INTO , 커서가 가리키는 곳(cursor_name )의 결과 값을 꺼내옴 ( )

ConnectionPool에 있는 커서메모리의 data들을 FETCH(꺼내옴)
dict_check = cursor.fetchone()

# dict_check = _cursor.fetchall() 이런것도 이따

 

 

5. CLOSE                       : 커서 닫기 ( )

if _pconn is not None:
    _pconn.close()
if _cursor is not None:
    _cursor.close()

 

 

 

 

댓글