I'm using pypyodbc to select data from an access database. I am using the following query with three parameters which have been specified.
I've tried a few varieties, but to no avail. I don't see anything wrong with my syntax.
SELECT [Date], [Time], [uSec], [threeR], [twoCV]
FROM [table_a]
WHERE (Date = ? AND Time > ?)
OR (Date > ?)
Parameters are of the following types:
[datetime.date, datetime.time, datetime. date]
Which, when printed:
1900-09-16 , 00:00:00, 1900-09-16
pypyodbc.DatabaseError: ('07002', '[07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 4.')
#-- Begin Python code sample
#-- Checks the DB file and retrieves data
def pullData(self):
#-- Connect to Access
con = pypyodbc.connect('Driver={Microsoft Access Driver (*.mdb)};DBQ=F:/database.mdb')
cur = con.cursor()
#-- Get column list
columnListODBC = '[thisDate], [thisTime]'
for y in myTable.getColumns():
columnListODBC = columnListODBC + ', [' + y + "]"
#-- See footnote 1
print(columnListODBC)
#-- Get the most recent SQL entry
for row in curSQL.execute('SELECT MAX(Datetime) FROM [' + _.getName() + ']'):
xDateTime = datetime.datetime.strptime(row[0], "%Y-%d-%m %H:%M:%S")
day = xDateTime.date() # Get only the DATE of the most recent entry
time = xDateTime.time() # Get only the TIME of the most recent entry
#-- Pull all ODBC data
queryString = 'SELECT ' + columnListODBC + ' FROM [' + _.getName() + '] WHERE (thisDate = ? AND thisTime > ?) OR (thisDate > ?)'
#-- See footnote 2
print(queryString, ", ", day, ", ", time)
cur.execute(queryString, [day,time,day])
Print 1: [thisDate], [thisTime], [uSec], [threeR], [twoCV]
Print 2: SELECT [thisDate], [thisTime], [uSec], [threeR], [twoCV] FROM [table_a] WHERE (thisDate = ? AND thisTime > ?) OR (thisDate > ?) , 1900-09-16 , 00:00:00
Edit: While toying around it seems to successfully execute when I remove one of the columns. Although both columns exist in the source table. This doesn't answer the question as to why the original query does not execute.
SELECT [Date], [Time], [uSec], [twoCV]
FROM [table_a]
WHERE (Date = ? AND Time > ?)
OR (Date > ?)
Edit 2: Changing the name of the Date and Time columns has not made a difference. The following still gives the error:
SELECT [thisDate], [thisTime], [uSec], [threeR], [twoCV]
FROM [table_a]
WHERE ([thisDate] = ? AND [thisTime] > ?)
OR ([thisDate] > ?)
[Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 5.
Edit 3: Here is the design view of the table it is pulling from.