Android Question Regenerate/copy DB file

daniedb

Active Member
Licensed User
Longtime User
HI Guys

I have this code in activity_create
B4X:
        If File.Exists(File.DirInternal,"tx.sql") = False Then
           File.Copy(File.DirAssets,"tx.sql",File.DirInternal,"tx.sql")
         End If

this will copy the file if not exists and allow the user to update records
I've changed the tx.db and add a new table.

Now the program won't copy it again, because it already exists.

So my work around were this:
B4X:
    If File.Exists(File.DirRootExternal,"dbflag.txt")=True Then
         If File.Exists(File.DirInternal,"tx.sql") = False Then
           File.Copy(File.DirAssets,"tx.sql",File.DirInternal,"tx.sql")
         End If
       Else
         File.WriteString(File.DirRootExternal, "dbflag.txt","")
         File.Copy(File.DirAssets,"tx.sql",File.DirInternal,"tx.sql")
        End If
    End If
Which means a new DB is copy, AND all the users data has been erased with a new db

What better way is there to use new tables added to DB, and create that tables without erase previous data

Thanks, appreciate
Danie
 
Last edited:

LucaMs

Expert
Licensed User
Longtime User
You could also use this code to check if a table exists:

B4X:
Public Sub TableExists(DB As SQL, TableName As String) As Boolean
    Private Query As String
    Private Cur As Cursor
  
    Query = "SELECT Name FROM sqlite_master WHERE type = 'table' AND Name =?"
  
    Cur = DB.ExecQuery2(Query, Array As String(TableName))

    Return (Cur.RowCount >= 1)
End Sub
 
Upvote 0
Top