파이썬기초103 : 데이터베이스 SELECT, UPDATE, DELETE문
작성자 정보
- 관리자 작성
- 작성일
컨텐츠 정보
- 1,942 조회
- 0 추천
- 목록
본문
import pymysql.cursors
conn = pymysql.connect(host='localhost',
user='root',
password='1234',
db='test',
charset='utf8mb4')
try:
with conn.cursor() as cursor:
sql = 'SELECT * FROM users WHERE email = %s'
cursor.execute(sql, ('test@test.com',))
result = cursor.fetchone()
print(result)
# (1, 'test@test.com', 'my-passwd')
except:
pass
#수정(update)
try:
with conn.cursor() as cursor:
sql = 'UPDATE users SET email = %s WHERE email = %s'
cursor.execute(sql, ('my@test.com', 'test@test.com'))
conn.commit()
print(cursor.rowcount) # 1 (affected rows)
except:
pass
#삭제(delete)
try:
with conn.cursor() as cursor:
sql = 'DELETE FROM users WHERE email = %s'
cursor.execute(sql, ('my@test.com',))
conn.commit()
print(cursor.rowcount) # 1 (affected rows)
finally:
conn.close()
관련자료
-
이전
-
다음