使用 SQLAlchemy 查询 Snowflake

问题描述 投票:0回答:1

尝试使用 SQLAlchemy 针对 Snowflake 表进行查询

from sqlalchemy import create_engine, text, Table, MetaData

engine = create_engine(
    'snowflake://{user}:{password}@{account_identifier}/'.format(
        user="",
        password="",
        account_identifier=""
    )
)
try:
    # Create a metadata object
    metadata = MetaData()

    # Create a session
    session = engine.connect()

    # Set Database for session
    session.execute(text('use database SNOWFLAKE_SAMPLE_DATA'))

    # Reflect the table from the database
    table = Table('ORDERS', metadata, autoload_with=engine, schema='TPCH_SF1')


    # Query the table
    orders = session.query(table).all()

这会引发错误

snowflake.connector.errors.ProgrammingError:001059(22023):SQL编译错误: 必须为 TPCH_SF1 指定从数据库开始的完整搜索路径

sqlalchemy snowflake-cloud-data-platform
1个回答
0
投票

这是对我有用的代码。


engine = create_engine(
    'snowflake://{user}:{password}@{account_identifier}/{database}/'.format(
        user="",
        password="",
        account_identifier="",
        database='SNOWFLAKE_SAMPLE_DATA'
    )
)
try:
    # Create a metadata object
    metadata = MetaData()

    # Create a session
    session = engine.connect()

    # Reflect the table from the database
    table = Table('ORDERS', metadata, autoload_with=engine, schema='TPCH_SF1')

    # Query the table
    stmt = select(table).where(table.c.o_orderdate == "1996-05-05")

    for row in session.execute(stmt):
        print("o_orderkey: ", row.o_orderkey)
        print("o_orderstatus: ", row.o_orderstatus)
        print("---------")

finally:
    session.close()
    engine.dispose()
© www.soinside.com 2019 - 2024. All rights reserved.