Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/324.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python SQLAlchemy递归插入到表中_Python_Recursion_Sqlalchemy - Fatal编程技术网

Python SQLAlchemy递归插入到表中

Python SQLAlchemy递归插入到表中,python,recursion,sqlalchemy,Python,Recursion,Sqlalchemy,是否有一种简洁的方法递归执行插入,以便在批量插入失败时,它一次尝试(例如)一半的记录。目前,我正在做这样的事情: def __recursive_import__(conn, insrt_func, validated_rows): try: result = conn.execute(insrt_func(validated_rows)) except: __recursive_import__(conn, insrt_func, validated_rows[len(

是否有一种简洁的方法递归执行插入,以便在批量插入失败时,它一次尝试(例如)一半的记录。目前,我正在做这样的事情:

def __recursive_import__(conn, insrt_func, validated_rows):
  try:
    result = conn.execute(insrt_func(validated_rows))
  except:
    __recursive_import__(conn, insrt_func, validated_rows[len(validated_rows//2):])
    __recursive_import__(conn, insrt_func, validated_rows[:len(validated_rows//2)])

def import_row(some_rows):
  return """INSERT INTO SomeTable {};""".format(some_rows)

with db.connect('some_db').begin() as conn:
  __recursive_import__(conn, import_row, rows_to_insert)

因此,如果某些行由于某些原因出错。。。(我正在插入一个包含大量业务逻辑的数据库),它将继续尝试越来越小的块。这很难看,可能不起作用,很难看。炼金术有更好的方法吗?有人有更好的方法吗?

作为旁注:不要使用
\uuuu dunder\u names\uuuu
,除非您提供了系统定义的属性,如
\uuuu eq\uu
。这样的名称是保留的,将来可能会突然被使用。为什么您当前的解决方案不简洁?我想我是在假设我正在尝试的是非常常见的,因此可能已经存在于sqlalchemy中。请看一看
事务
保存点
类似的情况: