creat_tbl.py 2.0 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071
  1. #!/usr/bin/env python
  2. # encoding: utf-8
  3. """
  4. @CreateTime: 2022/06/27 09:28
  5. @Author: lxc
  6. @LastEditTime:
  7. @Desctiption:
  8. """
  9. from event_storage import EventStorage
  10. from log import OutPutLog
  11. save_log = OutPutLog()
  12. operate_mysql = EventStorage()
  13. def creatr_table():
  14. # select_all_device_sql = "SELECT DISTINCT(device_name) FROM `data_point_tbl`;"
  15. select_all_device_sql = "SELECT DISTINCT(device_name) FROM `data_point_tbl` WHERE `device_name` LIKE '%_electric';"
  16. all_device = operate_mysql.execute_sql(select_all_device_sql)
  17. for device in all_device:
  18. device_name = device["device_name"]
  19. table_name = "table_" + device_name
  20. create_sql = f"CREATE TABLE `{table_name}` (`id` bigint(20) NOT NULL AUTO_INCREMENT,`times` datetime NOT NULL,"
  21. select_point_sql = f"SELECT serial_number,storage_type FROM `data_point_tbl` WHERE device_name=\'{device_name}\';"
  22. all_point = operate_mysql.execute_sql(select_point_sql)
  23. for point in all_point:
  24. serial_number = "c" + str(point["serial_number"])
  25. storage_type = point["storage_type"]
  26. create_sql = create_sql + f"`{serial_number}` {storage_type} DEFAULT NULL,"
  27. create_sql = create_sql + " `is_send` tinyint(4) NOT NULL DEFAULT '0',PRIMARY KEY (`id`),KEY `times` (`times`)) ENGINE=InnoDB DEFAULT CHARSET=utf8;"
  28. res = operate_mysql.execute_update_sql(create_sql)
  29. print(res, table_name)
  30. # save_log.info(f"{res, device_name}")
  31. # save_log.info(create_sql)
  32. creatr_table()
  33. """
  34. CREATE TABLE `table_dbqycfpffj_electric` (
  35. `id` bigint(20) NOT NULL AUTO_INCREMENT,
  36. `times` datetime NOT NULL,
  37. `c631` float DEFAULT NULL,
  38. `c632` float DEFAULT NULL,
  39. `c633` float DEFAULT NULL,
  40. `c634` float DEFAULT NULL,
  41. `c635` float DEFAULT NULL,
  42. `c636` float DEFAULT NULL,
  43. `c637` float DEFAULT NULL,
  44. `c638` float DEFAULT NULL,
  45. `c639` float DEFAULT NULL,
  46. `c640` float DEFAULT NULL,
  47. `is_send` tinyint(4) NOT NULL DEFAULT '0',
  48. PRIMARY KEY (`id`),
  49. KEY `times` (`times`)
  50. ) ENGINE=InnoDB DEFAULT CHARSET=utf8;
  51. """