SQL查询由于某种原因而中断

1 mysql coldfusion

请考虑以下图片:

在此输入图像描述

当我在coldfusion中运行以下查询时:

<cfquery datasource="mydb" name="qCoulmnInsert">
INSERT INTO 
simplexresults.contactresults_email_account_summary_devices 
(open_desktop_int,
open_other_int,
open_phone_int,
open_tablet_int,
open_webmail_int,  
<!--- FOR Unique Open --->
unique_webMail_int,
unique_tablet_int,
unique_other_int,
unique_phone_int,
unique_desktop_int,
<!--- FOR DATES --->
startdate_dt,
enddate_dt,
date_dt)

VALUES
<!--- loop through your array --->
<cfloop from="1" to="#arrayLen(cfData)#" index="i">
(
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i].open.Desktop#">
<cfelse>
NULL
</cfif> ,
<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i].open.Other#">
<cfelse>
NULL
</cfif> ,


<cfif structKeyExists(cfData[i], "open")>
<cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i].open.Phone#">
<cfelse>
NULL
</cfif> ,

  and so on and so forth ...
                   ....

</cfquery>
Run Code Online (Sandbox Code Playgroud)

我收到以下错误:

 Element OPEN.DESKTOP is undefined in a CFML structure referenced as part of an expression.

The error occurred in C:\myfile.cfm: line 55

53 :              (
54 :               <cfif structKeyExists(cfData[i], "open")>
55 :                 <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i].open.Desktop#">
56 :               <cfelse>
57 :                 NULL
Run Code Online (Sandbox Code Playgroud)

我知道它没有为一些结构定义,如下图所示,但我在<cfelse>声明中包含了"NULL" 来处理这些情况.我在这里做错了吗?请指教.

但是,查询在定义OPEN'S的日期范围内运行正常.

正确的方式:

<cfif structKeyExists(cfData[i], "open")>
                <cfif  structKeyExists(cfData[i].open,"Desktop")>
                <cfqueryparam CFSQLTYPE="CF_SQL_INTEGER" value="#cfData[i]["open"]["Desktop"]#">
                <cfelse>
                 NULL
                </cfif>
              <cfelse>
                NULL

              </cfif> ,
Run Code Online (Sandbox Code Playgroud)

Chr*_*ney 7

您认为if语句中提供了"open",但"Desktop"并不总是可用且未包含在限定语句中.换句话说,"桌面"并不总是一个键,但只要定义了"打开"键,你的循环就会一直查找它.

  • @ user3239126:问题不在cfelse中.您收到的错误消息表明"DESKTOP"不是结构中的元素.(你可以在抛出错误的行之前做一个结构的cfdump.)正如Chris Tierney在他的回答中指出的那样,你正在测试"OPEN"是否是一个元素,但是你没有测试是否"DESKTOP"在您引用之前,它是"OPEN"结构的元素. (2认同)
  • 是的.仅仅因为*parent*键即`open`存在,并不意味着子键即`desktop`,`open`也将存在(`desktop`,`open`).你必须*分别对它们进行测试.首先测试父键`structKeyExists(struct,"parentKeyName")`,然后测试子键`structKeyExists(struct.parentKeyName,"childKeyName")`. (2认同)