Android Question Basic SQL function turns me crazy

Serge Bertet

Member
Licensed User
Hello all ...

I'm unable to make this function working. I have a big database where I want to add a UIDS table but no way. The myDb database is initialized in Starter Service_Create and this sub is called from Main on_create if firstTime.

B4X:
' Get user's UID.
Sub UID As String
Dim myUID, sqlStr As String
    ' Informations table
    myDb.ExecNonQuery("CREATE TABLE IF NOT EXISTS UIDS (ID INTEGER PRIMARY KEY AUTOINCREMENT, UID TEXT)")
    sqlStr = "SELECT count(*) FROM UIDS"
    Dim cnt As Int = myDb.ExecQuerySingleResult(sqlStr)
    If cnt = 0 Then ' <-- always = 0
        myUID = su.EncodeUrl("_" & Rnd(1000000, 9999999) & "_", "UTF8")
        sqlStr = "INSERT INTO UIDS (UID) VALUES ('" & myUID & "')"
        myDb.ExecQuery(sqlStr)
    End If
    sqlStr = "SELECT UID FROM UIDS LIMIT 1"
    myUID = myDb.ExecQuerySingleResult(sqlStr)
    Return su.DecodeUrl(myUID, "UTF8")' <-- here myUID is Null
End Sub

I tried with KeyValueStore initialized just after myDb, it is working.
This UID is intended to be used with LicenseChecker. Is KVS more or less secure than SQL for that purpose?

Thanks.
 

RB Smissaert

Well-Known Member
Licensed User
Longtime User
Hello all ...

I'm unable to make this function working. I have a big database where I want to add a UIDS table but no way. The myDb database is initialized in Starter Service_Create and this sub is called from Main on_create if firstTime.

B4X:
' Get user's UID.
Sub UID As String
Dim myUID, sqlStr As String
    ' Informations table
    myDb.ExecNonQuery("CREATE TABLE IF NOT EXISTS UIDS (ID INTEGER PRIMARY KEY AUTOINCREMENT, UID TEXT)")
    sqlStr = "SELECT count(*) FROM UIDS"
    Dim cnt As Int = myDb.ExecQuerySingleResult(sqlStr)
    If cnt = 0 Then ' <-- always = 0
        myUID = su.EncodeUrl("_" & Rnd(1000000, 9999999) & "_", "UTF8")
        sqlStr = "INSERT INTO UIDS (UID) VALUES ('" & myUID & "')"
        myDb.ExecQuery(sqlStr)
    End If
    sqlStr = "SELECT UID FROM UIDS LIMIT 1"
    myUID = myDb.ExecQuerySingleResult(sqlStr)
    Return su.DecodeUrl(myUID, "UTF8")' <-- here myUID is Null
End Sub

I tried with KeyValueStore initialized just after myDb, it is working.
This UID is intended to be used with LicenseChecker. Is KVS more or less secure than SQL for that purpose?

Thanks.

This should work:

B4X:
        sqlStr = "INSERT INTO UIDS(UID) VALUES(?)"
        myDb.ExecNonQuery2(sqlStr, Array As String(myUID))

RBS
 
Upvote 0

Serge Bertet

Member
Licensed User
Thank you, you'r rigth it was ExecNonQuery instead of ExecQuery ...
My database class has one thousand of lines of code working (written months ago) and unable to add a single function for a small mistake like that :mad:
You make my day.
 
Upvote 0
Top