(curs, tablename, data, fields = None, use_insert = 0, quoted_table = False)
r"""Copy/insert a list of dict/list data to database.

    If curs == None, then the copy or insert statements are returned
    as string.  For list of dict the field list is optional, as its
    possible to guess them from dict keys.

    Example:
    >>> magic_insert(None, 'tbl', [[1, '1'], [2, '2']], ['col1', 'col2'])
    'COPY public.tbl (col1,col2) FROM STDIN;\n1\t1\n2\t2\n\\.\n'
r"""Copy/insert a list of dict/list data to database.
r Copy / insert a list of dict / list data to database .
def magic_insert(curs, tablename, data, fields = None, use_insert = 0, quoted_table = False):
    r"""Copy/insert a list of dict/list data to database.

    If curs == None, then the copy or insert statements are returned
    as string.  For list of dict the field list is optional, as its
    possible to guess them from dict keys.

    Example:
    >>> magic_insert(None, 'tbl', [[1, '1'], [2, '2']], ['col1', 'col2'])
    'COPY public.tbl (col1,col2) FROM STDIN;\n1\t1\n2\t2\n\\.\n'
    """
    if len(data) == 0:
        return

    # decide how to process
    if hasattr(data[0], 'keys'):
        if fields == None:
            fields = data[0].keys()
        if use_insert:
            row_func = _gen_dict_insert
        else:
            row_func = _gen_dict_copy
    else:
        if fields == None:
            raise Exception("Non-dict data needs field list")
        if use_insert:
            row_func = _gen_list_insert
        else:
            row_func = _gen_list_copy

    qfields = [skytools.quote_ident(f) for f in fields]
    if quoted_table:
        qtablename = tablename
    else:
        qtablename = skytools.quote_fqident(tablename)

    # init processing
    buf = StringIO()
    if curs == None and use_insert == 0:
        fmt = "COPY %s (%s) FROM STDIN;\n"
        buf.write(fmt % (qtablename, ",".join(qfields)))

    # process data
    for row in data:
        buf.write(row_func(qtablename, row, fields, qfields))
        buf.write("\n")

    # if user needs only string, return it
    if curs == None:
        if use_insert == 0:
            buf.write("\\.\n")
        return buf.getvalue()

    # do the actual copy/inserts
    if use_insert:
        curs.execute(buf.getvalue())
    else:
        buf.seek(0)
        hdr = "%s (%s)" % (qtablename, ",".join(qfields))
        curs.copy_from(buf, hdr)
def magic_insert ( curs , tablename , data , fields = None , use_insert = 0 , quoted_table = False ) : if len ( data ) == 0 : return # decide how to process if hasattr ( data [ 0 ] , 'keys' ) : if fields == None : fields = data [ 0 ] . keys ( ) if use_insert : row_func = _gen_dict_insert else : row_func = _gen_dict_copy else : if fields == None : raise Exception ( "Non-dict data needs field list" ) if use_insert : row_func = _gen_list_insert else : row_func = _gen_list_copy qfields = [ skytools . quote_ident ( f ) for f in fields ] if quoted_table : qtablename = tablename else : qtablename = skytools . quote_fqident ( tablename ) # init processing buf = StringIO ( ) if curs == None and use_insert == 0 : fmt = "COPY %s (%s) FROM STDIN;\n" buf . write ( fmt % ( qtablename , "," . join ( qfields ) ) ) # process data for row in data : buf . write ( row_func ( qtablename , row , fields , qfields ) ) buf . write ( "\n" ) # if user needs only string, return it if curs == None : if use_insert == 0 : buf . write ( "\\.\n" ) return buf . getvalue ( ) # do the actual copy/inserts if use_insert : curs . execute ( buf . getvalue ( ) ) else : buf . seek ( 0 ) hdr = "%s (%s)" % ( qtablename , "," . join ( qfields ) ) curs . copy_from ( buf , hdr )