问题描述
我正在使用 psycopg2 在Postgres表上一次一次插入多行。我正在使用下面的函数来完成我的工作,插入多行工作正常,但是当一行有任何问题时,它将无法插入所有剩余的行。例如,我一次传递了100条记录,并且几何结构存在一行问题,但它也会丢弃其余的所有99条记录。所以我的用例是我要继续插入,但有问题的那一行除外。另外,我不想一次插入一行。
def insert_to_db(data):
"""
Inserting records to db
:param data:
:return:
"""
if data:
try:
# connect postgresql
db_conn = psycopg2.connect(host=t_host,port=t_port,dbname=t_dbname,user=t_user,password=t_pw)
db_cursor = db_conn.cursor()
# make sql query
vars_list = [tuple(dic.values()) for dic in data]
sql = '''INSERT INTO locations_ep (location_id,name,country_code,slug,type,center,bounding_polygon) VALUES (%s,%s,ST_SetSRID(ST_GeomFromGeoJSON(%s),4326));'''
# execute and commit query
db_cursor.executemany(sql,vars_list)
db_conn.commit()
# close everything
db_cursor.close()
db_conn.close()
except Exception as e:
print(e)
所以我的问题是什么是另一种方法,这样我就可以批量插入并仅跳过有问题的那些插入对象?
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)