최대 1 분 소요

DB 연동시 where절 패턴

  • where절 패턴 1
param1 = (2,) 
cursor.execute("select * from user where id = ?", param1)
print('param1 : ', cursor.fetchone())

 

현재 위치에서는 더이상 레코드가 없음. 빈리스트 반환

print('param1 :', cursor.fetchall())

 

  • where 절 패턴 2 ```python param2 = 3 cur.execute(‘select * from user where id=”%s”’ % param2) #%s, %f, %d cur.execute(f’select * from user where id={param2}’)

print(‘param2 : ‘, cursor.fetchone()) print(‘param2 : ‘, cursor.fetchall())


   

- where 절 패턴3
```python
cursor.execute('select * from user where id=:id', {'id': 4})
print('param3 : ', cursor.fetchone())
print('param3 : ', cursor.fetchall())

 

  • where절 패턴 4
param4 = (2,5)
cursor.execute('select * from user where id in(?,?)', param4)
print('param 4 : ', cursor.fetchall())

 

  • where 절 패턴5
    cursor.execute("select * from user where id in('%d', '%d')" %(3,4))
    print('param5 : ', cursor.fetchall())
    

 

  • where 절 패턴6
cursor.execute('select * from user where id=:id1 or id=:id2', {'id1':2, 'id2':5})
print('param6 :', cursor.fetchall())

카테고리:

업데이트:

댓글남기기