Android Question SQLite don't keep changes

Ruben García

New Member
Licensed User
Longtime User
Hello,

I have a problem in SQLite.

First open database :

If File.Exists(File.DirDefaultExternal , DBName) = False Then
If FirstTime Then
File.Copy(File.DirAssets, DBName, File.DirDefaultExternal, DBName)
sql1.Initialize(File.DirDefaultExternal , DBName, True)
End If
Else
If FirstTime Then
sql1.Initialize(File.DirDefaultExternal , DBName, True)
End If
End If


Then I update a table, close activity, open activity and changes are OK.

But when I close application and open again I have lost the changes.

¿can anybody help me?

Thank you
 

Mahares

Expert
Licensed User
Longtime User
You code should be like this:
B4X:
If FirstTime Then

    if File.Exists(File.DirDefaultExternal , DBName) = False Then
      File.Copy(File.DirAssets,DBName, File.DirDefaultExternal,DBName)
    end if
    sql1.Initialize(File.DirDefaultExternal ,DBName, True)
end if
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Whilst the code Mahares has posted is a lot neater and easier to read it should produce the same result.

Are you sure the DirDefaultExternal is a useable and maintainable filespace? What device have you got?

I usually use DirInternal instead as it's a more useable filespace in most devices.

ETA: I would also confirm, whilst you're in your App that the Update has worked.

i.e.

Open App

Update / Add record

Check record has been updated

Close App

Reopen and View.

If you step through the processing in your App I'm sure the issue will become apparent. The Android App lifecycle can be a little misleading sometimes and it causes you to make assumptions about what code is being executed. Sometimes it can surprise you (I've been surprised more than once by B4A code!!)
 
Last edited:
Upvote 0

Ruben García

New Member
Licensed User
Longtime User
Thanks for the replies.

The problem persists. I step through the processing , and check record has been updated. The problem is when I close the application and reopen, the changes are lost, as if I had not recorded.

The android version is 4.2.2.

I have seen the sql file does not change the modification date. But there is another file in the same directory (bd.sqlite-journal) that changed the modification date.

any idea to investigate?
 
Upvote 0

Ruben García

New Member
Licensed User
Longtime User
I'm sure Klaus. Also I check the program jump this conditional instruction.

If File.Exists(TargetDir, FileName) = False Then
File.Copy(File.DirAssets, FileName, TargetDir, FileName)
End If
 
Upvote 0

Ruben García

New Member
Licensed User
Longtime User
Resolved!!!!

I began a SQL transaction that never completed. I have completed the transaction and works fine.

Thank you all for your help.
 
Upvote 0
Top