2015-04-14 74 views
1

我有以下代碼:PYODBC - 太少參數

Late_Students = cursor.execute(''' 
    SELECT Student.Forename, Student.Surname, FORMAT(Event_Date_Time,"Long Time") AS Time_Of_Event 
    FROM Events, Student 
    WHERE FORMAT(Event_Date_Time,"Short Date") = Date() 
    AND Events.RFID = Student.RFID AND 
    Events.In_Or_Out = ? 
    AND FORMAT(Event_Date_Time,"Long Time")>#08:40:00#''','In') 

rows = cursor.fetchall() 
print(rows) 

這是一個非常簡單的,我有很多我的程序喜歡它,但是當我運行該程序,我得到以下錯誤:

Traceback (most recent call last): 
    File "...Coursework System 1.8.py", line 104, in <module> 
    AND FORMAT(Event_Date_Time,"Long Time")>#08:40:00#''','In') 
pyodbc.Error: ('07002', '[07002] [Microsoft][ODBC Microsoft Access Driver] 
         Too few parameters. Expected 3. (-3010) (SQLExecDirectW)') 

當我添加參數,我碰到下面的錯誤告訴我,我有太多的參數:

Traceback (most recent call last): 
    File "...\Coursework System 1.8.py", line 104, in <module> 
    AND FORMAT(Event_Date_Time,"Long Time")>#08:40:00#''','In','','') 
pyodbc.ProgrammingError: ('The SQL contains 1 parameter markers, but 3 
          parameters were supplied', 'HY000') 

我是什麼做錯了?

+0

可能的重複[在Access中使用pyodbc給出「參數太少」錯誤]中的日期(http://stackoverflow.com/questions/28568110/working-with-dates-in-access-using- pyodbc-giving-too-few-parameters-error) –

+0

@PeterWood我試過在那裏發佈的解決方案,但無濟於事。由於某種原因,它仍然無法正常工作。 – Michael

+0

你會得到什麼錯誤? –

回答

0
Long_Time = 'Long Time' 
Short_Date = 'Short Date' 
Todays_Date = time.strftime('%d/%m/%Y') 
Reg_Time = str('#08:40:00#') 
In = 'In' 

Late_Students = ''' 
       SELECT Student.Forename, Student.Surname, FORMAT(Event_Date_Time,?) AS Time_Of_Event 
       FROM Events, Student 
       WHERE FORMAT(Event_Date_Time,?) =? 
       AND Events.RFID = Student.RFID AND 
       Events.In_Or_Out =? 
       AND FORMAT(Event_Date_Time,?)>?''' 

parameters = (Long_Time, Short_Date,Todays_Date, In, Long_Time, Reg_Time) 
cursor.execute(Late_Students, parameters)