0

I'm having trouble getting a multi-line sql query to format correctly using replacement values (%s). Any help in getting the function to iterate through a list of planName, and planData would be helpful!

# importing the libraries
import numpy as np
import pandas as pd
from sqlalchemy import create_engine

#creating the sql engine
engine = create_engine('insert engine creds here')

# creating the plan and plan_data dictionary
dict = {'plan1': 0, 'plan2':, 1}

#creating the function
def plan_usage(planName, planData):
    query = pd.read_sql_query(r'''
        select *
        from blah 
        where (planName = %s and plan_data = %s)''', engine)

My goal would be to have the code run the query for plan1, save the results to a dataframe, then run the query for plan2 and save the results to another dataframe.

  • `%` formatting doesn't work with triple-quoted strings – saud Dec 12 '17 at 15:49
  • 1
    @rottencandy It does actually. But those are DB-API placeholders and the %-operator should not be used here any way. Instead params should be passed as a sequence to [`pandas.read_sql_query()`](https://pandas.pydata.org/pandas-docs/version/0.21/generated/pandas.read_sql_query.html) using the `params=` keyword argument. – Ilja Everilä Dec 12 '17 at 18:49
  • Related https://stackoverflow.com/questions/24408557/pandas-read-sql-with-parameters – Ilja Everilä Dec 12 '17 at 18:56

0 Answers0