SQLiteRecordSet if field<>nil

Previously we could load from database safely by checking RecordSet for if theRecordSet.field(“fieldName”)<>nil before continuing.

What is the equivalent of doing this with the new framework? If you attempt to check for if theSQLiteRecordSet.field(“fieldName”)<>nil and the field doesn’t exist, it will throw an invalid argument exception.

How can we check if a recordset has a specific field? Is this another case of changing existing functions to require a bunch of try/catch clauses and reworking all of the already working code?

A quick test shows no problem with it. Note that there is no new framework for the database access.

If you meant iOS, you should have posted to the iOS section or at least mention iOS in the post.

Sorry I was under the impression that SQLiteRecordSet is eventually replacing RecordSet in the new framework. Guess this is iOS specific.

Either way, with the SQLiteRecordSet, I’m getting invalid argument exceptions on the following line:

if rs.Field("test")<>nil then

The recordset doesn’t have the field name, but that’s why I’m trying to check it there. The invalid argument exception’s reason is “test could not be found in the record set”

This seems to be a bug. It is not the test for Nil that fails, it is the argument to rst.Field() that raises the error. The docs say:

[quote]Field(name As Text) As SQLiteDatabaseField
?Gets the SQLiteDatabaseField information for the specified column name.

Field(index As Integer) As SQLiteDatabaseField
Gets the SQLiteDatabaseField information for the specified column index (0-based).[/quote]
… but only an integer index is accepted.

This fails too, whether the column “test” exists or not:
Dim dbf As SQLiteDatabaseField = rst.Field(“test”)

So it seems that this method is not implemented at all.

you could wrap it in try/catch and handle the exception.

It’s working fine to retrieve a SQLiteDatabaseField for me when I pass a text variable and the field exists. It’s just throwing an exception there if it doesn’t exist, which confuses me since I’m just trying to check if it actually exists.

For now I’ve wrapped everything with more try/catch clauses. The amount of exceptions thrown in the new framework are forcing me to turn off break on exceptions, which isn’t a good thing.

The old way:

If rs.field ("test")<>nil then
   value  = rs.field ("test").StringValue
else 
   do_something
end

The new way:

try
   value = rs.field ("test").StringValue
catch err
   do_something 
end 

[quote=193750:@Tim Hare]The new way:
[/quote]

The problem here is that the exceptions caught within try clauses are still causing the debugger to break on exception. I’ve got the code working fine, but with this format I have to turn off break on exception to run any project that previously was coded to check for and prevent these exceptions.

Shouldn’t exceptions be… the exception?

try #pragma BreakOnException false value = rs.field ("test").StringValue #pragma BreakOnException true catch err do_something end

I really can’t buy into these solutions. We would then need separate try/catch clauses for every single field we want to check. Plus any pragmas if we don’t want them ruining the project’s Break on Exception setting.

There should be a way to check if a field exists without throwing an exception. Otherwise exceptions become useless.

Bug report: <https://xojo.com/issue/39708>

I filled case 39710 asking about a HasField function. Maybe that could help?

Thanks, that would also be a good solution

In the mean time, create your own helper method that acts the way the old one does and encapsulates the #pragma. A global find/replace and you’re good to go.

The #pragma BreakOnException false doesn’t actually seem to do anything. It’s still breaking on exception within the clause.

Looks like that was a temporary bug, restarted Xojo and it’s functioning now.

Alright seems that it just randomly works or doesn’t work, as another section of the app is ignoring the pragma.

can you show that in a sample project and make a feedback case?

Been trying to make it completely reproducible for that purpose but it’s inconsistent and I haven’t figure out what exactly causes it yet.