控制网复测平面基准归算程序(包含控制网复测平面基准计算,平面控制网稳定性计算,水准测段高差稳定计算三个程序功能)
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 2.3KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354
  1. # database_operations.py 数据库操作相关代码,主要用于展示数据库名字
  2. import sqlite3
  3. import os
  4. from PySide6.QtWidgets import QMessageBox
  5. def create_database_and_tables(main_window,ui):
  6. db_name = main_window.ui.lineEdit.text().strip()
  7. if not db_name:
  8. QMessageBox.warning(main_window, "警告", "请输入数据库名称")
  9. return
  10. # 转换为UTF-8编码
  11. # db_name_utf8 = db_name.encode('utf-8')
  12. # 获取当前脚本所在的目录
  13. current_dir = os.path.dirname(os.path.abspath(__file__))
  14. # 构建 SQL 文件夹的相对路径(上一级的上一级中的 SQL 文件夹)
  15. sql_folder = os.path.join(current_dir, '..', '..', 'SQL')
  16. # 将相对路径转换为绝对路径
  17. sql_folder = os.path.abspath(sql_folder)
  18. # 确保 SQL 文件夹存在
  19. if not os.path.exists(sql_folder):
  20. os.makedirs(sql_folder)
  21. # 构建数据库文件的完整路径
  22. db_path = os.path.join(sql_folder, f"{db_name}.db")
  23. # 读取现有的数据库结构
  24. existing_db_path = os.path.join(sql_folder, 'DataBase.db')
  25. if not os.path.exists(existing_db_path):
  26. QMessageBox.critical(main_window.main_window, "错误", "找不到现有的数据库结构文件 DataBase.db")
  27. return
  28. try:
  29. # 连接到现有的数据库以获取表结构
  30. with sqlite3.connect(existing_db_path) as existing_conn:
  31. existing_cursor = existing_conn.cursor()
  32. existing_cursor.execute("SELECT name FROM sqlite_master WHERE type='table';")
  33. tables = [row[0] for row in existing_cursor.fetchall()]
  34. # 创建新的数据库
  35. with sqlite3.connect(db_path) as new_conn:
  36. new_cursor = new_conn.cursor()
  37. for table in tables:
  38. # 获取表的创建语句
  39. existing_cursor.execute(f"SELECT sql FROM sqlite_master WHERE type='table' AND name='{table}';")
  40. create_table_sql = existing_cursor.fetchone()[0]
  41. new_cursor.execute(create_table_sql)
  42. new_conn.commit()
  43. QMessageBox.information(main_window, "成功", f"项目{db_name}已成功创建")
  44. except Exception as e:
  45. QMessageBox.critical(main_window, "错误", f"创建项目时出错: {str(e)}")