Kamixitong/check_db.py
2025-11-13 16:51:51 +08:00

40 lines
1.0 KiB
Python
Raw Blame History

This file contains ambiguous Unicode characters

This file contains Unicode characters that might be confused with other characters. If you think that this is intentional, you can safely ignore this warning. Use the Escape button to reveal them.

#!/usr/bin/env python
# -*- coding: utf-8 -*-
"""
检查数据库中的产品数据
"""
import os
import sys
# 添加项目根目录到Python路径
sys.path.insert(0, os.path.dirname(os.path.abspath(__file__)))
# 尝试加载.env文件
try:
from dotenv import load_dotenv
if load_dotenv():
print("成功加载.env文件")
else:
print("未找到或无法加载.env文件")
except ImportError:
print("python-dotenv未安装跳过.env文件加载")
from app import create_app, db
from app.models import Product
# 创建应用实例
app = create_app()
with app.app_context():
print("数据库URI:", app.config['SQLALCHEMY_DATABASE_URI'])
total_products = Product.query.count()
print(f"产品总数: {total_products}")
if total_products > 0:
print("产品列表:")
products = Product.query.all()
for product in products:
print(f" - ID: {product.product_id}, 名称: {product.product_name}, 状态: {product.status}")
else:
print("数据库中没有产品数据")