73 lines
2.3 KiB
Python
73 lines
2.3 KiB
Python
#!/usr/bin/env python3
|
||
"""opening_balances テーブルのデータ確認スクリプト"""
|
||
import psycopg
|
||
|
||
print("="*60)
|
||
print("【opening_balances テーブルデータ確認】")
|
||
print("="*60)
|
||
|
||
try:
|
||
conn = psycopg.connect('host=localhost user=postgres password=postgres dbname=accounting')
|
||
cur = conn.cursor()
|
||
|
||
# 1. テーブル自体の存在確認
|
||
cur.execute("""
|
||
SELECT EXISTS (
|
||
SELECT 1 FROM information_schema.tables
|
||
WHERE table_name = 'opening_balances'
|
||
)
|
||
""")
|
||
table_exists = cur.fetchone()[0]
|
||
print(f"\n1️⃣ opening_balances テーブルの存在: {'✓ 存在' if table_exists else '❌ 存在しない'}")
|
||
|
||
if not table_exists:
|
||
print(" → テーブルを作成する必要があります")
|
||
cur.close()
|
||
conn.close()
|
||
exit(1)
|
||
|
||
# 2. 全行数
|
||
cur.execute("SELECT COUNT(*) FROM opening_balances")
|
||
total_count = cur.fetchone()[0]
|
||
print(f"\n2️⃣ 全体の行数: {total_count}")
|
||
|
||
if total_count == 0:
|
||
print(" ⚠️ opening_balances テーブルは空です")
|
||
else:
|
||
# 3. 年度別統計
|
||
cur.execute("""
|
||
SELECT fiscal_year, COUNT(*) as cnt
|
||
FROM opening_balances
|
||
GROUP BY fiscal_year
|
||
ORDER BY fiscal_year DESC
|
||
""")
|
||
print(f"\n3️⃣ 年度別件数:")
|
||
for row in cur.fetchall():
|
||
print(f" - {row[0]}年度: {row[1]}件")
|
||
|
||
# 4. 2025年度のデータ最初の5件
|
||
print(f"\n4️⃣ 2025年度のサンプルデータ(最初の5件):")
|
||
cur.execute("""
|
||
SELECT a.account_code, a.account_name, o.opening_debit, o.opening_credit
|
||
FROM opening_balances o
|
||
JOIN accounts a ON a.account_id = o.account_id
|
||
WHERE o.fiscal_year = 2025
|
||
LIMIT 5
|
||
""")
|
||
rows = cur.fetchall()
|
||
if rows:
|
||
for row in rows:
|
||
print(f" {row[0]:3s} {row[1]:20s} 借方:{row[2]:>12} 貸方:{row[3]:>12}")
|
||
else:
|
||
print(" → 2025年度のデータがありません")
|
||
|
||
cur.close()
|
||
conn.close()
|
||
|
||
print("\n" + "="*60)
|
||
|
||
except Exception as e:
|
||
print(f"\n❌ エラー: {e}")
|
||
import traceback
|
||
traceback.print_exc()
|