次の関数で finally を使用しようとしていますが、Python は構文エラーを報告します。私は何かばかげたことをしていると確信していますが、それを見つけることができないようです...
スニペットは以下のとおりです。
# Store ids with key
# Returns GUID (used to clear table after words)
def storeIdsInTemporaryTable(dbinfo, id_list):
conn = dbinfo['db_connection']
guid = genutils.getGUID()
orig_tableinfo = dbinfo['table']
orig_datarows = dbinfo['datarows']
metadata = dbinfo['metadata']
sql = "INSERT INTO temporary_data_key (key) VALUES ({0}) RETURNING id".format(guid)
key_id = executeSQLonDbConnection(dbinfo, sql, return_field='id')
tableinfo = Table('temporary_data', metadata, autoload=True)
datarows = []
for id_value in id_list:
datarows.append( { 'key_id': key_id, 'id_value': id_value} )
try:
insertToDb(dbinfo)
except:
guid = None # to indicate an error occured
if key_id:
conn.execute("DELETE FROM temporary_data_key WHERE key={0}".format(guid)
finally:
dbinfo['table'] = orig_tableinfo
dbinfo['datarows'] = orig_datarows
return guid
構文エラーの原因は何ですか?
余談ですが、トランザクションで 2 つの挿入をラップする必要があることは認識していますが、何らかの理由でトランザクションを機能させることができません (SQLALchemy はトランザクション関連のエラーをスローします)。
[[編集]]
例外エラー (現在は修正済み) は次のとおりです。
Traceback (most recent call last):
File "<stdin>", line 1, in <module>
File "/path/to/script.py", line 1632
finally:
^
SyntaxError: invalid syntax