首页 > 解决方案 > 如何修复 Snowflake 数据库写入错误:snowflake.connector.errors.ProgrammingError) 001003 (42000)

问题描述

ProgrammingError: (snowflake.connector.errors.ProgrammingError) 001003 (42000): SQL compilation error:
syntax error line 1 at position 13 unexpected 'sample'. [SQL: '\nCREATE TABLE sample (\n\t"Business Address" TEXT\n)\n\n'] (Background on this error at: http://sqlalche.me/e/f405)

已安装必要的软件包:pip install --upgrade snowflake-sqlalchemy

from sqlalchemy import create_engine
engine = create_engine(
    'snowflake://{user}:{password}@{account}/SAMPLE_WORK/public?warehouse=****&role=myrole'.format(user='***',password='****',account='*****') 
)

df.to_sql('sample', engine, if_exists='replace', index=False)

标签: pythonsqlalchemysnowflake-cloud-data-platform

解决方案


SAMPLE是 Snowflake(和 SQL:2003)中的保留关键字,但snowflake-sqlalchemy方言似乎没有正确引用它。一个快速的技巧是将其注入保留字集:

# Before creating the engine etc.
from snowflake.sqlalchemy.base import SnowflakeIdentifierPreparer
# The set uses lower case, though the source set upper.
SnowflakeIdentifierPreparer.reserved_words.add("sample")

推荐阅读