def table_def(table_name):
import cx_Oracle
import pandas as pd
dsn = cx_Oracle.makedsn('localhost', 1521, 'xe')
db = cx_Oracle.connect('c##scott', 'tiger', dsn)
cursor = db.cursor()
query = f"""SELECT A.COLUMN_ID AS "NO"
, B.COMMENTS AS "논리명"
, A.COLUMN_NAME AS "물리명"
, A.DATA_TYPE AS "자료 형태"
, A.DATA_LENGTH AS "길이"
, DECODE(A.NULLABLE, 'N', 'No', 'Y', 'Yes') AS "Null 허용"
, A.DATA_DEFAULT AS "기본값"
, B.COMMENTS AS "코멘트"
FROM ALL_TAB_COLUMNS A
LEFT JOIN ALL_COL_COMMENTS B
ON A.OWNER = B.OWNER
AND A.TABLE_NAME = B.TABLE_NAME
AND A.COLUMN_NAME = B.COLUMN_NAME
WHERE A.TABLE_NAME = '{table_name}' AND A.OWNER='C##SCOTT'
ORDER BY A.COLUMN_ID"""
cursor.execute(query)
row = cursor.fetchall()
colname = cursor.description
col = []
for i in colname:
col.append(i[0].lower()) #append 시키기
df = pd.DataFrame(row, columns=col)
return df
table_list = []
cursor.execute(""" select table_name
from dba_tables
where owner in ('C##SCOTT', 'HR', 'SH', 'OE')
""")
row = cursor.fetchall()
for i in row:
table_list.append(i[0])
table_list.sort()
for table_name in table_list:
df = table_def(table_name)
output_path = f"c:\\oracle_execl\\{table_name}_df.xlsx" # 예시: 테이블 이름에 따라 파일 경로 설정
df.to_excel(output_path)
print("Excel 파일 생성이 완료되었습니다.")