我有以下代码正试图从Access数据库中提取几个SQL查询
import pyodbc import datetime conx = pyodbc.connect("Driver={Microsoft Access Driver (*.mdb, *.accdb)};DBQ=C:\\Users\\Stuart\\PycharmProjects\\untitled\\Databases\\SandP.accdb;") cursor=conx.cursor() query=""" SELECT DISTINCT Date_ FROM Closing_prices WHERE Date_ >= ? AND Date_ < ?""" params1 = (datetime.date(2011, 8, 10), datetime.date(2014, 4, 30)) cursor.execute(query, params1) dates = list() for date in cursor: dates.append(date[0]) for date in dates: params2 = date cursor = conx.cursor() query= '''SELECT Volatility,Last_price FROM Volatility v,Closing_prices c WHERE c.Date_= ? and v.Date_=c.Date_''' cursor.execute(query,params2) for (vol,price) in cursor: volatility=float(vol) closing_price=float(price) cursor.close() cursor = conx.cursor() if (date.weekday()==4): nextDay=(date + datetime.timedelta(days=3)) else: nextDay=(date + datetime.timedelta(days=1)) query= '''SELECT Date_,Time_, Close_ FROM Intraday_values WHERE (date = ? and time >= ?) or (date = ? and time <= ?)''' params3 = (date,datetime.time(15, 30),nextDay,datetime.time(15, 14)) cursor.execute(query,params3)
最后一点引发以下错误:
Traceback (most recent call last): File "C:/Users/Stuart/PycharmProjects/untitled/Apache - Copy.py", line 67, in <module> cursor.execute(query,params3) pyodbc.Error: ('07002', '[07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 6. (-3010) (SQLExecDirectW)')
该请求正尝试从表中提取Date_,Time_和Close_项,以获取遍历先前创建的日期列表时传递给该请求的日期,以及“日期的15:30之后”的截止时间”和“ 15:14之前”作为“ date + 1”。
首先,当SQL请求中只有4个问号(?)时,为什么会期望有6个参数-我没有正确地形成它吗?
另外,我在datetime.time的参数创建过程中遇到了麻烦。这也是错误地形成的吗?
我有点不懂事了!
将查询更改为后有效
query= '''SELECT Date_,Time_, Close_ FROM Intraday_values WHERE (Date_ = ? and Time_ >= ?) OR (Date_ = ? and Time_ <= ?)'''