控制网复测平面基准归算程序(包含控制网复测平面基准计算,平面控制网稳定性计算,水准测段高差稳定计算三个程序功能)
You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

database_operations.py 3.1KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687
  1. #selectedCode:D:\Code\ControlNetwork\UI\Front\back\Program_Run\database_operations.py#L1-L83
  2. # database_operations.py
  3. import sqlite3
  4. import os
  5. import sys
  6. import logging
  7. from PySide6.QtWidgets import QMessageBox
  8. from .utils import resource_path # 从 utils.py 导入 resource_path
  9. # 配置日志
  10. def setup_logging():
  11. # 获取日志文件路径
  12. log_folder = os.path.join(os.getcwd(), 'logs')
  13. if not os.path.exists(log_folder):
  14. os.makedirs(log_folder)
  15. log_file = os.path.join(log_folder, 'app.log')
  16. # 配置日志
  17. logging.basicConfig(
  18. level=logging.DEBUG, # 设置日志级别
  19. format='%(asctime)s - %(levelname)s - %(message)s', # 日志格式
  20. handlers=[
  21. logging.FileHandler(log_file), # 输出到文件
  22. logging.StreamHandler() # 输出到控制台(可选)
  23. ]
  24. )
  25. # 获取 SQL 文件夹的路径
  26. def get_sql_folder():
  27. """获取 SQL 文件夹的路径,支持开发和打包环境"""
  28. return resource_path('SQL') # 使用 resource_path 获取 SQL 文件夹路径
  29. def create_database_and_tables(main_window, ui):
  30. # 设置日志
  31. setup_logging()
  32. db_name = main_window.ui.lineEdit.text().strip()
  33. if not db_name:
  34. QMessageBox.warning(main_window, "警告", "请输入数据库名称")
  35. return
  36. # 获取 SQL 文件夹路径
  37. sql_folder = get_sql_folder()
  38. # 构建数据库文件路径
  39. db_path = os.path.join(sql_folder, f"{db_name}.db")
  40. # 记录数据库路径
  41. logging.info(f"数据库路径: {db_path}")
  42. # 检查数据库文件是否已经存在
  43. if os.path.exists(db_path):
  44. QMessageBox.warning(main_window, "警告", "当前项目名重复,请重新设置项目名,或者删除旧项目再创建此项目")
  45. return
  46. # 获取现有数据库结构文件的路径
  47. existing_db_path = os.path.join(sql_folder, 'DataBase.db')
  48. if not os.path.exists(existing_db_path):
  49. QMessageBox.critical(main_window, "错误", "找不到现有的数据库结构文件 DataBase.db")
  50. return
  51. try:
  52. # 连接到现有数据库以获取表结构
  53. with sqlite3.connect(existing_db_path) as existing_conn:
  54. existing_cursor = existing_conn.cursor()
  55. existing_cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
  56. tables = [row[0] for row in existing_cursor.fetchall()]
  57. # 创建新的数据库
  58. with sqlite3.connect(db_path) as new_conn:
  59. new_cursor = new_conn.cursor()
  60. for table in tables:
  61. # 获取表的创建语句
  62. existing_cursor.execute(f"SELECT sql FROM sqlite_master WHERE type='table' AND name='{table}';")
  63. create_table_sql = existing_cursor.fetchone()[0]
  64. new_cursor.execute(create_table_sql)
  65. new_conn.commit()
  66. QMessageBox.information(main_window, "成功", f"项目{db_name}已成功创建")
  67. except Exception as e:
  68. logging.error(f"创建项目时出错: {str(e)}", exc_info=True)
  69. QMessageBox.critical(main_window, "错误", f"创建项目时出错: {str(e)}")