UPDATE fct update_to_sql

This commit is contained in:
Colas Geier 2021-12-01 14:43:18 +01:00
parent 0416810bf2
commit 359ac975e3

View File

@ -640,7 +640,8 @@ class ref_hydro:
##################################### #####################################
### Update ### ### 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 = [] a = []
b = [] b = []
table = table_name table = table_name
@ -652,14 +653,24 @@ def update_to_sql(df, con, table_name, schema_name, key_name):
b.append("t.{col}=f.{col}".format(col=col)) b.append("t.{col}=f.{col}".format(col=col))
else: else:
a.append("{col}=t.{col}".format(col=col)) a.append("{col}=t.{col}".format(col=col))
df.to_sql( if isinstance(df,GeoDataFrame):
name = 'temp_table', df.to_postgis(
con = con, name = 'temp_table',
schema = schema, con = con,
if_exists = 'replace', schema = schema,
index = False, if_exists = 'replace',
method = 'multi' 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_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_2 = " FROM {sch}.temp_table t".format(sch=schema)
update_stmt_6 = " WHERE %s"%' AND '.join(b) update_stmt_6 = " WHERE %s"%' AND '.join(b)