94 lines
2.8 KiB
Python
94 lines
2.8 KiB
Python
|
|
#!/usr/bin/env python
|
||
|
|
# -*- coding: utf-8 -*-
|
||
|
|
"""
|
||
|
|
测试SQL Server数据库连接
|
||
|
|
"""
|
||
|
|
from work_order_query import WORK_ORDER_DB_CONFIG, reload_config
|
||
|
|
|
||
|
|
def test_connection():
|
||
|
|
"""测试数据库连接"""
|
||
|
|
print("=" * 50)
|
||
|
|
print("测试SQL Server数据库连接")
|
||
|
|
print("=" * 50)
|
||
|
|
|
||
|
|
# 重新加载配置
|
||
|
|
reload_config()
|
||
|
|
|
||
|
|
print(f"连接配置:")
|
||
|
|
print(f" 服务器: {WORK_ORDER_DB_CONFIG['host']}:{WORK_ORDER_DB_CONFIG['port']}")
|
||
|
|
print(f" 数据库: {WORK_ORDER_DB_CONFIG['database']}")
|
||
|
|
print(f" 用户名: {WORK_ORDER_DB_CONFIG['username']}")
|
||
|
|
print()
|
||
|
|
|
||
|
|
# 尝试pyodbc
|
||
|
|
try:
|
||
|
|
import pyodbc
|
||
|
|
print("✓ pyodbc已安装")
|
||
|
|
|
||
|
|
# 列出可用的ODBC驱动
|
||
|
|
drivers = pyodbc.drivers()
|
||
|
|
print(f" 可用的ODBC驱动: {drivers}")
|
||
|
|
|
||
|
|
# 查找SQL Server驱动
|
||
|
|
sql_drivers = [d for d in drivers if 'SQL' in d.upper()]
|
||
|
|
if sql_drivers:
|
||
|
|
print(f" 找到SQL Server驱动: {sql_drivers}")
|
||
|
|
else:
|
||
|
|
print(" ⚠ 未找到SQL Server ODBC驱动")
|
||
|
|
except ImportError:
|
||
|
|
print("✗ pyodbc未安装")
|
||
|
|
|
||
|
|
print()
|
||
|
|
|
||
|
|
# 尝试pymssql
|
||
|
|
try:
|
||
|
|
import pymssql
|
||
|
|
print("✓ pymssql已安装")
|
||
|
|
|
||
|
|
# 测试连接
|
||
|
|
try:
|
||
|
|
conn = pymssql.connect(
|
||
|
|
server=WORK_ORDER_DB_CONFIG['host'],
|
||
|
|
port=WORK_ORDER_DB_CONFIG['port'],
|
||
|
|
user=WORK_ORDER_DB_CONFIG['username'],
|
||
|
|
password=WORK_ORDER_DB_CONFIG['password'],
|
||
|
|
database=WORK_ORDER_DB_CONFIG['database']
|
||
|
|
)
|
||
|
|
print("✓ pymssql连接成功!")
|
||
|
|
|
||
|
|
# 测试查询
|
||
|
|
cursor = conn.cursor()
|
||
|
|
cursor.execute("SELECT @@VERSION")
|
||
|
|
row = cursor.fetchone()
|
||
|
|
if row:
|
||
|
|
print(f" SQL Server版本: {row[0][:50]}...")
|
||
|
|
|
||
|
|
cursor.close()
|
||
|
|
conn.close()
|
||
|
|
except Exception as e:
|
||
|
|
print(f"✗ pymssql连接失败: {e}")
|
||
|
|
except ImportError:
|
||
|
|
print("✗ pymssql未安装")
|
||
|
|
|
||
|
|
print()
|
||
|
|
print("测试完成!")
|
||
|
|
print()
|
||
|
|
|
||
|
|
# 测试工单查询
|
||
|
|
test_query = input("是否测试工单查询?(y/n): ")
|
||
|
|
if test_query.lower() == 'y':
|
||
|
|
work_order_no = input("请输入工单号进行测试: ")
|
||
|
|
if work_order_no:
|
||
|
|
from work_order_query import query_work_order
|
||
|
|
result = query_work_order(work_order_no)
|
||
|
|
if result:
|
||
|
|
print("查询成功!")
|
||
|
|
for key, value in result.items():
|
||
|
|
print(f" {key}: {value}")
|
||
|
|
else:
|
||
|
|
print("未找到该工单号的信息")
|
||
|
|
|
||
|
|
if __name__ == "__main__":
|
||
|
|
test_connection()
|
||
|
|
input("\n按Enter键退出...")
|