mxivideo/examples/user_postgres_example.py

70 lines
1.9 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 python3
"""
UserTablePostgres 使用示例
"""
import sys
import os
sys.path.append(os.path.dirname(os.path.dirname(os.path.abspath(__file__))))
from python_core.database.user_postgres import UserTablePostgres
def main():
"""主函数"""
print("=== UserTablePostgres 使用示例 ===\n")
# 创建用户表实例
user_table = UserTablePostgres()
print("✅ 用户表初始化成功")
# 创建用户
print("\n1. 创建用户...")
user_id = user_table.create_user(
username="john_doe",
email="john@example.com",
password="secure_password_123",
display_name="John Doe"
)
print(f"用户创建成功ID: {user_id}")
# 获取用户信息
print("\n2. 获取用户信息...")
user = user_table.get_user_by_username("john_doe")
print(f"用户信息: {user['display_name']} ({user['email']})")
# 用户认证
print("\n3. 用户认证...")
auth_result = user_table.authenticate_user("john_doe", "secure_password_123")
if auth_result:
print(f"认证成功: {auth_result['username']}")
else:
print("认证失败")
# 更新用户信息
print("\n4. 更新用户信息...")
user_table.update_user(user_id, {
"display_name": "John Smith",
"avatar_url": "https://example.com/avatar.jpg"
})
print("用户信息更新成功")
# 获取用户统计
print("\n5. 用户统计...")
user_count = user_table.get_user_count()
print(f"总用户数: {user_count}")
# 搜索用户
print("\n6. 搜索用户...")
search_results = user_table.search_users("john")
print(f"搜索到 {len(search_results)} 个用户")
# 清理:删除测试用户
print("\n7. 清理测试数据...")
user_table.delete_user(user_id)
print("测试用户已删除")
print("\n✅ 示例完成")
if __name__ == "__main__":
main()