Add SQL insertion functionality

This commit is contained in:
coolneng 2020-01-08 01:23:00 +01:00
parent dfd13f6037
commit 7b945e2b98
Signed by: coolneng
GPG Key ID: 9893DA236405AF57
9 changed files with 51 additions and 28 deletions

View File

@ -62,3 +62,4 @@ CLOSED: [2020-01-03 Fri 00:44]
*** NEXT Documentation *** NEXT Documentation
*** INACTIVE Frontend [0/1] [0%] *** INACTIVE Frontend [0/1] [0%]
- [ ] [[https://adminlte.io/][Adminlte]] - [ ] [[https://adminlte.io/][Adminlte]]
- [ ] Bootstrap

View File

@ -29,9 +29,11 @@ db = create_connection()
class Glacier(db.Model): class Glacier(db.Model):
uid = db.Column(db.Integer, primary_key=True) uid = db.Column(db.String(5), primary_key=True)
country = db.Column(db.String(60)) country = db.Column(db.String(60))
name = db.Column(db.String(60)) name = db.Column(db.String(60))
annual_data = db.relationship("Annual_Data")
annual_change = db.relationship("Annual_Change")
def __init__(self, uid, country, name): def __init__(self, uid, country, name):
self.uid = uid self.uid = uid
@ -40,37 +42,30 @@ class Glacier(db.Model):
class Annual_Data(db.Model): class Annual_Data(db.Model):
glacier = db.relationship("Glacier", backref=db.backref("posts", lazy=True)) year = db.Column(db.Integer, primary_key=True)
uid = db.Column( uid = db.Column(db.String(5), db.ForeignKey("glacier.uid"), primary_key=True)
db.Integer, db.ForeignKey("glacier.uid"), nullable=False, primary_key=True
)
surface = db.Column(db.Integer) surface = db.Column(db.Integer)
length = db.Column(db.Integer) length = db.Column(db.Integer)
elevation = db.Column(db.Integer) elevation = db.Column(db.Integer)
year = db.Column(db.Integer, primary_key=True)
def __init__(self, surface, length, elevation, year): def __init__(self, year, surface, length, elevation):
self.year = year
self.surface = surface self.surface = surface
self.length = length self.length = length
self.elevation = elevation self.elevation = elevation
self.year = year
class Annual_Change(db.Model): class Annual_Change(db.Model):
glacier = db.relationship("Glacier", backref=db.backref("posts", lazy=True)) year = db.Column(db.Integer, db.ForeignKey("annual__data.year"), primary_key=True)
uid = db.Column( uid = db.Column(db.String(5), db.ForeignKey("glacier.uid"), primary_key=True)
db.Integer, db.ForeignKey("glacier.uid"), nullable=False, primary_key=True
)
surface_change = db.Column(db.Integer) surface_change = db.Column(db.Integer)
length_change = db.Column(db.Integer) length_change = db.Column(db.Integer)
elevation_change = db.Column(db.Integer) elevation_change = db.Column(db.Integer)
year = db.Column(db.Integer, primary_key=True)
def __init__(self, surface_change, length_change, elevation_change, year): def __init__(self, surface_change, length_change, elevation_change):
self.surface_change = surface_change self.surface_change = surface_change
self.length_change = length_change self.length_change = length_change
self.elevation_change = elevation_change self.elevation_change = elevation_change
self.year = year
class User(db.Model): class User(db.Model):

View File

@ -34,8 +34,24 @@ def select_columns() -> DataFrame:
return data return data
def rename_fields(df_list):
new_df_list = {}
new_fields = {
"POLITICAL_UNIT": "country",
"NAME": "name",
"WGMS_ID": "uid",
"YEAR": "year",
"MEDIAN_ELEVATION": "elevation",
"AREA": "surface",
"LENGTH": "length",
}
for key, value in df_list.items():
new_df_list[key] = value.rename(columns=new_fields)
return new_df_list
def create_databases(df): def create_databases(df):
users = {"UID": [7843], "USERNAME": ["admin"], "PASSWORD": [ADMIN_PW]} users = {"uid": [7843], "username": ["admin"], "password": [ADMIN_PW]}
files = { files = {
"glacier": "../data/glacier.csv", "glacier": "../data/glacier.csv",
"annual__data": "../data/annual_data.csv", "annual__data": "../data/annual_data.csv",
@ -48,7 +64,8 @@ def create_databases(df):
"annual__change": df[["WGMS_ID", "YEAR"]], "annual__change": df[["WGMS_ID", "YEAR"]],
"users": DataFrame(users), "users": DataFrame(users),
} }
for key, val in dataframes.items(): renamed_dfs = rename_fields(dataframes)
for key, val in renamed_dfs.items():
val.to_csv(files[key], index=False, quoting=QUOTE_NONNUMERIC) val.to_csv(files[key], index=False, quoting=QUOTE_NONNUMERIC)

View File

@ -1,5 +1,15 @@
from db_setup import create_connection from constants import DB_NAME, DB_PW, DB_USER
from pandas import DataFrame, read_csv, to_sql from pandas import DataFrame, read_csv
from sqlalchemy import create_engine, engine
def create_connection() -> engine:
host = "localhost:3306"
connection_uri = "mysql+pymysql://{user}:{pw}@{url}/{db}".format(
user=DB_USER, pw=DB_PW, url=host, db=DB_NAME
)
engine = create_engine(connection_uri)
return engine
def create_dataframes() -> DataFrame: def create_dataframes() -> DataFrame:
@ -11,13 +21,13 @@ def create_dataframes() -> DataFrame:
} }
df_list = {} df_list = {}
for csv in files.keys(): for csv in files.keys():
df_list[csv] = read_csv(files[csv], index=False) df_list[csv] = read_csv(files[csv])
return df_list return df_list
def insert_data(df_list, conn): def insert_data(df_list, conn):
for key, value in df_list.items(): for key, value in df_list.items():
value.to_sql(key, con=conn, if_exists="replace", index=False) value.to_sql(key, con=conn, index=False, if_exists="append", chunksize=1)
def main(): def main():

View File

@ -1,4 +1,4 @@
"WGMS_ID","YEAR" "uid","year"
10452,2018 10452,2018
2665,2014 2665,2014
2665,2015 2665,2015

1 WGMS_ID uid YEAR year
2 10452 2018
3 2665 2014
4 2665 2015

View File

@ -1,4 +1,4 @@
"WGMS_ID","YEAR","AREA","MEDIAN_ELEVATION","LENGTH" "uid","year","surface","elevation","length"
10452,2018,1.68,"",2.1 10452,2018,1.68,"",2.1
2665,2014,12.9,390.0,7.6 2665,2014,12.9,390.0,7.6
2665,2015,12.9,390.0,7.6 2665,2015,12.9,390.0,7.6

1 WGMS_ID uid YEAR year AREA surface MEDIAN_ELEVATION elevation LENGTH length
2 10452 2018 1.68 2.1
3 2665 2014 12.9 390.0 7.6
4 2665 2015 12.9 390.0 7.6

View File

@ -1,4 +1,4 @@
"POLITICAL_UNIT","NAME","WGMS_ID" "country","name","uid"
"Afghanistan","PIR YAKH",10452 "Afghanistan","PIR YAKH",10452
"Antarctica","BAHIA DEL DIABLO",2665 "Antarctica","BAHIA DEL DIABLO",2665
"Antarctica","BELLINGSHAUSEN",6833 "Antarctica","BELLINGSHAUSEN",6833

1 POLITICAL_UNIT country NAME name WGMS_ID uid
2 Afghanistan PIR YAKH 10452
3 Antarctica BAHIA DEL DIABLO 2665
4 Antarctica BELLINGSHAUSEN 6833

View File

@ -1,2 +1,2 @@
"UID","USERNAME","PASSWORD" "uid","username","password"
7843,"admin","fuckmonsanto" 7843,"admin","fuckmonsanto"

1 UID uid USERNAME username PASSWORD password
2 7843 7843 admin admin fuckmonsanto fuckmonsanto

View File

@ -20,17 +20,17 @@ datos relevantes para estudios acerca del cambio climático, y acotando éstos a
1. *RD1*: Datos del glaciar 1. *RD1*: Datos del glaciar
- País - /Cadena de 60 caracteres máximo/ - País - /Cadena de 60 caracteres máximo/
- Nombre del glaciar - /Cadena de 60 caracteres máximo/ - Nombre del glaciar - /Cadena de 60 caracteres máximo/
- ID del glaciar (Compatible con la WGMS) - /Entero de 5 dígitos/ - ID del glaciar (Compatible con la WGMS) - /Cadena de 5 caracteres/
2. *RD2*: Datos anuales de un glaciar 2. *RD2*: Datos anuales de un glaciar
- ID del glaciar (Compatible con la WGMS) - /Entero de 5 dígitos/ - ID del glaciar (Compatible con la WGMS) - /Cadena de 5 caracteres/
- Área - /Entero de 10 dígitos/ - Área - /Entero de 10 dígitos/
- Volumen - /Entero de 10 dígitos/ - Volumen - /Entero de 10 dígitos/
- Altura - /Entero de 10 dígitos/ - Altura - /Entero de 10 dígitos/
- Año - /Entero de 10 dígitos/ - Año - /Entero de 10 dígitos/
3. *RD3*: Datos de cambio de un glaciar 3. *RD3*: Datos de cambio de un glaciar
- ID del glaciar (Compatible con la WGMS) - /Entero de 5 dígitos/ - ID del glaciar (Compatible con la WGMS) - /Cadena de 5 caracteres/
- Variación de área - /Entero de 10 dígitos/ - Variación de área - /Entero de 10 dígitos/
- Variación de volumen - /Entero de 10 dígitos/ - Variación de volumen - /Entero de 10 dígitos/
- Variación de altura - /Entero de 10 dígitos/ - Variación de altura - /Entero de 10 dígitos/