给出以下查询字符串,
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
是标记参数的方法吗?
答案 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)