UPDATE clean zh

This commit is contained in:
Colas Geier 2021-11-30 15:46:57 +01:00
parent 991286e4df
commit 9716e61ea4

View File

@ -638,7 +638,8 @@ class ref_hydro:
#####################################
### Update ###
#####################################
def update_to_sql(df, con, table_name, schema_name, key_name):
def update_to_sql(df,con,table_name,schema_name,key_name,geom_col='geom'):
from geopandas import GeoDataFrame
a = []
b = []
table = table_name
@ -650,14 +651,24 @@ def update_to_sql(df, con, table_name, schema_name, key_name):
b.append("t.{col}=f.{col}".format(col=col))
else:
a.append("{col}=t.{col}".format(col=col))
df.to_sql(
name = 'temp_table',
con = con,
schema = schema,
if_exists = 'replace',
index = False,
method = 'multi'
)
if isinstance(df,GeoDataFrame):
df.to_postgis(
name = 'temp_table',
con = con,
schema = schema,
if_exists = 'replace',
index = False,
geom_col=geom_col,
)
else:
df.to_sql(
name = 'temp_table',
con = con,
schema = schema,
if_exists = 'replace',
index = False,
method = 'multi'
)
update_stmt_1 = "UPDATE {sch}.{final_table} f".format(sch=schema,final_table=table)
update_stmt_2 = " FROM {sch}.temp_table t".format(sch=schema)
update_stmt_6 = " WHERE %s"%' AND '.join(b)
@ -670,17 +681,4 @@ def update_to_sql(df, con, table_name, schema_name, key_name):
cnx.execute(drop_stmt)
return print('END update')
# [SQL: INSERT INTO zones_humides.r_site_reghydro (id, id_geom_site, id_reg_hydro, id_permanence, rmq_toponymie, in_out)
# VALUES (%(id)s, %(id_geom_site)s, %(id_reg_hydro)s, %(id_permanence)s, %(rmq_toponymie)s, %(in_out)s)]
# [parameters: ({'id': 0, 'id_geom_site': 5, 'id_reg_hydro': '0', 'id_permanence': '1', 'rmq_toponymie': '', 'in_out': True},
# {'id': 1, 'id_geom_site': 5, 'id_reg_hydro': '1', 'id_permanence': '1', 'rmq_toponymie': '', 'in_out': False},
# {'id': 2, 'id_geom_site': 6, 'id_reg_hydro': '0', 'id_permanence': '1', 'rmq_toponymie': '', 'in_out': True},
# {'id': 3, 'id_geom_site': 6, 'id_reg_hydro': '1', 'id_permanence': '1', 'rmq_toponymie': '', 'in_out': False},
# {'id': 4, 'id_geom_site': 7, 'id_reg_hydro': '2', 'id_permanence': '2', 'rmq_toponymie': 'plusieurs petites sources dans versant', 'in_out': True},
# {'id': 5, 'id_geom_site': 7, 'id_reg_hydro': '1', 'id_permanence': '2', 'rmq_toponymie': 'longe la route D209a', 'in_out': False},
# {'id': 6, 'id_geom_site': 8, 'id_reg_hydro': '0', 'id_permanence': '2', 'rmq_toponymie': '', 'in_out': True},
# {'id': 7, 'id_geom_site': 8, 'id_reg_hydro': '3', 'id_permanence': None, 'rmq_toponymie': '', 'in_out': False}
# ... displaying 10 of 5779 total bound parameter sets ...
# {'id': 5777, 'id_geom_site': 1951, 'id_reg_hydro': '0', 'id_permanence': None, 'rmq_toponymie': '', 'in_out': True},
# {'id': 5778, 'id_geom_site': 1951, 'id_reg_hydro': '3', 'id_permanence': None, 'rmq_toponymie': '', 'in_out': False})]