Skip to content

Commit

Permalink
st
Browse files Browse the repository at this point in the history
  • Loading branch information
ahuigo committed May 27, 2020
1 parent bc7617e commit 72b11bb
Show file tree
Hide file tree
Showing 2 changed files with 17 additions and 1 deletion.
16 changes: 15 additions & 1 deletion db/conn.py
Expand Up @@ -52,12 +52,13 @@ def insertBatch(cursor, table, rows, onConflictKeys=None):


# onConflictKeys="key1,key2"
def insertUpdate(cursor, table, row, onConflictKeys=None):
def insertUpdate(cursor, table, row, onConflictKeys=''):
keys = tuple(row.keys())
values = tuple(row.values())

key_fields = ",".join(keys)
value_format = ",".join(["%s"] * len(keys))
conflictKeys= onConflictKeys.split(",")

sql = f"insert into {table}({key_fields}) values({value_format})"
if onConflictKeys:
Expand All @@ -72,6 +73,19 @@ def insertUpdate(cursor, table, row, onConflictKeys=None):
# print(sql, values)
try:
cursor.execute(sql, values)
except psycopg2.errors.UniqueViolation as e:
if not onConflictKeys:
raise e
print([cursor.query])
conflictKeys += uk

sql = f'update {table} set'
set_keys = ','.join([f'"{k}"=%s' for k in keys])
where_keys = ' and '.join([f'"{k}"=%s' for k in conflictKeys])
values += [row[k] for k in conflictKeys]
sql = f'{sql} {set_keys} where {where_keys}'
cursor.execute(sql, values)

except Exception as e:
print([cursor.query])
raise e
Expand Down
2 changes: 2 additions & 0 deletions makefile
Expand Up @@ -4,6 +4,8 @@ sql:

benchMeanAsValue:
python -u bench/benchMeanAsValue.py
benchMeanAsValue:
python bench/benchLevelAsValue.py -c 1:200 --hold 36:39

mean: #求均值
python lib/MeanLine.py $(code)
Expand Down

0 comments on commit 72b11bb

Please sign in to comment.