20250819corp

This commit is contained in:
2025-09-19 16:47:15 +08:00
parent fc5fb355b4
commit b0e8a68204
12 changed files with 1148 additions and 809 deletions
+53 -29
View File
@@ -1,31 +1,55 @@
import sqlite3
# 导入数据库工具
from db_utils import get_db_connection, close_db_connection
# 连接数据库
conn = sqlite3.connect('car_info.db')
cursor = conn.cursor()
# 检查数据库连接是否正常
def check_database_connection():
try:
# 连接数据库
conn = get_db_connection()
cursor = conn.cursor()
# 执行简单的SQL查询来测试连接
cursor.execute("SELECT sqlite_version()")
version = cursor.fetchone()[0]
# 检查是否存在必要的表
necessary_tables = ['users', 'query_records']
tables_info = []
for table in necessary_tables:
cursor.execute("SELECT name FROM sqlite_master WHERE type='table' AND name=?", (table,))
exists = cursor.fetchone() is not None
tables_info.append({"name": table, "exists": exists})
# 关闭数据库连接
close_db_connection(conn)
# 构造返回结果
result = {
"success": True,
"version": version,
"tables": tables_info
}
return result
except Exception as e:
# 确保在异常情况下也关闭连接
if 'conn' in locals() and conn:
close_db_connection(conn)
return {
"success": False,
"error": str(e)
}
# 查询所有表
cursor.execute("SELECT name FROM sqlite_master WHERE type='table'")
tables = cursor.fetchall()
print('数据库中的表:', tables)
# 查询参数配置表
cursor.execute("SELECT sql FROM sqlite_master WHERE type='table' AND name='parameter_config'")
result = cursor.fetchone()
print('参数配置表SQL:', result)
# 如果参数配置表存在,查询其结构
if result:
cursor.execute("PRAGMA table_info(parameter_config)")
columns = cursor.fetchall()
print('参数配置表结构:')
for col in columns:
print(f"字段: {col[1]}, 类型: {col[2]}")
# 查询数据
cursor.execute("SELECT * FROM parameter_config")
data = cursor.fetchall()
print('参数配置数据:', data)
# 关闭连接
conn.close()
# 如果直接运行此脚本,则执行检查操作
if __name__ == "__main__":
result = check_database_connection()
print(f"数据库连接状态: {'成功' if result['success'] else '失败'}")
if result['success']:
print(f"SQLite版本: {result['version']}")
print("表状态:")
for table in result['tables']:
print(f" - {table['name']}: {'存在' if table['exists'] else '不存在'}")
else:
print(f"错误信息: {result['error']}")