iOS Question SQL elimintes the previous "0"

schimanski

Well-Known Member
Licensed User
Longtime User
Why does the sql eliminates the previous zero in the following example, if the value is a numer (0201 =201)? Is it possible to hold the previous zero to save telephonenumbers etc.?

B4X:
Sub Process_Globals
    Private sql1 As SQL
End Sub

Private Sub Application_Start (Nav As NavigationController)   
    Initialize
   
    PutSimple("Wert1", "0201")
    Log(GetString("Wert1"))
   
    PutSimple("Wert2", "0hallo")
    Log(GetString("Wert2"))
End Sub

Public Sub Initialize
    If sql1.IsInitialized Then sql1.Close
    sql1.Initialize(File.DirLibrary, "pm.dat", True)
    CreateTable
End Sub

Public Sub SetString(key As String, value As String)
    PutSimple(key, value)
End Sub

Public Sub GetString(key As String) As String
    Return GetSimpleString(key)
End Sub

Private Sub CreateTable
    sql1.ExecNonQuery("CREATE TABLE IF NOT EXISTS main(key TEXT PRIMARY KEY, value NONE)")
End Sub

Private Sub getCursor(Key As String) As ResultSet   
    Return sql1.ExecQuery2("SELECT value FROM main WHERE key = ?", Array As String(Key))
End Sub

Private Sub GetSimpleString(Key As String) As String   
    Dim c As ResultSet = getCursor(Key)
    If c.NextRow = False Then
        c.Close
        Return ""
    End If
    Dim res As String = c.GetString2(0)
    c.Close
    Return res
End Sub

Private Sub PutSimple(Key As String, Value As Object) As Boolean
    Try
        start(Key)
        insertQuery(Key, Value)
        Return complete(True)
    Catch
        Return complete(False)
    End Try
End Sub

Private Sub start (Key As String)
    sql1.BeginTransaction
    sql1.ExecNonQuery2("DELETE FROM main WHERE key = ?", Array As Object(Key))
End Sub

Private Sub insertQuery(Key As String, Value As Object)
    sql1.ExecNonQuery2("INSERT INTO main VALUES(?, ?)", Array As Object(Key, Value))
End Sub

Private Sub complete(success As Boolean) As Boolean
    If success Then
        sql1.TransactionSuccessful
    Else
        sql1.Rollback
    End If
    Return success
End Sub

Public Sub Close
    sql1.Close
End Sub
 

sorex

Expert
Licensed User
Longtime User
you'll need to store it as text. Is this SQLite or MySQL/MSSQL?

you can always add a space at the end maybe it gets treated as text then. (SQLite is kind of autosensing)
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
I'm using the iSQL-Library Version 1.01. I think, it is SQLite, like b4A.

Yes, if I add some kind of letters, it will be saved as string. If there is no other solution, I add a 'salt'.

Thanks for response...
 
Upvote 0

sorex

Expert
Licensed User
Longtime User
try some other char and replace it with nothing ("") when you need to display it.
 
Upvote 0

schimanski

Well-Known Member
Licensed User
Longtime User
Here is a workaround, that runs:

B4X:
Public Sub SetString(key As String, value As String)
    PutSimple(key, "_" & value)    
End Sub

Public Sub GetString(key As String) As String
    Return GetSimpleString(key).SubString(1)    
End Sub

Thank you for your efforts...
 
Upvote 0
Top