-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathimport_airports.py
More file actions
30 lines (24 loc) · 978 Bytes
/
import_airports.py
File metadata and controls
30 lines (24 loc) · 978 Bytes
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
import sqlite3
from app import db, Airport, app
def import_airports():
# Connect to the SQLite database
conn = sqlite3.connect('airports.db')
cursor = conn.cursor()
# Select airport data from the SQLite database
cursor.execute('SELECT city, airport_name, code, country FROM airports')
airports = cursor.fetchall()
conn.close()
# Insert data into the Flask application's database using SQLAlchemy
with app.app_context():
for city, airport_name, code, country in airports:
# Check if the airport already exists
existing_airport = Airport.query.filter_by(code=code).first()
if existing_airport:
# Skip duplicates
continue
# Insert new record
airport = Airport(city=city, airport_name=airport_name, code=code, country=country)
db.session.add(airport)
db.session.commit()
if __name__ == '__main__':
import_airports()