Android Question sqlite on tablet sd

fernando gibert

Member
Licensed User
Pls!

Is there a way to copy file from /storage/emulated/0/mydb.db to file.dirinternal i.e.

make the sd card act as dirassets?

*** if you update the db with simplesqliteviewer for example changed data is not achievable by my b4a apk
that uses the assets copying to dirinternal .

*** simplesqliteviewer opens the db on /storage/emulated/0/mydb.db

thanks alot
 

fernando gibert

Member
Licensed User
thanks Erel!

my problem is to use the same database that Works with simplesqliteviewer (a downloaded apk ) that lets me change , update, add récords.

and open it later with b4a generated apk.

the added or modified récords ( with the dwnldd apk) do not appear in my Project db.

--------

i don't understand why i can't see the same db information .
 
Upvote 0

AnandGupta

Well-Known Member
Licensed User
Hi fernando gibert,

I understand what you are trying to achieve, as I wanted the same in my attendance app.

I use codes from the forum search, as below,

In "Starter.bas"
B4X:
Sub Process_Globals
    'These global variables will be declared once when the application starts.
    'These variables can be accessed from all modules.
    Public rp As RuntimePermissions
    Public SourceFolder As String
  
End Sub

Sub Service_Create
    'This is the program entry point.
    'This is a good place to load resources that are not specific to a single activity.
    SourceFolder = rp.GetSafeDirDefaultExternal("")

End Sub


In my app.b4a
B4X:
Sub Activity_Create(FirstTime As Boolean)
    If FirstTime Then

'        If File.Exists(File.DirInternal, "AttendG3.db") = True Then
'            File.Delete(File.DirInternal, "AttendG3.db") ' only for testing, removes the database
'        End If

        'check if the database already exists
        If File.Exists(File.DirInternal, "AttendG3.db") = False Then
            File.Copy(File.DirAssets, "AttendG3.db", File.DirInternal, "AttendG3.db")
        End If
        If File.Exists(Starter.SourceFolder, "AttendG3.db") = False Then
            File.Copy(File.DirInternal, "AttendG3.db", Starter.SourceFolder, "AttendG3.db")
        End If
        SQL_Data.Initialize(Starter.SourceFolder, "AttendG3.db", True)
    End If

    'Do not forget to load the layout file created with the visual designer. For example:

End Sub


And I use the Starter.SourceFolder+"AttendG3.db" to process in my app and also to open/edit etc. from sqlliteviewer app.
Both see and modify the same db.

There may be better way.

I have given the part of the code which is relevant.

Regards,

Anand
 
Upvote 0

Mahares

Expert
Licensed User
I have given the part of the code which is relevant.
Anand:
Why are you copying the db to Internal and then again to sourcefolder. All you need to do is copy it from assets to sourcefolder.
B4X:
If File.Exists(Starter.SourceFolder, "AttendG3.db") = False Then
            File.Copy(File.DirAssets, "AttendG3.db", Starter.SourceFolder, "AttendG3.db")
        End If
 
Upvote 0

AnandGupta

Well-Known Member
Licensed User
"There may be better way" :)

This is the first app I made, which is used by our company. There are many hit and trial codes with remark lines. The above is same. I just left it after all was well. I have not done clean up, as I am still adding more features to it.

Regards,

Anand
 
Upvote 0

Mahares

Expert
Licensed User
My main objective was to make you aware that you are copying the db unnecessarily to 2 places instead of only one and to make sure fernando uses the proper code from the start.
 
Upvote 0

fernando gibert

Member
Licensed User
Thanks both Anand & Mahares !

I've done your suggestions, but with simplesqliteviewer i added 1 record,

with the generated apk this récords is not available .

it seems exist 2 instances of the SQLite db !!!
 
Upvote 0
Top