Android Question How to manage null values in SqlLite GetString?

Paolo Pini

Member
Licensed User
Longtime User
Hi all,
I have new record inserted in a SqlLite table.
Most of this fields are empty/null.
I have the same routine to read new and existent records

I try to read the fields in a loop to check if assign to a variables:

B4X:
Dim DbField as string="Field1"

If curor.GetString(DbField)<>Null Then
    textbox.Text=cur.GetString(DbField)
Else
    textbox.Text=""
End If

but the Null check return :Error evaluating expression.

I tried many solution without success.

B4X:
Dim obj As Object=cur.GetString(campodb)
If obj<>Null Then

I read some post that suggest to insert a clause if in the SELECT:

B4X:
SELECT ifnull(col1, "") as Field1 ......

but is seem a bit complex, moreover I have field name not 'hardcoded' etc.

Some other suggestions to check the Null value in a field?

Thanks in advance

Paolo
 

Paolo Pini

Member
Licensed User
Longtime User
Hi all,
I have new record inserted in a SqlLite table.
Most of this fields are empty/null.
I have the same routine to read new and existent records

I try to read the fields in a loop to check if assign to a variables:

B4X:
Dim DbField as string="Field1"

If curor.GetString(DbField)<>Null Then
    textbox.Text=cur.GetString(DbField)
Else
    textbox.Text=""
End If

but the Null check return :Error evaluating expression.

I tried many solution without success.

B4X:
Dim obj As Object=cur.GetString(campodb)
If obj<>Null Then

I read some post that suggest to insert a clause if in the SELECT:

B4X:
SELECT ifnull(col1, "") as Field1 ......

but is seem a bit complex, moreover I have field name not 'hardcoded' etc.

Some other suggestions to check the Null value in a field?

Thanks in advance

Paolo
UPDATE:
I tried to modify the fields property adding DEFAULT 0 for integer and DEFAULT "" for text, seems to work.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
adding DEFAULT 0 for integer
The problem with using a default of 0 for integer is you are actually changing the data in the table. Later on, when someone opens the table and looks at the data, they will not be able to tell if the 0 is from using a default of 0 or it is actually input or came from the field. Unless it is not important in your case, I would avoid the DEFAULT gimmick and do not alter the table. But to avoid crashes, use the INULL or COALESCE functions in your queries.
 
Upvote 0
Top