I have a MySql database with a date column in a table. I would like to update the date column according to user’s entry, here is my code:
if len(FieldDate.text) > 0 then
rec.DateColumn("Expiry") = app.FormatStringToDate(FieldDate.text)
else
dim d as new date
d = nil
rec.DateColumn("Expiry") = d
end if
But the date column seems does not update with a nil date, it remains the original value if there is one.
If I recall that is a “bug” (feature?) in mySQL.
I was the webmaster of a large site years ago, and I had to define a “dummy” date to use, as it would not allow date datatypes to be assigned NIL… but then it was “years ago”
Assuming MySQL doesn’t allow for empty string dates in date fields, why not just return that field in the database to NULL, as suggested by Antonio above? Doesn’t Xojo set a database field to NULL by setting its value to Nil? Like this:
rec.DateColumn("Expiry").Value = Nil
I’m still learning Xojo, so bear with me if this is naive.
The issue is under certain circumstances, you cannot set a mySQL date to anything other than a legit date…I do not know if this is by design or a bug in mySQL… but check Google and you will find tons of discussions about this…
(a mySQL issue, not an Xojo one)
seems 13-Dec-1901 is the smallest date that mySQL will accept (poor design in my opinion)…
If you have the option… I would change it to a TEXT field, and store your own SQLDate formatted string (oh and then NULL will work!)
Thanks all, I have read that thread also. Hoping I did not mislead you. MySQL do accept null for date fields during insert record. Just if I want to empty a date field during update, it does nothing, the code is:
D = nil
Rs.edit
Rs.field(“Expiry”).datevalue = d
Rs.update
With respect to a Date Field, it appears that MySQL differentiates between these three:
""
0
NULL
The first throws an error.
The second is captured by Xojo as dateField.DateValue=Nil or dateField.DateValue=0
The third is captured by Xojo as dateField.Value=Nil
I think that Xojo is interpreting “dateField.DateValue=Nil” as saying there’s a Value there, and that Value is Nil, and the only Value that makes sense as Nil is 0. Whereas, “dateField.Value=Nil” says there’s no Value there at all, so the database field is NULL.
[quote]The properties that get and set the values of specific data types are recommended over Value.
Set Value to Nil to set the field to NULL.[/quote]