Something like this?
Var test As Variant = ""
Var test2 As String = Str(CType(test, Integer)) // 0
Yes, that gives us 0.
But why this gives us ‘abc’ from the database and not 0?
Var test As Variant = "abc"
Var test2 As String = Str(CType(test, Integer)) // 0
Maybe the underlying code is something like this?
Var test As Variant = "abc"
Var test2 As String
If test.IsNumeric Or test.StringValue.Length = 0 Then
test2 = Str(CType(test, Integer))
Else
test2 = test.StringValue
End If
From what I read here:
SQLite uses a more general dynamic type system. In SQLite, the datatype of a value is associated with the value itself, not with its container.
is that SQLite doesn’t care if the column is Integer or Text, if the value is Text, even if the column is Integer, the value itself is Text and needs to be treated that way. Variants are great for this (I think). So
Var test As Variant = 1 //assign a number to variant
Var test2 As String
test = "" //assign empty string
test2 = test.StringValue //we get empty string
test = 123
test = "abc"
test2 = test.StringValue //we get 'abc'
I agree 100% with this. It is not a good practice to define a column Integer and assign Text even if SQLite works this way, just for the reason that if you want the product to scale we will get problems from this design. But a bad practice (define Integer column and assign a text value), if it is allowed by SQLite and Xojo saves the empty string to the database, should pull an empty string from it.
Since SQLite 3.37.0 we can have Strict tables, so in the event of planning for the future (instead of re-purposing an unused column) we can ‘force’ SQLite to work as any other relational database on the planet. Trying to add a string (even an empty one) to an Integer column will get an exception:
cannot store TEXT value in INTEGER column Team.City
Strict is a good option to have to design new SQLite project that can potentially grow to other database engines.
In short:
- it is not recommended to mix text/integer in an integer column even if SQLite allows it
- in SQLite the value itself should define the datatype, not the container (by default, this change if we use STRICT)
- Xojo, as it should, can save any TEXT in an INTEGER SQLite column, and gives back the text correctly but in the case when the String is empty, it returns 0 instead (the reported bug)