熊猫中pyodbc中的参数read_sql_query

时间:2019-10-06 18:13:10

标签: pandas pyodbc

给出以下查询字符串,

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"""

当我尝试执行它时:

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

我得到一个错误:

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

我认为:symbol是标记参数的方法吗?

1 个答案:

答案 0 :(得分:0)

您使用的是什么版本的python?正如您应该能够使用字符串格式一样。

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"""

如果有兴趣,可以在here上进行详细说明。

添加,然后在查询中使用它: df = pd.read_sql_query(query, conn)