Android Question Strange SQLite behaviour

incendio

Well-Known Member
Licensed User
Longtime User
Hi guys,

I have an app (using JRDC), this app downloaded data from server and then save it to local sqlite database.

Usually this process goes OK, but on a phone, data downloaded OK but not saved on local sqlite database.

Here is the declaration on sqlite table
B4X:
Sub Service_Create
    Private rtp As RuntimePermissions

    Main.PUB_InstDir= rtp.GetSafeDirDefaultExternal("")
    Provider.Initialize
End Sub

Sub Service_Start (StartingIntent As Intent)
    Private dbName As String = "mydata.dat"
    Try
        If File.Exists(Main.PUB_InstDir,dbName) =  False Then
            If File.Exists(File.DirRootExternal & "/APP",dbName) = True Then
                File.Copy(File.DirRootExternal & "/APP",dbName,Main.PUB_InstDir, dbName)
            Else
                File.Copy(File.DirAssets, dbName, Main.PUB_InstDir, dbName)
            End If
        End If
        Main.SQLTbl.Initialize(Main.PUB_InstDir, dbName, False)
    Catch
        ExitApplication
    End Try
End Sub

And here is the codes to download and save to local sqlite

B4X:
Sub UpdateDsc(result As DBResult)
    Dim ListOfMaps As List
    Dim m As Map
    Private IdSrc As Int
    
    ListOfMaps.Initialize
                
    Main.SQLTbl.BeginTransaction
    Try
        Main.SQLTbl.ExecNonQuery("delete from m_dsc")
            
        For Each records() As Object In result.Rows
            IdSrc = records(1)
            m.Initialize
            m.Put("Id",records(0))
            
            If records(0) = 1 Then
                m.Put("Id_Src",Main.PUB_DPT_ID)
            Else
                m.Put("Id_Src",records(1))
            End If
            
            m.Put("Dsc_Nm",records(2))
            m.Put("Dsc",records(3))
                
            ListOfMaps.Add(m)
        Next
            
        DBUtils.InsertMaps(Main.SQLTbl, "m_dsc", ListOfMaps)
        
        Main.SQLTbl.TransactionSuccessful
        LogColor("OK",Colors.Cyan)

    Catch

    End Try
    Main.SQLTbl.EndTransaction
    ProgressDialogHide
End Sub

After that strange behaviour, I installed B4 Bridge on that phone, compile and run again the app while still connected to B4a Bridge, add only this code to Sub UpdateDsc : LogColor("OK",Colors.Cyan), and it worked as expected!

Any of you can shed a light, why this is happen?

My OS is Android 10, B4A 12,2, and DbUtils version 1.09.
 

incendio

Well-Known Member
Licensed User
Longtime User
Whenever you see code with File.DirRootExternal you immediately know that it is broken.

First step is to change all folders to XUI.DirDefault or File.DirInternal.
Sorry, I forgot to mention, that the app, previously stored data in the DirRootExternal, but because there's problems with DirRootExternal (forgot what was the problem), now app moved it's data to Android\data \appname.

The code in Service Start simply look for old data, if it was exist, will copied to folder Android, and data from DirRootExternal won't be used anymore.
 
Last edited:
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
Additional info :

The app was build when no run time permission needed, perhaps it is around Android 4 & 5, so at that time, no runtime permission to checked.

If app install on Android 6 or later, it will installed but no access to write to external storage, users must give this permission manually.
But even permission already given, that strange behaviour still exist.

I changed the codes, on start up, app will ask permission if needed and problem solved.

But mystery remain, on the original app, why sqlite failed to saved data although it has already have a permission to write on external storage?
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
What is the value of targetSdkVersion?
B4X:
AddManifestText(
<uses-sdk android:minSdkVersion="5" android:targetSdkVersion="26"/>
<supports-screens android:largeScreens="true"
    android:normalScreens="true"
    android:smallScreens="true"
    android:anyDensity="true"/>)
 
Upvote 0

incendio

Well-Known Member
Licensed User
Longtime User
There was no errors.

Will try to use new Dbutils, I read that in new version, insert operasion return true if successful.
 
Upvote 0
Top