Using try catch to test valid sql date

mmm just wanted to check if it does sql validation for strings :frowning:

no

I try to NEVER use string concatenation to do sql and always try to use prepared statements so sql injection isnā€™t an issue

i was looking for SQL date time format validation, before i send it to graphQL or REST API. i donā€™t talk to servers directly.
maybe there is something in sqllite plugins about daata validation

Iā€™d look into date.sqldate or sqldatetime or the newer date time.sqldate sqldatetime

thats exactly what those are for

2 Likes

yes but i just need to check if a string is a valid sql date time
if i create a xojo date from not valid string i have exception.

image

catch the exception and write code to say ā€œthis isnā€™t validā€ ?

2 Likes

As Norman said, you have to catch (intecept) the exception and use that to indicate the date string is not valid. Something like

SQLDateTime = "20199908-01 11:00"
#Pragma BreakOnExceptions False //so that a debug run does not stop on the execption
Try
  myDate1 = DateTime.FromString(SQLDateTime)
Catch
  //place code here to handle invalid dates
  Return //usually you want to exit or otherwise skip any following code when the date is bad
End Try

I usually place something like this in a global method that returns the converted date value, or Nil if it was invalid. This makes it easier to use without cluttering your code with the Try/Catch structure.

1 Like

thanks ! after posting i was trying this from doc
image
didnā€™t work will try yiur code

i never use Tryā€¦ iā€™m not used too, it feels like iā€™m creating a bug and try to catch it before it occursā€¦

mmm in fact i really thought there was some way of checking if a string is a valid string for sql.
is this method witth exepctions a common practice for this problem ?
thanks a lot

I think the problem is that the documentation is wrong - it produces an IllegalArgumentException instead of a RunTimeExecption. But in this case, the specifioc execption and itā€™s message are inconsequetial. Any kind of exception means the date string is not valid. Thatā€™s why my code doesnā€™t catch any specific exception.

I agree. I find relying on exceptions to be unsettling.

In API1 they have ParseDate, which returns a boolean to indicate whether the string can be parsed into a valid date. And it is more forgiving. I still use it for checking and coercing a user entered date string into a proper date, for speedier data entry rather than using a date picker.

Pretty much.

1 Like

when debugging and you have Break On Exceptions ENABLED yes the code will still stop at that exception

BUT if you press resume then you can keep debugging and see that the try/catch is indeed working

2 Likes

Try-Catch is your friend. While it requires some additional lines of code, it shields users of your app from unpleasant experiences that arise from invalid data or other issues, depending on the exception type.

1 Like