简体   繁体   中英

Error Executing database query

What am I doing wrong here:

<cfquery datasource="mydb" name="qCoulmnInsert">
INSERT INTO 
           mytable (delivered_int,unique_open_int,spamreport_int,drop_int,request_int,bounce_int,deferred_int,                      processed_int,date_dt,startdate_dt,enddate_dt,open_int,blocked_int)

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

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


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

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


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


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

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

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

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

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


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



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



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

      )

      <cfif i neq arrayLen(cfData)>,</cfif>
  </cfloop>
</cfquery> 

You can refer to my previous post for more information: inserting structure elements into database

SQL Fiddle for Table Structure: http://sqlfiddle.com/#!2/e255c/1

Where date, start and end date are datetime in my database table.

Error Message:

Error Executing Database Query. You have an error in your SQL syntax; check the manual that corresponds to your MySQL server version for the right syntax to use near '5 5 5 '2014-01-06' 10 ) ( '2014-01-07' 3 ' at line 8

The error occurred in C:\myfile.cfm: line 55
Called from C:\application.cfc: line 386
Called from C:\myfile.cfm: line 55
Called from C:\application.cfc: line 386

53 : VALUES
54 :       <!--- loop through your array --->
55 :      <cfloop from="1" to="#arrayLen(cfData)#" index="i">
56 :      (
57 :       <cfif structKeyExists(cfData[i], "delivered")>

You don't have commas after each element / column in your query.

Your error shows it:

'5 5 5 '2014-01-06' 10 )

Add commas

'5, 5, 5, '2014-01-06', 10 )

That's the key issue.

You need to put a comma between each value in the bracketed values section of your SQL statement.

The comma goes after each if statement so the result would be :

VALUES (NULL,NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL, NULL)

if you had none of your struct keys.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM