inserting structure elements into database

混江龙づ霸主 提交于 2019-12-25 02:56:08

问题


I am looping through an array of structures as follows:

<cfloop array="#cfData#" index="i">
  <cfif structKeyExists(i, "date")>
    <cfset counter++>
   <cfoutput>#counter#</cfoutput> Date  is: <cfoutput> #i.date#</cfoutput> <br/>
  </cfif>
</cfloop>

Now, I have to insert the values of Date and other keys into my database and I am attempting it like the following:

<cfquery datasource="mydb" dbname="Stats">
    INSERT INTO mydatabase
    VALUES  
       <cfif structKeyExists(cfData, "date")>
           <cfset counter++>#cfData.date#
       </cfif>
       ,
       <cfif structKeyExists(cfData, "delivered")>
           <cfset counter1++>
           #cfData.delivered#
       </cfif>
       ,
       ... and so on for other key values...
  </cfquery>

Is that a correct way of inserting it into the MySQL database?

P.S: You can also refer to my previous thread for more information:

Checking for key existence in structure

UPDATE:

Actually, in order to avoid the columlist mismatch, I decided to test it as following

<cfset KeyList   = "delivered,
                    unique_open,
                    spamreport,
                    drop,
                    request,
                    bounce,
                    deferred,
                    processed,
                    date,
                    startdate,
                    enddate,
                    open,
                    blocked">

<cfloop from="1" to="#arraylen#" index="i">

        <cfloop list="#KeyList#" index="colItem">    
        <cfif structKeyExists(cfData[i], "colItem")>
        <cfoutput>#cfData[i].colItem#</cfoutput>

      <cfelse>
         NULL
      </cfif>
        <cfif colItem neq listLast(KeyList)>,</cfif> 
    </cfloop>
    </cfloop>

Still it shows NULL in the browser.

However, when I test it like the following , I get correct results for delievered: 5 NULL 12 2 1 12 1

<cfloop from="1" to="#arraylen#" index="i">


        <cfif structKeyExists(cfData[i], "delivered")>
        <cfoutput>#cfData[i].delivered#</cfoutput>

      <cfelse>
         NULL
      </cfif>


    </cfloop>

What's wrong with using KeyList elements ?


回答1:


It's a very bad way of inserting data. An sql insert statement looks like this:

insert into mytable
(field1, field2, etc)
values
(value1, value2, etc)

and each specified field needs a matching value. Also, each value needs a matching field.

There is this alternative:

insert into mytable
values
(value1, value2, etc)

But, if you do that you need a value for every field in the table.

You have conditional logic in the values part of your query. If any of your cfif block returns false, you have at least one and possibly two problems. You will definitely have a mismatch between the number of fields and number of values. You will possibly have an extra comma. Either will cause your query to crash.

I think you need a different approach. cfquery has a null attribute that you can use in your conditional logic. I'll let you think about that for awhile.




回答2:


This answer is based upon the array of struts linked to in the question being the data inserted

If you know your expected struct values and name your column names are the same, this would be one solution:

<!--- set your column names in a list --->
<cfset columnList = "date,delivered,open,processed,request,unique_open">

<cfquery datasource="mydb" dbname="Stats">
INSERT INTO mydatabase (#columnList#)
VALUES
  <!--- loop through your array --->
   <cfloop from="1" to="#arrayLen(cfData)#" index="i">
   (
    <!--- loop through the list of columns and see if they exists in the struct --->
    <cfloop list="#columnList#" index="colItem">
      <cfif structKeyExists(cfData[i], colItem)>
        <cfqueryparam value="#cfData[i][colItem]#">
      <cfelse>
         NULL
      </cfif>
        <cfif colItem neq listLast(columnList)>,</cfif>
    </cfloop>
    )<cfif i neq arrayLen(cfData)>,</cfif>
  </cfloop>
</cfquery>

Also in this format i would suggest using #createODBCDate()# before inserting the dates



来源:https://stackoverflow.com/questions/21443661/inserting-structure-elements-into-database

标签
易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!