wp-post-automation/package/MariaDB.py

56 lines
1.6 KiB
Python

import mysql.connector
class MariaDB:
def __init__(self, dict):
self.db_host = dict['db_host']
self.db_user = dict['db_user']
self.db_pw = dict['db_pw']
self.db_database = dict['db_database']
def show_config(self):
for key, value in self.__dict__.items():
print(f"{key.upper()}: {value}")
def fetch_data_from_mariadb(self):
try:
# 데이터베이스 연결
connection = mysql.connector.connect(
host=self.db_host,
user=self.db_user,
password=self.db_pw,
database=self.db_database
)
# 커서 생성
cursor = connection.cursor(dictionary=True)
# 쿼리 실행
query = "SELECT * FROM healty_url_source ORDER BY idx DESC LIMIT 1;"
cursor.execute(query)
# 결과 가져오기
result = cursor.fetchone()
return result
except mysql.connector.Error as err:
print(f"Error: {err}")
finally:
if connection.is_connected():
cursor.close()
connection.close()
if __name__ == "__main__":
import GetConfig
config = GetConfig.GetConfig()
# config 잘 가져오는지 확인
config_dict = config.get_config_as_dict()
# MariaDB 테스트
dbg = MariaDB(config_dict)
# dbg.show_config()
url = dbg.fetch_data_from_mariadb()
print(url['url'])