취미와 밥줄사이
[Python] MySQL Update Table 본문
테이블 업데이트
- "UPDATE" 문을 사용하여 테이블에 존재하는 records를 업데이트 할 수 있다.
-
# Overwrite the address column from "Valley 345" to "Canyoun 123" import mysql.connector mydb = mysql.connector.connect( host="localhost", user="your username", password="your password", database="mydatabase" ) mycursor = mydb.cursor() sql = "UPDATE customers SET address = "Canyon 123' WHERE address = 'Valley 345'" mycursor.execute(sql) mydb.commit() print(mycursor.rowcount, "record(s) affected")
- mydb.commit() 명령문이 없다면 테이블이 변경되지 않습니다.
- WHERE절은 업데이트해야 하는 레코드를 지정합니다.
- WHERE절을 생략하면 모든 record가 업데이트 됩니다.
-
# Escape values by using the placholder %s method import mysql.connector mydb = mysql.connecotr.connect( host="localhost", user="your username", password="your password", database="mydatabase" ) mycursor = mydb.cursor() sql = "UPDATE customers SET address = %s WHERE address = %s" val = ("Valley 345", "Canyon 123") mycursor.execute(sql, val) mydb.commit() print(mycursor.rowcount, "record(s) affected")
REFERENCE
https://www.w3schools.com/python/python_mysql_update.asp
Python MySQL Update Table
W3Schools offers free online tutorials, references and exercises in all the major languages of the web. Covering popular subjects like HTML, CSS, JavaScript, Python, SQL, Java, and many, many more.
www.w3schools.com
'DB' 카테고리의 다른 글
[Python] MySQL Join (0) | 2021.10.28 |
---|---|
[Python] MySQL Limit (0) | 2021.10.28 |
[Python] MySQL Drop Table (0) | 2021.10.28 |
[Python] MySQL Delete From By (0) | 2021.10.28 |
[Python] MySQL Order By (0) | 2021.10.28 |