Android Question Database update for app in the play store

mscientist33

Active Member
Licensed User
I have an app that has the following code in the "Starter"

DB copy code:
If File.Exists(File.DirInternal, Filename) = False Then
        File.Copy(File.DirAssets, Filename, File.DirInternal, Filename)
        'Log("Copied")
    End If

It checks if the database exists and if not copies it.

Lets say the app then gets put on google play store. I then update the database, either add columns or add rows to some tables but not add new tables.

The main question is, how does google play work with "updates"? If I have to add code to the update, what is the best way for my program to check if the database actually needs to be replaced? I'm not sure how to update my database on an upgrade and it not copying the database every time the app runs.
 

Alex_197

Well-Known Member
Licensed User
Longtime User
For adding table and / or a field into existing table you need to create a script inside your app and put the updated app into the play store so the next time these tables / fields will be created in app local database.

For example - a sub that does the update - from the Main activity I call this sub and pass a table name, field name and field type
B4X:
modDB.AddColumn("tblMobileUserAccounts","ProviderTypeID","INTEGER",SQL1)

public Sub AddColumn(TableName As String    ,FieldName As String, Definition As String ,SQL1 As SQLCipher)
    
    Try
        modFun.strName="modDB"
        
        Dim MySQL As String=""
        
        If IsFieldExists2(TableName,FieldName,SQL1)=True Then
            Return
        End If
                
        MySQL="ALTER TABLE " & TableName & " Add " & FieldName & " " & Definition & ";"
        
        SQL1.ExecNonQuery(MySQL)
        
    Catch
        Log("AddColumn " & LastException.Message)
        modFun.ShowError("modDB_AddColumn " & LastException.Message)
    End Try
    
End Sub

To create a table - use this
B4X:
tblProfession="CREATE TABLE  IF NOT EXISTS tblProviderTypes ( "
    tblProfession=tblProfession & "ID INTEGER Not Null PRIMARY KEY AUTOINCREMENT, "
    tblProfession=tblProfession & "ProviderTypeID int,"
    tblProfession=tblProfession & "ProviderType Text,"
    tblProfession=tblProfession & "Description Text"
    tblProfession=tblProfession & ")"
    
    SQL1.ExecNonQuery(tblProfession)

To update records or add records - it's a bit more complicated. You need to add them into the database on tour server and sync this database with with a local database either by JSON or in any other way so you need a server on the Internet. For example you can create a web site either in PHP / ASP.NET and your app will call pages on this website, pass the requestes and get JSON as a response. Then your app will parse this JSON and update a local DB.
 
Last edited:
Upvote 0

Alex_197

Well-Known Member
Licensed User
Longtime User
What is the logic you recommend so it doesn't try to run the script every time you run the app after the update?
In my app I'm running these scripts every time. I check if the table / field exist and every time I'm doing synch with my server to ensure the data integrity.
 
Upvote 0

watesoft

Active Member
Licensed User
Longtime User
I use this way:
B4X:
Sub Activity_Create(FirstTime As Boolean)
    Activity.LoadLayout("Layout")
    'Your App first version, you can create a new txt file named "version.txt" and write a sign(for example,it is a number 1.0)
    If File.Exists(File.DirInternal,"version.txt")=False Then
        File.Copy(File.DirAssets,"version.txt",File.DirInternal,"version.txt")
    End If
    Dim VersionNum As String
    VersionNum=File.ReadString(File.DirInternal,"version.txt")
    
    If FirstTime  And VersionNum<1.5 Then  '1.5 is the sign of new app version
        'del your old database
        If File.Exists(File.DirInternal, "my.db") = True Then
            File.Delete(File.DirInternal, "my.db")
        End If
        'copy new database to File.DirInternal
        File.Copy(File.DirAssets,"new.db",File.DirInternal,"my.db")
        File.WriteString(File.DirInternal,"version.txt",1.5) 'also not need this line
    End If
End Sub
 
Upvote 0

Xfood

Expert
Licensed User
best to use this method of the colleague
@udg
 
Upvote 0

mscientist33

Active Member
Licensed User
That is great @Xfood , I will have to come up with something for the next release since I have no current db version in the current db. Once I have that set I believe what you showed me will work great for me. Thanks.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
When you update the app on the play (version number) obviously all the users will get an updated app from the Play store automatically.

Me, I would run the following query. If the table exists the query will return 1, if it does not exist then the query will return 0. If it's 0 then just run your add table routine, it's as simple as that.

B4X:
SELECT count(*) FROM sqlite_master WHERE type='table' AND name='table_name';

It's not difficult to check for columns either.


Enjoy...
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…