ColdFusion 10 - 在插入查询上查询cfloop

时间:2013-04-10 14:08:53

标签: mysql coldfusion coldfusion-10 cfspreadsheet cfloop

首先发布在这里。我正在使用<CFSPREADSHEET>从Excel创建查询,然后尝试将查询结果插入MySQL表。这工作正常。我还需要从一列中取一个值并将其正确的ID号插入表中的ID列。这是我的代码。我得到的结果是在每个表行中插入相同的ID(6)。这里的帮助肯定会受到赞赏。谢谢!

<cfspreadsheet action="read" 
 src="#myFile#" 
 sheet="5" 
 excludeheaderrow="true" 
 headerrow="1" 
 query="newQuery" />

<cfloop query="newQuery">
  <!--- set ID variables --->
  <cfif newQuery.Branch EQ "Army"> 
    <cfset variables.Branch_ID = 6 />
  <cfelseif newQuery.Branch EQ "Marine Corps">
    <cfset variables.Branch_ID EQ 9 />
  <cfelseif newQuery.Branch EQ "Navy">
    <cfset variables.Branch_ID EQ 7 />
  <cfelseif newQuery.Branch EQ "Air Force">
    <cfset variables.Branch_ID EQ 8 />
  <cfelseif newQuery.Branch EQ "Coast Guard">
    <cfset variables.Branch_ID EQ 10 />
  </cfif>

  <cfquery name="importXLS" datasource="memorials_mysql"> 
  INSERT INTO honorees_temp(FirsttName,MName,LastName,Branch,Branch_ID,Unit) 
  VALUES 
  ('#Trim(FirstName)#', 
   '#Trim(MName)#', 
   '#Trim(LastName)#',
   '#Trim(Branch)#', 
   #variables.Branch_ID#,  
   '#Trim(Unit)#') 
  </cfquery> 
</cfloop>

2 个答案:

答案 0 :(得分:4)

一个问题是您的cfset语句中存在语法错误。在cfset中,您需要使用=代替EQ

<!--- set ID variables --->
    <cfif newQuery.Branch EQ "Army"> 
        <cfset variables.Branch_ID = 6 />
    <cfelseif newQuery.Branch EQ "Marine Corps">
        <cfset variables.Branch_ID =9 />
    <cfelseif newQuery.Branch EQ "Navy">
        <cfset variables.Branch_ID =7 />
    <cfelseif newQuery.Branch EQ "Air Force">
        <cfset variables.Branch_ID = 8 />
    <cfelseif newQuery.Branch EQ "Coast Guard">
        <cfset variables.Branch_ID =10 />
    </cfif>

你对newQuery.Branch的价值是什么?如果你的if语句中没有匹配,那么它将保留最后一个值,因为它没有被重新初始化

您还应该在INSERT语句中使用<cfqueryparam>

此外,这更像是个人偏好,但使用cfswitch比使用多个else if语句更有效。

<cfswitch expression="#trim(newQuery.Branch)#">
  <cfcase value="Army" >
    <cfset Branch_ID = 6 />     
  </cfcase>
  <cfcase value="Marine Corps" >
    <cfset Branch_ID = 9 />     
  </cfcase>
  <cfcase value="Navy" >
    <cfset Branch_ID = 7 />     
  </cfcase>
  <cfcase value="Air Force" >
    <cfset Branch_ID = 8 />     
  </cfcase>
  <cfcase value="Coast Guard" >
    <cfset Branch_ID = 10 />    
  </cfcase>
  <cfdefaultcase>
    <!--- set either a default value or throw an error, depending on your needs --->
    <cfset Branch_ID = 0>
    <cfthrow message="Invalid Branch_ID">
  </cfdefaultcase>
</cfswitch>

答案 1 :(得分:0)

每次循环时将variables.brand_id重置为0。您可能没有在IF语句中获得匹配,因此它保留了每个未来不匹配记录的最后一个匹配值。

<cfspreadsheet action="read" 
               src="#myFile#" 
         sheet="5" 
         excludeheaderrow="true" 
         headerrow="1" 
         query="newQuery" />
<cfloop query="newQuery">
    **<cfset variables.Branch_id=0>**
        <!--- set ID variables --->
        <cfif newQuery.Branch EQ "Army"> 
            <cfset variables.Branch_ID = 6 />
        <cfelseif newQuery.Branch EQ "Marine Corps">
            <cfset variables.Branch_ID = 9 />
        <cfelseif newQuery.Branch EQ "Navy">
            <cfset variables.Branch_ID = 7 />
        <cfelseif newQuery.Branch = "Air Force">
            <cfset variables.Branch_= EQ 8 />
        <cfelseif newQuery.Branch EQ "Coast Guard">
            <cfset variables.Branch_ID = 10 />
        </cfif>

    <cfquery name="importXLS" datasource="memorials_mysql"> 
            INSERT INTO honorees_temp(FirsttName,MName,LastName,Branch,Branch_ID,Unit) 
            VALUES 
              ('#Trim(FirstName)#', 
               '#Trim(MName)#', 
               '#Trim(LastName)#',
               '#Trim(Branch)#', 
                #variables.Branch_ID#,  
               '#Trim(Unit)#') 
</cfquery> 
</cfloop>