Python

[mysql] Python - MySQL Delete From (데이터 삭제)

4302

목차

  1. DELETE FROM - 데이터 삭제
  2. SQL Injection 방지 - %s 자리표시자

 

DELETE FROM - 데이터 삭제


WHERE 절을 이용해 삭제할 데이터를 선택.

주의:  WHERE 절을 생략하면 모든 데이터가 삭제됨.

 


[예제]

 

import mysql.connector


mydb = mysql.connector.connect(

  host="localhost",

  user="root",

  password="autoset",

  database="hz"

)


mycursor = mydb.cursor()

sql = "DELETE FROM hz_member WHERE mb_level = '1'"

mycursor.execute(sql)

mydb.commit()

print(mycursor.rowcount, "record(s) deleted")

 

결과값: 1 record(s) deleted

주의: mydb.commit() 없으면, 변화가 반영 X

 

SQL Injection 방지 - %s 자리표시자

※ DELETE FROM 구문도 %s 자리표시자 메서드로 회피 (escape) 권장.


import mysql.connector


mydb = mysql.connector.connect(

  host="localhost",

  user="root",

  password="autoset",

  database="hz"

)


mycursor = mydb.cursor()

sql = "DELETE FROM hz_member WHERE mb_level = %s"

level = ("2", )

mycursor.execute(sql, level)

mydb.commit()

print(mycursor.rowcount, "record(s) deleted")

 

 

PS.

Node.js DELETE FROM (데이터 삭제)

https://homzzang.com/b/njs-19



분류 제목
mysql Python - MySQL Start (DB 사용)
mysql Python - MySQL Create Database (DB 생성)
mysql Python - MySQL Create Table (테이블 생성)
mysql Python - MySQL Insert Into Table (데이터 삽입)
mysql Python - MySQL Select From (데이터 선택)
mysql Python - MySQL Where (조건절)
mysql Python - MySQL Order By (정렬순서)
mysql Python - MySQL Delete From (데이터 삭제)
mysql Python - MySQL Drop Table (테이블 삭제)
mysql Python - MySQL Update (데이터 수정)
mysql Python - MySQL Limit (데이터 출력개수)
mysql Python - MySQL Join (테이블 결합)
목록
 홈  PC버전 로그인 일본어
그누앞단언어
그누뒷단언어
그외코딩언어
그누보드
제작의뢰
Q&A
커뮤니티 2
웹유틸
회원센터
홈짱닷컴 PC버전 로그인