
λ°μ΄ν°λ² μ΄μ€λ₯Ό λ€λ£¨κΈ° μμνλ©΄ κ°μ λ¨μ΄λ€μ΄ κ³μ λ°λ³΅λλ€.
μλ―Έλ₯Ό μ λ§€νκ² μκ³ λμ΄κ°λ©΄, μ΄ν μ½λκ° μ λΆ ν·κ°λ¦¬κΈ° μμνλ€.
SELECTλ μμ΄ λ¨μ΄ select(μ ννλ€)μμ μλ€.
λ°μ΄ν°λ² μ΄μ€ ν
μ΄λΈμ μ μ₯λ μλ§μ λ°μ΄ν° μ€μμ
νμν κ²λ§ 골λΌμ κ°μ Έμ€λ λͺ
λ Ήμ΄λ€.
[ users ν μ΄λΈ ] -------------------------- | id | name | email | age| -------------------------- | 1 | μ² μ | ... | 25 | | 2 | μν¬ | ... | 28 | | 3 | νμ΄ | ... | 22 | -------------------------- SELECT * FROM users WHERE age >= 25
λ§νΈ μ§μ΄λμμ νμν μνλ§ μ₯λ°κ΅¬λμ λ΄λ κ²κ³Ό κ°λ€.
cursorλ λΌν΄μ΄ currere (λ¬λ¦¬λ€)μμ μ λνλ€.
μλ―Έλ₯Ό ν λ¬Έμ₯μΌλ‘ μ 리νλ©΄ μ΄λ λ€.
컀μ = 쿼리 κ²°κ³Όμμ βμ§κΈ μ΄λλ₯Ό μ½κ³ μλμ§βλ₯Ό κ°λ¦¬ν€λ ν¬μΈν°
쿼리 κ²°κ³Ό ------------------ [1] μ² μ, 25 β cursor [2] μν¬, 28 [3] νμ΄, 22
λ¬Έμ νΈμ§κΈ°μμ κΉλΉ‘μ΄λ 컀μκ° νμ¬ μ
λ ₯ μμΉλ₯Ό κ°λ¦¬ν€λ―,
DB 컀μλ κ²°κ³Ό μ§ν©μμ νμ¬ μ½μ μμΉλ₯Ό κΈ°μ΅νλ€.
fetchλ μμ΄λ‘ κ°μ Έμ€λ€λΌλ λ»μ΄λ€.
컀μκ° βμμΉβλΌλ©΄, fetchλ μ€μ λ°μ΄ν°λ₯Ό μμ μ₯λ νμλ€.
1οΈβ£ cursor β μ± μμΉλ₯Ό κ°λ¦¬ν΄ 2οΈβ£ fetch β μ± μ μ€μ λ‘ κΊΌλ΄μ΄
컀μ μμ΄ fetchλ μ‘΄μ¬ν μ μλ€.
(μμΉλ λͺ¨λ₯΄λλ° λ°μ΄ν°λ₯Ό κ°μ Έμ¬ μλ μκΈ° λλ¬Έ)
μ΄μ Python μ½λλ‘ μ§μ SELECTλ₯Ό μ€νν΄λ³΄μ
import pymysql
# 1. λ°μ΄ν°λ² μ΄μ€ μ°κ²°
conn = pymysql.connect(
host="μλ²μ£Όμ",
user="μ¬μ©μλͺ
",
password="λΉλ°λ²νΈ",
db="λ°μ΄ν°λ² μ΄μ€λͺ
",
charset="utf8mb4"
)
# 2. 컀μ μμ±
cursor = conn.cursor()
# 3. SELECT μ€ν
cursor.execute("SELECT * FROM demo.users")
# 4. κ²°κ³Ό μΆλ ₯
for row in cursor.fetchall():
print(row)
# 5. μμ μ 리
cursor.close()
conn.close()
μ€ν κ²°κ³Όλ μ΄λ κ² λμ¨λ€.
(1, 'μ² μ', 'chulsoo@example.com', 25)
(2, 'μν¬', 'younghee@example.com', 28)
(3, 'νμ΄', 'hooni@example.com', 22)
κ° ν(row)μ νν(tuple) ννλ‘ λ°νλλ€.
[ Python μ½λ ]
β
βΌ
1. pymysql.connect() β DB μ°κ²°
β
βΌ
2. conn.cursor() β 컀μ μμ±
β
βΌ
3. cursor.execute() β SELECT μ λ¬
β
βΌ
4. DB κ²°κ³Ό μμ±
β
βΌ
5. cursor.fetch() β κ²°κ³Όλ₯Ό PythonμΌλ‘ κ°μ Έμ΄
SELECTλ₯Ό μ€ννλ€κ³ ν΄μ λ°μ΄ν°κ° λ°λ‘ PythonμΌλ‘ λμ΄μ€μ§λ μλλ€.
컀μλ κ²°κ³Όλ₯Ό λ€κ³ μκ³ , fetchκ° λ°μ΄ν°λ₯Ό κΊΌλΈλ€.
fetch λ°©μμ μ΄ μΈ κ°μ§λ€. μν©μ λ§κ² μ ννμ§ μμΌλ©΄ λ©λͺ¨λ¦¬ ννμ λ§λλ€.
ββββββββββββββ¬βββββββββββββββββββββββββββββββ¬ββββββββββββββββββββββ β λ©μλ β μλ―Έ β λ°ν νν β ββββββββββββββΌβββββββββββββββββββββββββββββββΌββββββββββββββββββββββ€ β fetchone() β 1νλ§ κ°μ Έμ€κΈ° β νν 1κ° / None β β fetchall() β μ λΆ κ°μ Έμ€κΈ° β νν 리μ€νΈ β β fetchmany β nκ°μ© λλ μ κ°μ Έμ€κΈ° β νν 리μ€νΈ β ββββββββββββββ΄βββββββββββββββββββββββββββββββ΄ββββββββββββββββββββββ
κ²°κ³Όκ° 1건μμ΄ λ³΄μ₯λ λ μ¬μ©νλ λ©μλλ€.
μ£Όλ‘ PK(id) μ‘°νμ μ¬μ©νλ€.
cursor.execute("SELECT * FROM users WHERE id = 1")
row = cursor.fetchone()
if row:
print(row)
else:
print("κ²°κ³Ό μμ")
(1, 'μ² μ', 'chulsoo@example.com', 25)
κ²°κ³Όκ° μμΌλ©΄ Noneμ λ°ννλ€.
μμΈκ° μλλΌ Noneμ΄λΌλ μ μ΄ μ€μνλ€.
쑰건μ λ§λ λͺ¨λ νμ ν λ²μ λ©λͺ¨λ¦¬λ‘ κ°μ Έμ¨λ€.
cursor.execute("SELECT * FROM users")
rows = cursor.fetchall()
print(f"μ΄ {len(rows)}건")
for row in rows:
print(row)
β οΈ λ°μ΄ν°κ° λ§μμλ‘ μννλ€
[ DB κ²°κ³Ό 100λ§ κ±΄ ]
β
fetchall()
β
Python λ©λͺ¨λ¦¬ 100λ§ κ±΄ μ μ¬ π₯
κ΄λ¦¬μ νμ΄μ§, ν μ€νΈ λ°μ΄ν°μ²λΌ ν μκ° μ λ€λ κ² νμ€ν λλ§ μ¬μ©νλ€.
λμ©λ λ°μ΄ν°λ₯Ό λ€λ£° λ μ¬μ©νλ λ©μλλ€.
κ²°κ³Όλ₯Ό μ‘°κ° λ¨μλ‘ λλ μ μ²λ¦¬νλ€.
cursor.execute("SELECT * FROM users")
while True:
rows = cursor.fetchmany(2)
if not rows:
break
for row in rows:
print(row)
[1] μ² μ [2] μν¬ --- [3] νμ΄ [4] λ―Όμ ---
λ‘κ·Έ μ²λ¦¬, λ°°μΉ μμ , ν΅κ³ μ§κ³μμ νμμ μΌλ‘ μ¬μ©λ¨.
κΈ°λ³Έ 컀μλ κ²°κ³Όλ₯Ό νν(tuple)λ‘ λ°ννλ€.
row = (1, 'μ² μ', 'chulsoo@example.com', 25)
print(row[1]) # μ² μ
μ΄ μ½λμ λ¬Έμ λ λκΉ?
row[1] β 1μ΄ λ μλ―Ένλμ§ μ½λλ§ λ³΄κ³ μ μ μμ
μ»¬λΌ μμλ₯Ό μΈμμΌ νκ³ ,
컬λΌμ΄ νλλΌλ λ°λλ©΄ μ λΆ μμ ν΄μΌ νλ€.
DictCursorλ₯Ό μ¬μ©νλ©΄ κ²°κ³Όλ₯Ό λμ λλ¦¬λ‘ λ°λλ€.
from pymysql.cursors import DictCursor
conn = pymysql.connect(
host="μλ²μ£Όμ",
user="μ¬μ©μλͺ
",
password="λΉλ°λ²νΈ",
db="λ°μ΄ν°λ² μ΄μ€λͺ
",
charset="utf8mb4",
cursorclass=DictCursor
)
cursor = conn.cursor()
cursor.execute("SELECT * FROM users")
for row in cursor.fetchall():
print(f"{row['name']} ({row['email']})")
μΆλ ₯ μμ:
μ² μ (chulsoo@example.com)
μν¬ (younghee@example.com)
νμ΄ (hooni@example.com)
ββββββββββββββββ¬ββββββββββββββββ β λ°©μ β μ κ·Ό λ°©λ² β ββββββββββββββββΌββββββββββββββββ€ β νν β row[1] β β DictCursor β row['name'] β ββββββββββββββββ΄ββββββββββββββββ
π μ€λ¬΄μμλ DictCursorκ° μ¬μ€μ νμ€μ΄λ€
SQLμ λ¬Έμμ΄λ‘ μ‘°ν©νλ μκ°, 보μμ΄ λ¬΄λμ§λ€.
# β μ λ μ°μ§ λ§ κ²
name = "μ² μ"
cursor.execute(f"SELECT * FROM users WHERE name = '{name}'")
μ λ ₯κ°: '; DROP TABLE users; --
μ€ν SQL: SELECT * FROM users WHERE name = ''; DROP TABLE users;
name = "μ² μ"
cursor.execute(
"SELECT * FROM users WHERE name = %s",
(name,)
)
pymysqlμ΄ μλμΌλ‘ μ΄μ€μΌμ΄ν μ²λ¦¬νλ€.SQL ꡬ쑰μ κ°μ΄ λΆλ¦¬λλ€.
βββββββββββββββ¬ββββββββββββββββββββββββββ β λ°©μ β μμ μ± β βββββββββββββββΌββββββββββββββββββββββββββ€ β λ¬Έμμ΄ μ‘°ν© β β Injection μν β β %s λ°μΈλ© β β μμ β βββββββββββββββ΄ββββββββββββββββββββββββββ
μ€λ¬΄μμ DB μ½λλ₯Ό κ·Έλλ‘ μ°λ μΌμ μλ€.
λ°λμ ν¨μλ‘ κ°μΌλ€.
def get_user_by_id(user_id):
conn = get_connection()
cursor = conn.cursor()
cursor.execute(
"SELECT * FROM users WHERE id = %s",
(user_id,)
)
user = cursor.fetchone()
cursor.close()
conn.close()
return user
μ§κΈκΉμ§ λ°°μ΄ SELECT κ°λ
μ μ΄μ©ν΄μ
μ€μ μλΉμ€μμ λ°λ‘ μ°λ ν¨μ ννλ‘ μ 리νλ€.
[ μ΄λ² μ€μ΅μμ νλ ¨νλ κ² ] - SELECT 쑰건 κ²μ - fetch λ©μλ μ ν κΈ°μ€ - κ²°κ³Όκ° μμ λμ μ²λ¦¬ λ°©μ - DictCursor + %s λ°μΈλ© μ΅κ΄ν
λ¨Όμ λͺ¨λ ν¨μμμ μ¬μ©ν DB μ°κ²° ν¨μλ₯Ό μ€λΉνλ€.
import pymysql
from pymysql.cursors import DictCursor
def get_connection():
return pymysql.connect(
host="μλ²μ£Όμ",
user="μ¬μ©μλͺ
",
password="λΉλ°λ²νΈ",
db="λ°μ΄ν°λ² μ΄μ€λͺ
",
charset="utf8mb4",
cursorclass=DictCursor
)
βββββββββββββββββββββββββββββ β get_connection() μν β βββββββββββββββββββββββββββββ€ β - DB μ°κ²° μμ± β β - DictCursor κ°μ β β - μ°κ²° μ€μ μ€μ κ΄λ¦¬ β βββββββββββββββββββββββββββββ
쑰건 κ²μ + μ¬λ¬ ν λ°ν λ¬Έμ λ€.
κ²°κ³Όλ μ¬λ¬ κ±΄μΌ μ μμΌλ―λ‘ fetchall()μ μ¬μ©νλ€.
def get_users_by_age_range(min_age, max_age):
conn = get_connection()
cursor = conn.cursor()
cursor.execute(
"SELECT * FROM users WHERE age BETWEEN %s AND %s",
(min_age, max_age)
)
users = cursor.fetchall()
cursor.close()
conn.close()
return users
[ μ¬κ³ νλ¦ ] - κ²°κ³Όκ° 0κ±΄μΌ μλ μμ - fetchall()μ κ²°κ³Ό μμΌλ©΄ [] - νΈμΆνλ μͺ½μμ len(users)λ‘ νλ¨ κ°λ₯
μ¬μ© μμ
users = get_users_by_age_range(20, 30)
if not users:
print("ν΄λΉ λμ΄λ μ¬μ©μ μμ")
else:
for user in users:
print(user["name"], user["age"])
μ΄λ©μΌμ μ λν¬ κ°μ΄λ€.
κ²°κ³Όλ 0 λλ 1건 β fetchone()μ΄ μ λ΅
def get_user_by_email(email):
conn = get_connection()
cursor = conn.cursor()
cursor.execute(
"SELECT * FROM users WHERE email = %s",
(email,)
)
user = cursor.fetchone()
cursor.close()
conn.close()
return user
[ ν¬μΈνΈ ] - κ²°κ³Ό μμ β None - μμΈ X, None λ°ν - νΈμΆλΆμμ λΆκΈ° μ²λ¦¬
μ¬μ© μμ
user = get_user_by_email("chulsoo@example.com")
if user is None:
print("ν΄λΉ μ΄λ©μΌ μ¬μ©μ μμ")
else:
print(user["name"], user["email"])
COUNTλ νμ κ²°κ³Όκ° 1νμ΄λ€.<
λ°λΌμ fetchone()μ μ¬μ©νλ€.
def count_users():
conn = get_connection()
cursor = conn.cursor()
cursor.execute("SELECT COUNT(*) AS cnt FROM users")
result = cursor.fetchone()
cursor.close()
conn.close()
return result["cnt"]
βββββββββββββββββββββββββββββ β COUNT 쿼리 νΉμ§ β βββββββββββββββββββββββββββββ€ β - κ²°κ³Όλ 무쑰건 1ν β β - κ°μ΄ 0μΌ μλ μμ β β - None μλ β βββββββββββββββββββββββββββββ
μ¬μ© μμ
total = count_users()
print(f"μ 체 μ¬μ©μ μ: {total}")
βββββββββββββββββββββββββββββ¬βββββββββββββββββββββ β μν© β λ°ν κ° β βββββββββββββββββββββββββββββΌβββββββββββββββββββββ€ β fetchone κ²°κ³Ό μμ β None β β fetchall κ²°κ³Ό μμ β [] (λΉ λ¦¬μ€νΈ) β β COUNT(*) β 0 β βββββββββββββββββββββββββββββ΄βββββββββββββββββββββ
μμΈλ₯Ό λμ§μ§, Noneμ λ°νν μ§λ λΉμ¦λμ€ μꡬμ¬νμ λ°λΌ κ²°μ νλ€.
β λͺ¨λ 쿼리λ %s λ°μΈλ© μ¬μ©
β μ¬μ©μ μ
λ ₯ μ§μ SQLμ μ½μ
X
β DictCursor μ¬μ©μΌλ‘ κ°λ
μ± ν보
β fetchone β λ¨κ±΄ μ‘°ν
β fetchall β μλ λ°μ΄ν°
β fetchmany β λμ©λ λ°μ΄ν°
β DictCursor β μ€λ¬΄ νμ
β %s λ°μΈλ© β 보μ νμ