import mysql.connector
import datetime

# MySQL에 연결
conn = mysql.connector.connect(
    host="localhost",       # 또는 '127.0.0.1'
    user="lee2",   # 예: 'root'
    password="Lee12345!",
    database="lee"  # 연결할 DB 이름
)

# 커서 생성
cursor = conn.cursor()

 

#######################################################
#   insert  select  
########################################################

today = datetime.date.today()
cursor.execute("INSERT INTO test (name, title, date) VALUES (%s, %s, %s)", ('newname', 'newname입니다', today))

#  COMMIT 하자! 중요!!
conn.commit()

# 예제 쿼리 실행
cursor.execute("SELECT * FROM test")

# 결과 가져오기
rows = cursor.fetchall()
for row in rows:
    print(row)

 

#######################################################
#   update  select  
########################################################

# 커서 생성
cursor = conn.cursor()

today = datetime.date.today()
cursor.execute("UPDATE test SET title = 'update 테스트입니다' WHERE id = 4 ")

# COMMIT 하자! 중요!!
conn.commit()

# 예제 쿼리 실행
cursor.execute("SELECT * FROM test")

# 결과 가져오기
rows = cursor.fetchall()
for row in rows:
    print(row)
# 연결 종료
cursor.close()
conn.close()

+ Recent posts