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?

Ivan
  • 7,448
  • 14
  • 69
  • 134
  • 2
    Unlike other DB-APIs, pyodbc does not [support named parameters](https://github.com/mkleehammer/pyodbc/issues/227). Use `?` placemarker instead and pass list into *params* argument of pandas' `read_sql`. – Parfait Oct 06 '19 at 18:30

1 Answers1

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)