10

我需要将以下值替换为选择查询。但我收到了下面提到的错误

self.jobNo = J-12060
qcActivity = C173
self.wrkArea = 1666339

cursor.execute("""SELECT A.MARKERID, D.COMMENTS,A.STATUS,A.X1,A.Y1,A.X2,A.Y2,C.ERRGROUP,C.ERRDESC,c.category
           FROM MDP_ERR_MASTER A,(SELECT MARKERID, MAX(RECNO) maxRECNO FROM MDP_ERR_MASTER where project_code = ':jobno'
           and errorcode like ':jobno_:qcActivity%' AND WORKAREA LIKE ':workarea%'
           GROUP BY MARKERID) B,MDP_ERR_CONFIG C,(SELECT MARKERID, COMMENTS FROM MDP_ERR_MASTER WHERE PROJECT_CODE = ':jobno'
           AND RECNO = 1 AND errorcode like ':jobno_:qcActivity%' AND WORKAREA LIKE ':workarea%') D
           WHERE(A.MARKERID = B.MARKERID And A.RECNO = B.maxRECNO And A.Markerid = D.MARKERID)AND A.PROJECT_CODE = ':jobno'
           AND A.ERRORCODE LIKE ':jobno_:qcActivity%'  AND A.WORKAREA LIKE ':workarea%' AND A.ERRORCODE = C.ERRCODE""",
           {"jobno" : str(self.jobNo),
            "qcActivity" : str(qcActivity),
            "workarea" : str(self.wrkArea)
            })


Traceback (most recent call last):
File "D:\work\venkat\QGIS\Tools\GlobalErrorMarker\globalerrormarker.py", line 272, in      btnDownloadError_Clicked
"workarea" : str(self.wrkArea)
DatabaseError: ORA-01036: illegal variable name/number
4

1 回答 1

21

我认为您误解了绑定变量如何与 Oracle 和 cx_Oracle 一起使用。

Oracle 将诸如:myvarSQL 查询中的表达式识别为绑定变量占位符。当它遇到这种情况时,它会记下它需要这个变量的值才能运行查询,但它仍然可以在没有这个值的情况下继续解析查询。

绑定变量占位符在字符串文字中不起作用。诸如此类的条件project_code = ':jobno'将仅匹配project_code实际六字符字符串的行:jobno,而不管您是否具有jobno定义了名称的绑定参数。相反,您应该编写project_code = :jobno. 不必担心告诉 Oraclejobno必须包含什么类型的值;当您实际为其赋值时,它会检查您是否获得了正确的类型。

在某些地方,您尝试LIKE通过连接绑定变量占位符来构建子句。这种连接仍然可以完成,但必须在 SQL 中使用||运算符来完成。所以,不要写':workarea%',写,写而不是:workarea || '%'':jobno_:qcActivity%',写:jobno || '_' || :qcActivity || '%'

我对您的 SQL 查询进行了这些更改,创建了几个具有足够列以使查询有效的表,然后运行它。我没有数据可以运行它,所以我没有得到任何结果,但数据库至少解析并成功运行了查询。我还对查询进行了格式化,使其更易于阅读:

    cursor.execute("""
        SELECT A.MARKERID, D.COMMENTS, A.STATUS, A.X1, A.Y1, A.X2, A.Y2, C.ERRGROUP, C.ERRDESC, c.category
          FROM MDP_ERR_MASTER A,
               (SELECT MARKERID, MAX(RECNO) maxRECNO
                  FROM MDP_ERR_MASTER
                 WHERE project_code = :jobno
                   AND errorcode like :jobno || '_' || :qcActivity || '%'
                   AND WORKAREA LIKE :workarea || '%'
                 GROUP BY MARKERID) B,
               MDP_ERR_CONFIG C,
               (SELECT MARKERID, COMMENTS
                  FROM MDP_ERR_MASTER
                 WHERE PROJECT_CODE = :jobno
                   AND RECNO = 1
                   AND errorcode like :jobno || '_' || :qcActivity || '%'
                   AND WORKAREA LIKE :workarea || '%') D
         WHERE A.MARKERID = B.MARKERID
           AND A.RECNO = B.maxRECNO
           AND A.Markerid = D.MARKERID
           AND A.PROJECT_CODE = :jobno
           AND A.ERRORCODE LIKE :jobno || '_' || :qcActivity || '%'
           AND A.WORKAREA LIKE :workarea || '%'
           AND A.ERRORCODE = C.ERRCODE""",
        {"jobno" : str(self.jobNo),
         "qcActivity" : str(qcActivity),
         "workarea" : str(self.wrkArea)
        })
于 2013-05-06T12:01:23.770 回答