Use Parameters on Snowflake Queries with the Python Connector

Поделиться
HTML-код
  • Опубликовано: 14 окт 2024
  • Parameterized queries are very important in the world of data engineering, and it is no different when you’re connecting to your Snowflake database. Luckily, the Snowflake Connector for Python gives us two great ways to use parameters in our queries. These methods will help guard against SQL injection attacks, and will make your queries more robust and efficient. Let’s look at our qmark and numeric parameter styles for querying Snowflake today!
    Related Videos:
    Python on Snowflake - Getting started with the Snowflake Connector for Python
    • Python on Snowflake - ...
    Python on Snowflake: Create and Populate a Database and Retrieve Data
    • Python on Snowflake: C...
    How to Use write_pandas to Migrate Dataframes into Snowflake Databases
    • How to Use write_panda...
    How to Query Snowflake Data into Pandas Dataframes
    • How to Query Snowflake...
    How to Execute Many SQL Statements from a File Using the Python Connector for Snowflake
    • How to Execute Many SQ...
    Use Parameters on Snowflake Queries with the Python Connector
    You are watching this one now!
    How to Use execute_string for Many SQL Statements Using Python on Snowflake
    • How to Use execute_str...
    Python on Snowflake - How to Use executemany with Lists or Tuples for Parameters
    • Python on Snowflake - ...
    Want to work with me 1:1? Contact me today and book your free 20 min consultation!
    Contact form you can find at www.mackenziema...
    Follow me on social media:
    / mackenziedataanalytics
    / seamacke
    / seamacke
    / seamacke
    / psmackenzie
    Get Microsoft Office including Access:
    click.linksyne...
    Got a RUclips Channel? I use TubeBuddy, it is awesome. Give it a try:
    www.tubebuddy....
    For developers looking for a new role, check out:
    www.toptal.com...
    Want my team to do a project for you? Let's get to it!
    system.billzon...
    #Snowflake #ParameterizedQuery #PythonConnector
    • Use Parameters on Snow...

Комментарии • 14

  • @narengarapati2556
    @narengarapati2556 2 года назад +1

    Thanks so much! Great playlist

  • @235sri
    @235sri 3 года назад +1

    Very good explanation, for i’ve a scenario where I’ve to define multiple snowflake data names that I have to use in my query how do I define multiple database??
    USE Database db1, db2, db3..

    • @seanmackenziedataengineering
      @seanmackenziedataengineering  3 года назад

      Good question! You can create several databases and switch between them. You can see some syntax in this video:
      ruclips.net/video/yvdwKpm1P7A/видео.html

  • @Jmignet
    @Jmignet 3 года назад +1

    Great video!
    What is the advantage of using the pandas connector for snowflake?
    Edit: Could we do a similar parameterization using a redshift instance/connector?

    • @seanmackenziedataengineering
      @seanmackenziedataengineering  3 года назад +1

      Thanks! I think the advantage with using the Python connector for snowflake is that there are some neat functions built-in that will use things like storage for staging to make things more efficient.
      However, it is not totally necessary to use the connector; in fact, you can access Snowflake using ODBC and run many of the same commands, pull data directly to pandas, etc. Also, you can use ODBC for Redshift as well.
      ODBC supports parameterization of the first type (1 of 2 in this video) using the ? character. So, to answer your question, the answer is yes, you can use very similar syntax with ODBC to provide safe parameterized queries against Redshift.
      I will be covering ODBC on snowflake in a later video! You can see an example of Python with ODBC here (Oracle example):
      ruclips.net/video/__pWyKvycjc/видео.html

    • @Jmignet
      @Jmignet 3 года назад

      @@seanmackenziedataengineering
      I see. I am sort of new to snowflake but can see it can be a powerful tool for scalability and cross cloud platform capabilities.
      Thank you so much for the information!
      I am looking forward to that next video!

  • @phugacious
    @phugacious 2 года назад +1

    Thank you again sir. However I noticed that you used cs.execute() and passed the parameters. Is there a way to do the same with pandas e.g. in pd.read_sql() method can we parameterize our SQL query??
    Thanks again in advance. Really appreciate all your videos

    • @seanmackenziedataengineering
      @seanmackenziedataengineering  2 года назад

      You can use cs.fetch_pandas_all() to retrieve the results into a pandas dataframe. If you're using Python with ODBC to Snowflake, you can also use read_sql with parameters like you say, but generally it is just easier with fetch_pandas_all() when using the connector.

  • @nikhilm5896
    @nikhilm5896 2 года назад +1

    Is it possible to parameterize the table names?

    • @seanmackenziedataengineering
      @seanmackenziedataengineering  2 года назад

      I don't think I have tried that! Let me know how it goes.

    • @nikhilm5896
      @nikhilm5896 2 года назад +1

      @@seanmackenziedataengineering It did not let me use parameters for table names. But I was able to use the python variable directly in the sql query.
      sql_query = (f"select * from {object_name} limit 1")
      cur.execute(sql_query)

    • @seanmackenziedataengineering
      @seanmackenziedataengineering  2 года назад

      @@nikhilm5896 I see!

    • @luciusanderson5933
      @luciusanderson5933 Год назад +1

      Using Snowflake's identifier function, you parametrize table names:
      snowflake.connector.paramstyle = 'qmark'
      sql = ("select * from identifier(?)")
      cur.execute(sql, ['table_name'])