1. MariaDB Connector 모듈 설치
pip install mariadb
2. Connection & Transactiion 처리
import mariadb
import sys
try:
conn = mariadb.connect(
user="xxx",
password="xxx",
host="localhost",
port=3306,
database="mysql")
conn.autocommit = False
cursor = conn.cursor()
cursor.execute("SELECT code,codename FROM codemaster WHERE code=?", ('001',))
# Print Result-set
for (first_name, last_name) in cursor:
print(f"Code: {first_name}, Code Name: {last_name}")
# withdraw from account A
# sql_update_query = """Update codemaster set crt_emp = '000030' where codegrp = 'MST001'"""
# cursor.execute(sql_update_query)
# Deposit to account B
sql_update_query = """Update codemaster set crt_emp = 'XXX' where code = '001'"""
cursor.execute(sql_update_query)
print ("Record Updated successfully ")
#Commit your changes
conn.commit()
except mariadb.Error as error :
print("Failed to update record to database rollback: {}".format(error))
#reverting changes because of exception
conn.rollback()
finally:
cursor.close()
conn.close()
print("connection is closed")