data_init.py 2.3 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283848586878889909192939495
  1. import sqlite3
  2. # -----
  3. # 创建数据库
  4. curs = (conn:=sqlite3.connect('db.sqlite')).cursor()
  5. # --------
  6. # 表创建
  7. #
  8. # 创建用户表
  9. curs.execute('''
  10. create table if not exists user (
  11. id integer PRIMARY KEY,
  12. uname text not null,
  13. passwd text not null,
  14. nickname text not null,
  15. token text not null default '',
  16. createtime timestamp not null default current_timestamp,
  17. lastlogin timestamp,
  18. isdelete integer not null default 0
  19. )
  20. ''').fetchall()
  21. # --------
  22. # 设备与区域有映射关系,其中设备最多映射两个区域,区域之间存在上下级关系
  23. #
  24. # 创建设备表
  25. curs.execute('''
  26. create table if not exists device (
  27. id integer PRIMARY KEY,
  28. name text not null,
  29. typo text not null,
  30. area integer not null default 0 -- 0:未分配到区域 其他:所在区域编号
  31. )
  32. ''').fetchall()
  33. # 创建区域表
  34. curs.execute('''
  35. create table if not exists area (
  36. id integer PRIMARY KEY,
  37. name text not null,
  38. sub integer not null -- 0:无上级区域 其他:下级区域编号
  39. )
  40. ''').fetchall()
  41. # 用户-设备映射表
  42. curs.execute('''
  43. create table if not exists map_user_device (
  44. id integer PRIMARY KEY,
  45. did integer not null,
  46. uid integer not null
  47. )
  48. ''').fetchall()
  49. # 区域-设备映射表
  50. curs.execute('''
  51. create table if not exists map_area_device (
  52. id integer PRIMARY KEY,
  53. did integer not null,
  54. aid integer not null
  55. )
  56. ''').fetchall()
  57. # 用户-区域映射表
  58. curs.execute('''
  59. create table if not exists map_user_area (
  60. id integer PRIMARY KEY,
  61. uid integer not null,
  62. aid integer not null
  63. )
  64. ''').fetchall()
  65. curs.execute('''select * from user''').fetchall()
  66. # --------
  67. # 流程处理:分享与转移设备操作
  68. # 创建设备分享表
  69. curs.execute('''
  70. create table if not exists flow_task_share (
  71. id integer PRIMARY KEY,
  72. did integer not null,
  73. typo integer not null,
  74. ticket integer not null, -- 不需要uid, 因为分享不限用户
  75. createtime timestamp not null default current_timestamp,
  76. isdelete integer not null default 0, -- 0:未处理 1:处理完毕 2:超时未处理
  77. unique(ticket)
  78. )
  79. ''').fetchall()
  80. conn.commit()
  81. # -----
  82. # 表内容初始化
  83. #
  84. # 添加用户
  85. curs.execute('''
  86. insert into user (uname,passwd,nickname) values
  87. ('root', 'e10adc3949ba59abbe56e057f20f883e', 'admin'),
  88. ('admin', 'e10adc3949ba59abbe56e057f20f883e', 'test')
  89. ''').fetchall()
  90. conn.commit()
  91. curs.execute('''
  92. select * from flow_task_share
  93. ''').fetchall()