Android Question B4XTable- Problem after editing data in SQLite database

anglia

Member
Licensed User
Longtime User
I'm very impressed with B4XTable and want to use it in my app.

I copy a SQLite database into File.DirInternal and display some of the fields in a B4XTable okay.

A problem arises when I start a different 'View Record' activity to view all of the fields for the record selected from the table.

This 'View Record' activity has an 'Edit Record' button. When pressed this starts the Edit activity which allows the user to edit the data.
After editing, the 'View Record' activity is opened again, but no changes are shown. When I return to the B4XTable the changes are shown.

Also, I need to be able to do a backup copy of the database for copying to another device.

Could someone please offer some advice on where I'm going wrong and how to backup the edited database.

Thanks
 

Brian Dean

Well-Known Member
Licensed User
Longtime User
After editing the table entries you still have to save them to the database file. You have to read the values from the edit views and write them to the database. The B4XTable view does save and reload its data, but only (as far as I know) to its own CSV file, not to any other source. Then you will have to read the updated values from the database into the views in the previous activity.
 
Last edited:
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Upvote 0

anglia

Member
Licensed User
Longtime User
Thanks Brian/Erel

I will have a look at Erel's suggestion.

Can anyone suggest a method for backing up the database file from File.DirInternal to allow the user access for copying etc?

Thanks again
 
Upvote 0

anglia

Member
Licensed User
Longtime User
Sounds as if it might be better to export to a CSV file backup file and have an 'Import CSV' feature in the app to restore the data.

I'm still not clear about how a file in the DirInternal folder can be copied to a folder that is accessible to the user.
Any examples?
 
Upvote 0

Brian Dean

Well-Known Member
Licensed User
Longtime User
Any examples?
Here is an example that might help. There are two or three steps ...

1. Getting the external file write permission. This is well covered here.
2. Writing the file. That is pretty straightforward; Here is some example code ...
B4X:
Sub Globals
  Dim permitted as Boolean
End Sub
. . .
. . .

' Prepare to backup file
Sub makeBackup
  rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
  Do While Not(permitted)
    Sleep(10)
  Loop
  backup(filename)                  ' Only runs if permission is given
End Sub

' Handle permission request result
Sub Activity_PermissionResult(Permission As String, Result As Boolean)
  permitted = Result
  If (permitted = False) Then ExitApplication              ' Assumes that permission is essential
End Sub

' Backup file to external storage - folder name is "backup"
Private Sub Backup(filename As String)
  ' If a previous backup exists then remove it
  If File.Exists(File.DirRootExternal & "/backup", filename) Then
    File.Delete(File.DirRootExternal & "/backup", filename)
  Else
  ' If a previous backup does not exist then create a backup folder
    File.MakeDir(File.DirRootExternal, "/backup")
  End If
  ' Copy the current internal file to the external backup
  File.Copy(File.DirInternal, filename, File.DirRootExternal & "/backup", filename)
End Sub
In this example writing an external file was essential, so the app terminated if permission was not given. That might not apply in your case. Also the destination folder in external storage was predetermined. Probably you would like the User to be able to choose a folder, in which case you need to add a folder dialogue. You can find one in the forum - https://www.b4x.com/android/help/dialogs.html.

Sorry that I made an incorrect post earlier. Hope this serves as compensation.
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…