dbman.py 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209
  1. #!/usr/bin/env python3
  2. # -*- coding: utf-8 -*-
  3. """
  4. Created on Thu May 22 20:04:38 2020
  5. Used to create the initial database layout.
  6. @author: Felix Turowsky
  7. /*
  8. *openPilot Log - A FOSS Pilot Logbook Application
  9. *Copyright (C) 2020 Felix Turowsky
  10. *
  11. *This program is free software: you can redistribute it and/or modify
  12. *it under the terms of the GNU General Public License as published by
  13. *the Free Software Foundation, either version 3 of the License, or
  14. *(at your option) any later version.
  15. *
  16. *This program is distributed in the hope that it will be useful,
  17. *but WITHOUT ANY WARRANTY; without even the implied warranty of
  18. *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  19. *GNU General Public License for more details.
  20. *
  21. *You should have received a copy of the GNU General Public License
  22. *along with this program. If not, see <https://www.gnu.org/licenses/>.
  23. */
  24. """
  25. This set of functions is used to initialise or reset the database.
  26. """
  27. import sqlite3
  28. import os
  29. def initialisedb(): # For first run only, creates all databases.
  30. createdbairports()
  31. createdbflights()
  32. def createdbflights(): #this database will hold flight information
  33. conn = sqlite3.connect(os.path.join('./resources', 'flog.db'))
  34. c = conn.cursor()
  35. c.execute('''create table flights (
  36. id integer primary key,
  37. doft numeric not null,
  38. dept text not null,
  39. tofb integer not null,
  40. dest text not null,
  41. tonb integer not null,
  42. tblk integer,
  43. pic integer,
  44. acft integer,
  45. foreign key (pic) references pilots (pilot_id)
  46. foreign key (acft) references tails (tail_id)
  47. )
  48. ''')
  49. conn.close()
  50. def deletedbflights(): # deletes flights database. Use with caution!
  51. conn = sqlite3.connect(os.path.join('./resources', 'flog.db'))
  52. c = conn.cursor()
  53. c.execute("DROP TABLE flights")
  54. print('Table flights has been deleted')
  55. conn.close()
  56. def createdbairports(): # this database will hold airport information
  57. conn = sqlite3.connect(os.path.join('./resources', 'flog.db'))
  58. c = conn.cursor()
  59. c.execute('''create table airports(
  60. airport_id integer primary key,
  61. icao text not null,
  62. iata text,
  63. name text,
  64. lat real,
  65. long real,
  66. country text,
  67. alt integer,
  68. utcoffset integer,
  69. tzolson text)''')
  70. conn.close()
  71. def deletedbairports(): # deletes flights database. Use with caution!
  72. conn = sqlite3.connect(os.path.join('./csv', 'flog.db'))
  73. c = conn.cursor()
  74. c.execute("DROP TABLE airports")
  75. print('Table airports has been deleted')
  76. conn.close()
  77. def createdbpilots(): # this database will hold Pilot names
  78. conn = sqlite3.connect(os.path.join('./csv', 'flog.db'))
  79. c = conn.cursor()
  80. c.execute('''create table pilots(
  81. pilot_id integer primary key,
  82. picfirstname text,
  83. piclastname text not null,
  84. alias text)''')
  85. firstname = input('Please enter your First name: ')
  86. lastname = input('Please enter your Last name: ')
  87. todb = (firstname,lastname,'self')
  88. c.execute("INSERT INTO pilots (\
  89. picfirstname,\
  90. piclastname,\
  91. alias) \
  92. VALUES (?,?,?)", todb)
  93. conn.commit()
  94. conn.close()
  95. def deletedbpilots(): # deletes flights database. Use with caution!
  96. conn = sqlite3.connect(os.path.join('./csv', 'flog.db'))
  97. c = conn.cursor()
  98. c.execute("DROP TABLE pilots")
  99. print('Table pilots has been deleted')
  100. conn.close()
  101. def createdbaircraft():
  102. """
  103. This database will hold AC information. Some triggers
  104. like multiengine or heavy (above 5.7t) should be
  105. Boolean but sqlite does not have a seperate type
  106. definition so 1=True, 0=False. Seperate table needs
  107. to be created for Registrations (Tails)
  108. """
  109. conn = sqlite3.connect(os.path.join('./csv', 'flog.db'))
  110. c = conn.cursor()
  111. c.execute('''create table aircraft(
  112. aircraft_id integer primary key,
  113. Name text,
  114. iata text,
  115. icao text,
  116. multipilot integer,
  117. multiengine integer,
  118. jet integer,
  119. heavy integer)''') #heavy as in above 5.7t
  120. conn.close()
  121. def deletedbaircraft(): # deletes aircraft database. Use with caution!
  122. conn = sqlite3.connect(os.path.join('./csv', 'flog.db'))
  123. c = conn.cursor()
  124. c.execute("DROP TABLE aircraft")
  125. print('Table aircraft has been deleted')
  126. conn.close()
  127. def createdbtails():
  128. """
  129. This table links individual aircraft to an aircraft
  130. type specified in the aircraft table.
  131. """
  132. conn = sqlite3.connect(os.path.join('./csv', 'flog.db'))
  133. c = conn.cursor()
  134. c.execute('''create table tails(
  135. tail_id integer primary key,
  136. Registration text not null,
  137. aircraft_id integer not null,
  138. Company Text,
  139. foreign key (aircraft_id) references aircraft (aircraft_id)
  140. )''') #heavy as in above 5.7t
  141. conn.close()
  142. def deletedbtails(): # deletes extras database. Use with caution!
  143. conn = sqlite3.connect(os.path.join('./csv', 'flog.db'))
  144. c = conn.cursor()
  145. c.execute("DROP TABLE tails")
  146. print('Table tails has been deleted')
  147. conn.close()
  148. def createdbextras():
  149. """
  150. This database will hold additional information like function
  151. times, operational condition time, night flying
  152. time...
  153. """
  154. conn = sqlite3.connect(os.path.join('./csv', 'flog.db'))
  155. c = conn.cursor()
  156. c.execute('''create table extras(
  157. extras_id integer primary key,
  158. PilotFlying integer,
  159. TOday integer,
  160. TOnight integer,
  161. LDGday integer,
  162. LDGnight integer,
  163. autoland integer,
  164. tNight integer,
  165. tIFR integer,
  166. tPIC integer,
  167. tSIC integer,
  168. tDual integer,
  169. tInstructor integer,
  170. tSIM integer,
  171. ApproachType text,
  172. FlightNumber text,
  173. Remarks text)''')
  174. conn.close()
  175. def deletedbextras(): # deletes extras database. Use with caution!
  176. conn = sqlite3.connect(os.path.join('./csv', 'flog.db'))
  177. c = conn.cursor()
  178. c.execute("DROP TABLE extras")
  179. print('Table extras has been deleted')
  180. conn.close()