소스 검색

Add files via upload

Felix Turowsky 4 년 전
부모
커밋
9589abe7c1
2개의 변경된 파일338개의 추가작업 그리고 0개의 파일을 삭제
  1. 129 0
      dbpopulation/csvimport.py
  2. 209 0
      dbpopulation/dbman.py

+ 129 - 0
dbpopulation/csvimport.py

@@ -0,0 +1,129 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+"""
+Created on Thu May 28 11:23:44 2020
+Used to import csv data into tables, written in python for ease of use.
+Data is obtained from:
+https://ourairports.com/data/
+https://openflights.org/data.html
+@author: Felix Turowsky
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+"""
+import csv
+import sqlite3
+import os
+
+conn = sqlite3.connect(os.path.join('./csv', 'logbook.db'))
+c = conn.cursor()
+
+with open(os.path.join('./csv', 'airports_edited.csv')) as fin:
+    dr = csv.DictReader(fin)  # comma is default delimiter
+    to_dbap = [(i['icao'],
+              i['iata'],
+              i['name'], 
+              i['lat'],
+              i['long'],
+              i['country'],
+              i['alt'],
+              i['utcoffset'],
+              i['tzolson'],
+              ) for i in dr]
+
+
+def csvtodbap(to_db):
+    c.executemany("INSERT INTO airports (icao, iata, name, lat, long, country, alt, utcoffset, tzolson) VALUES (?, ?, ?, ?, ?, ?, ?, ?, ?);", to_db)
+    conn.commit()
+    print('Database entries have been added. New table:')
+    for row in c.execute("select * from airports"):
+        print(row)
+    conn.close()
+
+
+with open(os.path.join('./csv', 'flights_test.csv')) as fin:  # imports flights
+    dr = csv.DictReader(fin)  # comma is default delimiter
+    to_dbfl = [(i['doft'],
+              i['dept'],
+              i['tofb'],
+              i['dest'],
+              i['tonb'],
+              i['tblk'],
+              i['pic'],
+              i['acft']
+              ) for i in dr]
+
+
+def csvtodbfl(to_db):
+    c.executemany("INSERT INTO flights (doft, dept, tofb, dest, tonb, tblk, pic, acft) VALUES (?, ?, ?, ?, ?, ?, ?, ?);", to_db)
+    conn.commit()
+    print('Databse entries have been added. New table:')
+    for row in c.execute("select * from flights"):
+        print(row)
+    conn.close()
+
+
+with open(os.path.join('./csv', 'pilots_test.csv')) as fin:  # imports flights
+    dr = csv.DictReader(fin)  # comma is default delimiter
+    to_dbpl = [(i['picfirstname'],
+              i['piclastname'],
+              i['alias']
+              ) for i in dr]
+
+
+def csvtodbpl(to_db):
+    c.executemany("INSERT INTO pilots (picfirstname, piclastname, alias) VALUES (?, ?, ?);", to_db)
+    conn.commit()
+    print('Database entries have been added. New table:')
+    for row in c.execute("select * from pilots"):
+        print(row)
+    conn.close()
+
+
+with open(os.path.join('./csv', 'aircraft.csv')) as fin:  # imports flights
+    dr = csv.DictReader(fin)  # comma is default delimiter
+    to_dbac = [(i['Name'],
+              i['iata'],
+              i['icao'],
+              ) for i in dr]
+
+
+def csvtodbac(to_db):
+    c.executemany("INSERT INTO aircraft (Name, icao, iata) VALUES (?, ?, ?);", to_db)
+    conn.commit()
+    print('Databse entries have been added. New table:')
+    for row in c.execute("select * from aircraft"):
+        print(row)
+    conn.close()
+
+
+with open(os.path.join('./csv', 'tails_test.csv')) as fin: # imports flights
+    dr = csv.DictReader(fin) # comma is default delimiter
+    to_dbtails = [(i['Registration'], 
+              i['aircraft_id'],
+              i['Company']
+              ) for i in dr]
+
+
+def csvtodbtails(to_db):
+    c.executemany("INSERT INTO tails (Registration, aircraft_id, Company) VALUES (?, ?, ?);", to_db)
+    conn.commit()
+    print('Databse entries have been added. New table:')
+    for row in c.execute("select * from tails"):
+        print(row)
+    conn.close()

+ 209 - 0
dbpopulation/dbman.py

@@ -0,0 +1,209 @@
+#!/usr/bin/env python3
+# -*- coding: utf-8 -*-
+"""
+Created on Thu May 22 20:04:38 2020
+Used to create the initial database layout.
+@author: Felix Turowsky
+/*
+ *openPilot Log - A FOSS Pilot Logbook Application
+ *Copyright (C) 2020  Felix Turowsky
+ *
+ *This program is free software: you can redistribute it and/or modify
+ *it under the terms of the GNU General Public License as published by
+ *the Free Software Foundation, either version 3 of the License, or
+ *(at your option) any later version.
+ *
+ *This program is distributed in the hope that it will be useful,
+ *but WITHOUT ANY WARRANTY; without even the implied warranty of
+ *MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
+ *GNU General Public License for more details.
+ *
+ *You should have received a copy of the GNU General Public License
+ *along with this program.  If not, see <https://www.gnu.org/licenses/>.
+ */
+
+
+"""
+This set of functions is used to initialise or reset the database.
+"""
+
+import sqlite3
+import os
+
+
+def initialisedb(): # For first run only, creates all databases.
+    createdbairports()
+    createdbflights()
+
+
+def createdbflights(): #this database will hold flight information
+    conn = sqlite3.connect(os.path.join('./resources', 'flog.db'))
+    c = conn.cursor()
+    c.execute('''create table flights (
+            id integer primary key,
+            doft numeric not null,
+            dept text not null,
+            tofb integer not null,
+            dest text not null,
+            tonb integer not null,
+            tblk integer,
+            pic integer,
+            acft integer,
+            foreign key (pic) references pilots (pilot_id)
+            foreign key (acft) references tails (tail_id)
+            )
+            ''')
+    conn.close()
+
+
+def deletedbflights(): # deletes flights database. Use with caution!
+    conn = sqlite3.connect(os.path.join('./resources', 'flog.db'))
+    c = conn.cursor()
+    c.execute("DROP TABLE flights")
+    print('Table flights has been deleted')
+    conn.close()
+
+
+def createdbairports(): # this database will hold airport information
+    conn = sqlite3.connect(os.path.join('./resources', 'flog.db'))
+    c = conn.cursor()
+    c.execute('''create table airports(
+            airport_id integer primary key,
+            icao text not null,
+            iata text,
+            name text,
+            lat real,
+            long real,
+            country text,
+            alt integer,
+            utcoffset integer,
+            tzolson text)''')
+    conn.close()
+
+
+def deletedbairports(): # deletes flights database. Use with caution!
+    conn = sqlite3.connect(os.path.join('./csv', 'flog.db'))
+    c = conn.cursor()
+    c.execute("DROP TABLE airports")
+    print('Table airports has been deleted')
+    conn.close()
+
+
+def createdbpilots(): # this database will hold Pilot names
+    conn = sqlite3.connect(os.path.join('./csv', 'flog.db'))
+    c = conn.cursor()
+    c.execute('''create table pilots(
+            pilot_id integer primary key,
+            picfirstname text,
+            piclastname text not null,
+            alias text)''')
+    firstname = input('Please enter your First name: ')
+    lastname = input('Please enter your Last name: ')
+    todb = (firstname,lastname,'self')
+    c.execute("INSERT INTO pilots (\
+                                      picfirstname,\
+                                      piclastname,\
+                                      alias) \
+                                      VALUES (?,?,?)", todb)
+    conn.commit()
+    conn.close()
+
+
+def deletedbpilots(): # deletes flights database. Use with caution!
+    conn = sqlite3.connect(os.path.join('./csv', 'flog.db'))
+    c = conn.cursor()
+    c.execute("DROP TABLE pilots")
+    print('Table pilots has been deleted')
+    conn.close()
+
+
+def createdbaircraft():
+    """
+    This database will hold AC information. Some triggers
+    like multiengine or heavy (above 5.7t) should be 
+    Boolean but sqlite does not have a seperate type
+    definition so 1=True, 0=False. Seperate table needs
+    to be created for Registrations (Tails)
+    """
+    conn = sqlite3.connect(os.path.join('./csv', 'flog.db'))
+    c = conn.cursor()
+    c.execute('''create table aircraft(
+            aircraft_id integer primary key,
+            Name text,
+            iata text,
+            icao text,
+            multipilot integer,
+            multiengine integer,
+            jet integer,
+            heavy integer)''') #heavy as in above 5.7t
+    conn.close()
+
+
+def deletedbaircraft(): # deletes aircraft database. Use with caution!
+    conn = sqlite3.connect(os.path.join('./csv', 'flog.db'))
+    c = conn.cursor()
+    c.execute("DROP TABLE aircraft")
+    print('Table aircraft has been deleted')
+    conn.close()
+
+
+def createdbtails():
+    """
+    This table links individual aircraft to an aircraft
+    type specified in the aircraft table.
+    """
+    conn = sqlite3.connect(os.path.join('./csv', 'flog.db'))
+    c = conn.cursor()
+    c.execute('''create table tails(
+            tail_id integer primary key,
+            Registration text not null,
+            aircraft_id integer not null,
+            Company Text,
+            foreign key (aircraft_id) references aircraft (aircraft_id)
+            )''') #heavy as in above 5.7t
+    conn.close()
+
+
+def deletedbtails(): # deletes extras database. Use with caution!
+    conn = sqlite3.connect(os.path.join('./csv', 'flog.db'))
+    c = conn.cursor()
+    c.execute("DROP TABLE tails")
+    print('Table tails has been deleted')
+    conn.close()
+
+
+def createdbextras():
+    """
+    This database will hold additional information like function
+    times, operational condition time, night flying
+    time...
+    """
+    conn = sqlite3.connect(os.path.join('./csv', 'flog.db'))
+    c = conn.cursor()
+    c.execute('''create table extras(
+            extras_id integer primary key,
+            PilotFlying integer,
+            TOday integer,
+            TOnight integer,
+            LDGday integer,
+            LDGnight integer,
+            autoland integer,
+            tNight integer,
+            tIFR integer,
+            tPIC integer,
+            tSIC integer,
+            tDual integer,
+            tInstructor integer,
+            tSIM integer,
+            ApproachType text,
+            FlightNumber text,
+            Remarks text)''')
+    conn.close()
+
+
+def deletedbextras(): # deletes extras database. Use with caution!
+    conn = sqlite3.connect(os.path.join('./csv', 'flog.db'))
+    c = conn.cursor()
+    c.execute("DROP TABLE extras")
+    print('Table extras has been deleted')
+    conn.close()