I am working with SQLAlchemy and my insert function works correctly. However, i want and i need it to be efficient, therefore since i am inserting inside a "for loop", i would like to commit just once at the end of my program execution.
I am not sure, this kind of thinking applies to SQLAlchemy, so please advice me on the right, efficient way of doing it.
my code will call a insert_query function from a for loop. I do not return the query object that is created inside the function call.
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()