UPDATE fct update_to_sql
This commit is contained in:
parent
0416810bf2
commit
359ac975e3
29
pycen/zh.py
29
pycen/zh.py
@ -640,7 +640,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
|
||||
@ -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))
|
||||
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)
|
||||
|
||||
Loading…
x
Reference in New Issue
Block a user