Python在循环中第二次使用函数时,无法在函数上调用错误bool



我正试图在已上传到云中的表之间添加一个外键:

这是基于以下内容的脚本:

  1. 添加主表
  2. 更改主表以具有名为(_INDEX(的主键
  3. 通过名为(SURVEY_ID(的主键将主表连接到顶级表
  4. 添加子表
  5. 将子表通过_PARENT_INDEX从主表连接到_INDEX

步骤3在主表和顶级表之间工作,但在步骤5执行的相同功能导致错误:

"池"对象不可调用

这是脚本:

add_dataframe = add_dataframe_as_table(conn, cursor, data, survey_new_title)
if(add_dataframe==True):
add_foreign_key = add_foreign_key(conn, cursor, 'SURVEYS', survey_new_title, 'SURVEY_ID', 'SURVEY_ID')
# Specify _INDEX as a primary key so it can be used as foreign key in the child table
add_primary_key = add_primary_key_for_main_survey(conn, cursor, survey_new_title, '_INDEX')

此脚本运行正常,没有任何错误。以下是add_dataframe_as_table()add_foreign_key()add_primary_key_for_main_survey()脚本:

将数据帧添加为表:

def add_dataframe_as_table(conn, cursor, df, survey_title):
try:
print("Number of rows: "+str(len(df)))
start_time = time.time()
data.to_sql(name=survey_title, 
con=engine.connect(), 
if_exists="replace", 
# dtype=object,
index=False)
print(survey_title+' to_sql() done.'+'---n', file=logfile)
print("--- %s seconds ---" % (time.time() - start_time)+'---n', file=logfile)
print(survey_title+' to_sql() done.'+'---n')
print("--- %s seconds ---" % (time.time() - start_time)+'---n')
return True
# log_frame.append({'Survey Name': filename, 'Survey ID': nextval}, ignore_index=True)
except (RuntimeError, TypeError, NameError, snowCtx.connection.errors.Error) as e:
print("******* Error adding the table: "+ str(e)+'*******n', file=logfile)
print("******* Error adding the table: "+ str(e)+'*******n')
conn.rollback()
return False

通过外键将主表连接到SURVEYS表(此处工作良好(:

def add_foreign_key(conn, cursor, reference_table_name, child_table_name, child_table_key_name, reference_table_key_name):
print('Add_Foreign_Key')
try:
print('--- Adding a foreign key between the child table: '+child_table_name+ ' and the reference table: '+reference_table_name+' with foreign key as: '+child_table_key_name+' ---n')
print('--- Adding a foreign key between the child table: '+child_table_name+ ' and the reference table: '+reference_table_name+' with foreign key as: '+child_table_key_name+' ---n', file=logfile
)
child_table = """+database+"".""+schema+"".""+child_table_name+"""
print(child_table)
query = """ALTER TABLE {0} ADD FOREIGN KEY ({1}) REFERENCES {2}({3})"""
.format(child_table.replace(''', ''''), child_table_key_name, reference_table_name, reference_table_key_name)
print("Foreign Key Query: "+query+"n")
print("Foreign Key Query: "+query+"n", file=logfile)
if(cursor.execute(query)):
print("--- "+child_table+ " FOREIGN KEY added successfully"+" ---n")
print("--- "+child_table+ " FOREIGN KEY added successfully"+" ---n", file=logfile)
return True
else:
return False
except (RuntimeError, TypeError, NameError, snowCtx.connection.errors.Error) as e:
print("******* Error when adding foreign key: "+ str(e)+" *******n")
print("******* Error when adding foreign key: "+ str(e)+" *******n", file=logfile)
return False

指定主表的主键,以便在子表中用作外键:

def add_primary_key_for_main_survey(conn, cursor, main_table, main_column):
print('------- Adding a primary key for: '+main_table+' ---n')
print('------- Adding a primary key for: '+main_table+' ---n', file=logfile)
try:
main_table = """+database+"".""+schema+"".""+main_table+"""
query = """ALTER TABLE {0} ADD PRIMARY KEY ({1})""".format(main_table.replace(''', ''''), main_column);
if(cursor.execute(query)):
print('------- Primary key: '+main_column+' has been successfully added to the table: '+main_table+' ---n')
print('------- Primary key: '+main_column+' has been successfully added to the table: '+main_table+' ---n', file=logfile)
return True
except (RuntimeError, TypeError, NameError, snowCtx.connection.errors.Error) as e:
print('******* Error on adding primary key: '+str(e)+' *******n')
print('******* Error on adding primary key: '+str(e)+' *******n', file=logfile)
conn.rollback()
return False

现在在儿童桌一侧:

add_dataframe = add_dataframe_as_table(conn, cursor, data, survey_new_title)
print(main_survey, survey_new_title) # printing correct names
# Getting the error in here
add_foreign_key(conn, cursor, main_survey, survey_new_title, '_PARENT_INDEX', '_INDEX')

编写时:

add_foreign_key = add_foreign_key(conn, cursor, 'SURVEYS', survey_new_title, 'SURVEY_ID', 'SURVEY_ID')

然后您擦除函数add_foreign_key,其结果为布尔。因此进一步呼吁对此提出例外。

相关内容

  • 没有找到相关文章

最新更新