Python和雪花光标正在添加额外的文本来更改表外键查询,这导致找不到表本身



我正在构建一个管道,将数据从本地文件夹上传到雪花云中。

上传每个表后的最后一步是更改以CSV文件名命名的表本身,向主表添加外键。我正在做以下事情:

query = """ALTER TABLE """+filename+""" ADD FOREIGN KEY (SURVEY_ID) REFERENCES SURVEYS(SURVEY_ID)"""
if(cursor.execute(query)):
print(filename+ " FOREIGN KEY added successfully")
else:
print(filename+" FOREIGN KEY was not added")

我有以下错误:

表'MY_DB.PUBLIC.SURVEY_AST'不存在或不存在授权。

我知道表名应该如上所述,以便找到并执行查询:

"MY_DB"公共"SURVEY_ AST";

如果我打印filename,我将得到:

SURVEY_AST

我尝试使用%s,但无法使用雪花光标绑定表名本身

query = """ALTER TABLE %s ADD FOREIGN KEY (SURVEY_ID) REFERENCES SURVEYS(SURVEY_ID)"""
if(cursor.execute(query, (filename)):
print(filename+ " FOREIGN KEY added successfully")
else:
print(filename+" FOREIGN KEY was not added")

如何让引擎停止将额外的文本绑定到表本身,以便找到它并执行查询?

请注意,前面的查询工作正常(INSERT、SELECT…(

编辑

conn = snowCtx.connect(
user=user,
password=password,
account=account,
database='MY_DB',
schema='PUBLIC',
warehouse='COMPUTE_WH',
role='SYSADMIN'
)
if(success):
print(filename+' columns uploaded')
# UPDATE now variable
now = date.today().strftime("%Y-%m-%d")
# To insert the data files into the new table
data = data.applymap(str)
data['_xform_id'] = data['_xform_id'].apply(lambda x: int(float(x)))
data['SURVEY_ID'] = data['SURVEY_ID'].apply(lambda x: int(str(x)))
try:
data.to_sql(name=filename, 
con=engine.connect(), 
if_exists="replace", 
index=False)
print(filename+' to_sql() done.')
print("--- %s seconds ---" % (time.time() - start_time))
log_frame.append({'Survey Name': filename, 'Survey ID': nextval}, ignore_index=True)
except sqlalchemy.exc.Error as e:
print("Error adding the table: "+ e)
# Add a foreign key
finally:
# query = "select count (*) from information_schema.tables WHERE table_type = 'BASE TABLE' AND table_name="+filename
print(filename)
query = """ALTER TABLE {0}  ADD FOREIGN KEY (SURVEY_ID) REFERENCES SURVEYS(SURVEY_ID)""".format(filename.replace(''', ''''))
print(query)
if(cursor.execute(query)):
print(filename+ " FOREIGN KEY added successfully")
else:
print(filename+" FOREIGN KEY was not added")

我不认为它添加了任何额外的文本。正如我所看到的,错误消息包括数据库和模式名称(这是预期的(。例如:

create table DENEME ( i integer );
alter table DENEME2 add column z varchar;
SQL compilation error: Table 'GOKHAN_DB.PUBLIC.DENEME2' does not exist or not authorized.

请检查查询历史以查看SQL文本:

https://docs.snowflake.com/en/sql-reference/functions/query_history.html

你认为表名应该像下面这样吗?

"MY_DB"公共"SURVEY_ AST";

它与MY_DB.PUBLIC.SURVEY_AST没有什么不同(因为所有字符都是大写的(。所以它应该起作用。请检查前面的命令,看看您是如何创建表的(可能是在不同的架构中创建的(。

解决方案是对filename本身执行以下操作:

filename = """+database+"".""+schema+"".""+filename+"""

确切的结果是:

"MY_DB"."SCHEMA"."TABLENAME"

相关内容

  • 没有找到相关文章

最新更新