带有MS Access的pyodbc期望参数太多

时间:2017-05-13 17:00:12

标签: sql python-3.x ms-access pyodbc

我正在尝试在特定时间范围内查询Access数据库以查找结果。在我查询时,我想自动在输出中添加一列,指定数据所属的周(从几周开始)。通过将参数传递给select语句生成automagic列。我一直在两个错误之间来回走动,我无法弄清楚原因。

错误1)参数太少。

错误2)JOIN中的语法错误

我的python代码:

错误1代码:

qry = '''
    SELECT [PreQuery].[defect], SUM([PreQuery].[qty_rej] + [PreQuery].[qty_rew]) AS [qty_defect], ? AS [wk_no]
    FROM (
        SELECT [Sort].[Date of Sort] AS [srt_date], [Part].[Part Name] AS [prt_no], [Defect Table].[Defect2] AS [defect], [Defect Table].[Quantity Rejected] AS [qty_rej], [Defect Table].[Quantity Reworked] AS [qty_rew]
        FROM (([Defect Table] LEFT JOIN [Sort] ON [Defect Table].[Sort Number] = [Sort].[Sort Number]) LEFT JOIN [Part] ON [Sort].[Part Number] = [Part].[Part Number])) AS PreQuery LEFT JOIN [Defects] ON [Defects].[Defect Code] = [PreQuery].[Defect2]
    WHERE (([Sort].[Date of Sort] BETWEEN ? AND ?) AND ([Part].[Part Name] LIKE ? & '_TZ______'))
    GROUP BY [defect], [wk_no];'''

引发以下错误:

pyodbc.Error: ('07002', '[07002] [Microsoft][ODBC Microsoft Access Driver] Too few parameters. Expected 8. (-3010) (SQLExecDirectW)')

从[排序]中删除别名。[排序日期]和[部件]。[部件名称]再现相同的错误,但驱动程序需要 6 参数而不是 8 和以前一样。在任何一种情况下,整个语句中只有四个,所以DB应该期待4个参数。

错误2代码:

qry = '''
    SELECT ? AS [wk_no], [defect], SUM([qty_rej] + [qty_rew]) AS [qty_defect]
    FROM (
        SELECT [Sort].[Date of Sort], [Part].[Part Name], [Defect Table].[Defect2] AS [defect], [Defect Table].[Quantity Rejected] AS [qty_rej], [Defect Table].[Quantity Reworked] AS [qty_rew]
        FROM (([Defect Table] LEFT JOIN [Sort] ON [Defect Table].[Sort Number] = [Sort].[Sort Number])
        LEFT JOIN [Part] ON [Sort].[Part Number] = [Part].[Part Number]))
    LEFT JOIN [Defects] ON [Defects].[Defect Code] = [Defect Table].[Defect2]
    WHERE (([Sort].[Date of Sort] BETWEEN ? AND ?) AND ([Part].[Part Name] LIKE ? & '_TZ______'))
    GROUP BY [defect], [wk_no];'''

这引起了:

pyodbc.ProgrammingError: ('42000', '[42000] [Microsoft][ODBC Microsoft Access Driver] Syntax error in JOIN operation. (-3510) (SQLExecDirectW)')

我自己检查了内部select语句,它在任何一种情况下执行都很好。此外,我无法看到任何明显的语法错误(PyCharm也无法使用MySQL方言进行检查)。

1 个答案:

答案 0 :(得分:1)

总的来说,两次查询尝试都有几个问题:

  1. 将文字'_TZ______'连接到您的上一个参数值,因为占位符?必须独立存在。
  2. 您需要为派生表 PreQuery 分配一个表别名,就像在第一次查询中一样。
  3. JOIN必须位于新列别名缺陷,而不是缺陷2
  4. 在子查询中移动WHERE子句。
  5. 删除GROUP BY中的 wk_no ,因为它不是实际字段。常量不需要GROUP BY
  6. 考虑使用表别名进行下面的语法调整,以减少长度和可读性。当然没有数据,我无法充分测试。因此,您可能需要进一步调整:

    qry = """
        SELECT ? AS [wk_no], [defect], SUM([qty_rej] + [qty_rew]) AS [qty_defect]
        FROM
          (
            SELECT s.[Date of Sort], p.[Part Name], d.[Defect2] AS [defect], 
                   d.[Quantity Rejected] AS [qty_rej], d.[Quantity Reworked] AS [qty_rew]
            FROM (([Defect Table] d LEFT JOIN [Sort] s ON d.[Sort Number] = s.[Sort Number])
                   LEFT JOIN [Part] p ON s.[Part Number] = p.[Part Number])
            WHERE ((s.[Date of Sort] BETWEEN ? AND ?) AND (p.[Part Name] LIKE ?))
          ) As PreQuery
        LEFT JOIN [Defects] ON [Defects].[Defect Code] = [PreQuery].[defect]        
        GROUP BY [defect];
    """   
    
    cursor.execute(qry, myparams)  # WHERE LAST PARAM HAS '_TZ______' CONCATENATED TO ORIG VALUE