Enter date and time in MySQL using ColdFusion

I am completely lost here.

There is a field of type "datetime" in the MySQL database. I want to populate its datetime with ColdFusion. I found that CreateODBCDateTime must be used to convert to maint format so that MySQL accepts it, so ...

<cfset myDateTime = CreateODBCDateTime("07-04-2012 20:11:00")>

And somewhere later:

<cfquery name="qAddDate">
    INSERT INTO some_table
    (`date`)
    VALUES
    ('#myDateTime#')
</cfquery>

However, I get this error when trying to send data to the database:

You have an error in the SQL syntax; check the manual that matches your version of MySQL server for the correct syntax to use next to '2012-07-04 20:11:00'} ')' on line 8

Line 8 is the date line:

INSERT INTO some_table
(`date`)
VALUES
('{ts '2012-07-04 20:11:00'}')

Can anyone help?

Thank.

+3
source share
3 answers

, . :

  INSERT INTO some_table (`date`)
  VALUES ( #myDateTime# )

cfqueryparam

  INSERT INTO some_table (`date`)
  VALUES ( <cfqueryparam value="#myDateTime#" cfsqltype="cf_sql_timestamp"> )

... , / , createODBCDate :

  INSERT INTO some_table (`date`)
  VALUES ( <cfqueryparam value="07-04-2012 20:11:00" cfsqltype="cf_sql_timestamp"> )
+10

" ", . , MySQL, .

(`date`)

:

INSERT INTO some_table(date)
VALUES ('{ts '2012-07-04 20:11:00'}')
0

The following code snippet and display output (bottom of message)

display and update MySQL date field using ColdFusion 10.

<CFOUTPUT>

  <CFSET mLST_UPDT = CreateODBCDate("07/04/2012")>

  Date for SQL Insert = #mLST_UPDT# <br>

<!---- INSERT code without use of cfqueryparam ---->
<CFQUERY NAME="TAB_INSERT" DATASOURCE="HELMSFARMS">
INSERT INTO DATE_TABLE (LST_UPDT) VALUES (#mLST_UPDT#)
</CFQUERY>

<!---- INSERT code using cfqueryparam ---->
<CFQUERY NAME="TAB_INSERT" DATASOURCE="HELMSFARMS">
INSERT INTO DATE_TABLE
(LST_UPDT)
VALUES (<cfqueryparam value="#mLST_UPDT#" cfsqltype="cf_sql_date"> ) 
</CFQUERY>

<!---- Getting and Displaying results of INSERT ---->
<CFQUERY NAME="GET_DATES1" DATASOURCE="HELMSFARMS">
SELECT LST_UPDT FROM DATE_TABLE  
</CFQUERY>

<CFLOOP QUERY="GET_DATES1">
#CURRENTROW#. #GET_DATES1.LST_UPDT# #dateformat(GET_DATES1.LST_UPDT,"mm/dd/yyyy")#
<br>
</CFLOOP>

<!---- Code that uses UPDATE on the records INSERTED above ---->
<cfset mNXT_UPDT = CreateODBCDate("08/05/2014")>
<br>
Date for SQL Update = #mNXT_UPDT# <br>

<!---- UPDATE code using cfqueryparam ---->
<CFQUERY NAME="GET_DATES2" DATASOURCE="HELMSFARMS">
UPDATE DATE_TABLE SET
LST_UPDT = <cfqueryparam value="#mNXT_UPDT#" CFSQLType="cf_sql_date" > 
</CFQUERY>

<CFQUERY NAME="GET_DATES2" DATASOURCE="HELMSFARMS">
SELECT LST_UPDT FROM DATE_TABLE  
</CFQUERY>

<CFLOOP QUERY="GET_DATES2">
#CURRENTROW#. #GET_DATES2.LST_UPDT# #dateformat(GET_DATES2.LST_UPDT,"mm/dd/yyyy")#<br>
</CFLOOP>

</CFOUTPUT>  

<!----- Results of running above code 
Used a MySQL table named DATE_TABLE with 
one Date Field named LST_UPDT
--------------------------------------
Date for SQL Insert = {d '2012-07-04'} 
1. {ts '2014-08-05 00:00:00'} 08/05/2014
2. {ts '2014-08-05 00:00:00'} 08/05/2014
3. {ts '2014-08-05 00:00:00'} 08/05/2014
4. {ts '2014-08-05 00:00:00'} 08/05/2014
5. {ts '2012-07-04 00:00:00'} 07/04/2012
6. {ts '2012-07-04 00:00:00'} 07/04/2012
Date for SQL Update = {d '2014-08-05'} 
1. {ts '2014-08-05 00:00:00'} 08/05/2014
2. {ts '2014-08-05 00:00:00'} 08/05/2014
3. {ts '2014-08-05 00:00:00'} 08/05/2014
4. {ts '2014-08-05 00:00:00'} 08/05/2014
5. {ts '2014-08-05 00:00:00'} 08/05/2014
6. {ts '2014-08-05 00:00:00'} 08/05/2014
---->
0
source

All Articles