Python,SQLAlchemy,如何只用一条提交指令以有效的方式插入查询

时间:2013-07-15 13:37:04

标签: python sql sqlalchemy performance

我正在使用SQLAlchemy,我的插入功能正常工作。但是,我想要并且我需要它是高效的,因此,因为我插入一个“for循环”,我想在程序执行结束时只提交一次。

我不确定,这种想法适用于SQLAlchemy,所以请以正确,有效的方式为我提供建议。

我的代码将从for循环中调用insert_query函数。我不返回在函数调用中创建的查询对象。

def insert_query(publicId, secret, keyhandle, secretobj):

    #creates the query object 
    sql = secretobj.insert().values(public_id=publicId, keyhandle=keyhandle, secret=secret)
    #insert the query
    result = connection.execute(sql)

    return result

#####################
# CALL INSERT BELOW #
#####################


#walk across the file system to do some stuff
for root, subFolders, files in os.walk(path):
    if files:

        do_some_stuff_that_produce_output_for_insert_query()

        #########################
        # here i call my insert #
        #########################
        if not insert_query(publicId, secret, keyhandle, secretobj):
            print "WARNING: could not insert %s" % publicId


#close sqlalchemy
connection.close()

1 个答案:

答案 0 :(得分:3)

我认为你最好使用executemany。

def make_secret(files):
    # You'd have to define how you generate the dictionary to insert.
    # These names should match your table column names.
    return {
        'public_id': None,
        'secret': None,
        'keyhandle': None,
    }
# You can make the whole list of rows to insert at once.
secrets = [make_secret(files) for root, subFolders, files in os.walk(path) if files]
# Then insert them all like this
connection.execute(secretobj.insert(), secrets)

executemany在本节的第二部分进行了解释:

http://docs.sqlalchemy.org/en/rel_0_8/core/tutorial.html#executing-multiple-statements