test
This commit is contained in:
32
test_database.py
Normal file
32
test_database.py
Normal file
@@ -0,0 +1,32 @@
|
|||||||
|
import logging
|
||||||
|
from sqlalchemy import create_engine
|
||||||
|
from sqlalchemy.orm import sessionmaker
|
||||||
|
from app.models import Domain, Mailbox # 根据你的项目结构调整导入
|
||||||
|
|
||||||
|
# 配置数据库连接
|
||||||
|
DATABASE_URL = "sqlite:///email_system.db" # 使用默认的 SQLite 数据库
|
||||||
|
engine = create_engine(DATABASE_URL)
|
||||||
|
Session = sessionmaker(bind=engine)
|
||||||
|
|
||||||
|
def test_database_queries():
|
||||||
|
session = Session()
|
||||||
|
try:
|
||||||
|
# 查询所有域名
|
||||||
|
domains = session.query(Domain).all()
|
||||||
|
print("所有域名:")
|
||||||
|
for domain in domains:
|
||||||
|
print(f"域名: {domain.name}, 描述: {domain.description}")
|
||||||
|
|
||||||
|
# 查询所有邮箱
|
||||||
|
mailboxes = session.query(Mailbox).all()
|
||||||
|
print("\n所有邮箱:")
|
||||||
|
for mailbox in mailboxes:
|
||||||
|
print(f"邮箱: {mailbox.address}, 域名ID: {mailbox.domain_id}, 描述: {mailbox.description}")
|
||||||
|
|
||||||
|
except Exception as e:
|
||||||
|
logging.error(f"查询数据库时出错: {str(e)}")
|
||||||
|
finally:
|
||||||
|
session.close()
|
||||||
|
|
||||||
|
if __name__ == "__main__":
|
||||||
|
test_database_queries()
|
||||||
Reference in New Issue
Block a user