我需要将以下值替换为select查询。但是我得到了我在下面提到的错误
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
最佳答案
我认为您误解了绑定(bind)变量如何与Oracle和cx_Oracle一起使用。
Oracle将SQL查询中的诸如:myvar
之类的表达式识别为绑定(bind)变量占位符。遇到这种情况时,它将记下该变量需要一个值才能运行查询,但是在没有该值的情况下仍可以继续解析查询。
绑定(bind)变量占位符在字符串文字中不起作用。诸如project_code = ':jobno'
之类的条件将仅匹配project_code
是实际的六个字符字符串:jobno
的行,而不管您是否具有已定义名称jobno
的绑定(bind)参数。相反,您应该编写project_code = :jobno
。不必担心告诉Oracle jobno
必须包含哪种类型的值。实际为它提供值时,它将检查您是否具有正确的类型。
在某些地方,您尝试通过连接绑定(bind)变量占位符来构建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)
})
关于python - DatabaseError : ORA-01036: illegal variable name/number,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/16392288/