python SQLite DB에 있는 Table 출력하기

2024. 1. 18. 00:35python

import sqlite3

# SQLite 데이터베이스에 연결
conn = sqlite3.connect('example.db')

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

# 현재 데이터베이스에 있는 테이블 목록 조회
cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
tables = cursor.fetchall()

# 테이블 목록 출력
print("현재 데이터베이스에 있는 테이블 목록:")
for table in tables:
    print(table[0])

# 연결 종료
conn.close()

내 DB에 저장되어 있는 table을 출력하고 싶을 때 아래와 같이 코드를 작성하면 된다.