1

Given the following query string,

symbol = "'AAPL'"

query = """SELECT TOP (1000000) 
       [date]
      ,[symbol]
      ,[open]
      ,[high]
      ,[low]
      ,[close]
      ,[volume]
      ,[exch]
      FROM [AMEXEOD].[dbo].[Stocks_eod]
      where 
          symbol = :symbol
      order by 
         date desc"""

When I try to execute it:

df = pd.read_sql_query(query, conn, params={'symbol': symbol})

I get an error:

('The SQL contains 0 parameter markers, but 1 parameters were supplied', 'HY000')

I thought the :symbol is the way to mark parameters?

1
  • 2
    Unlike other DB-APIs, pyodbc does not support named parameters. Use ? placemarker instead and pass list into params argument of pandas' read_sql. Commented Oct 6, 2019 at 18:30

1 Answer 1

1

What version of python are you using? As you should be able to use string formatting.

Python 2.x

symbol = "'AAPL'"

query = """SELECT TOP (1000000) 
       [date]
      ,[symbol]
      ,[open]
      ,[high]
      ,[low]
      ,[close]
      ,[volume]
      ,[exch]
      FROM [AMEXEOD].[dbo].[Stocks_eod]
      where 
          symbol = {0}
      order by 
         date desc""".format(symbol)

Python 3.x

symbol = "'AAPL'"

query = """SELECT TOP (1000000) 
       [date]
      ,[symbol]
      ,[open]
      ,[high]
      ,[low]
      ,[close]
      ,[volume]
      ,[exch]
      FROM [AMEXEOD].[dbo].[Stocks_eod]
      where 
          symbol = {symbol}
      order by 
         date desc"""

Lots of detail here on this if you're interested.

Add then use this in your query: df = pd.read_sql_query(query, conn)

Sign up to request clarification or add additional context in comments.

1 Comment

this query is not parameterized, and therefore subject to sql injection.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.