Android Question SQLite Cannot Save Changes

BitsAndBytes

Active Member
Licensed User
I am adding a simple row on my database by pressing a button but when i close my app the database reverts back to its original state without had save the last row

B4X:
Sub Activity_Create(FirstTime As Boolean)
Activity.LoadLayout("main")
If FirstTime = True Then     
     If File.Exists(File.DirInternal, "db.db") = False Then
        File.Copy(File.DirAssets, "db.db",File.DirInternal,"db.db")
        sql.Initialize(File.DirInternal, "db.db",  True)
     Else
        sql.Initialize(File.DirInternal, "db.db",  True)
     End If
End If
End Sub


Sub buttonSaveContact_Click
Dim m As Map
Dim list As List
    m.Initialize
    list.initialise
    m.Put("DESCRIPTION", "description1")
    m.Put("LOCATION", "location1")
    m.Put("NOTE", "notes1")
    list.Add(M)
    DBUtils.InsertMaps(sql, "TABLE_CONTACTS", list)
End sub

I am testing the application it on a Android 6.0.1 Xiaomi Prime 3s Mobile Phone.
This is wierd because my database works fine. :(
 
Last edited:

Computersmith64

Well-Known Member
Licensed User
Longtime User
Also - I notice you are doing a

B4X:
sql.BeginTransaction

But there is no corresponding TransactionSuccessful or EndTransaction. You don't need to run a transaction on a single SELECT query anyway & I don't even know if it would work - but if it does, then your SELECT will fail because you haven't got a TransactionSuccessful statement.

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
The issue may not be with your INSERT. To make sure, just put a breakpoint at the:

B4X:
If Cursor.RowCount > 0 Then

line (which isn't required btw, because your For statement takes care of cases where the cursor is empty) & see what the Cursor.RowCount is. If it's > 0, then there's s problem in your For loop somewhere, but if it's 0 then your INSERT is failing.

- Colin.
 
Upvote 0

Computersmith64

Well-Known Member
Licensed User
Longtime User
FWIW - this is how I would write the load function:

B4X:
Sub loadContactsList()
    Private i As Int
    Private cur As Cursor

   contactsList.Initialize 'I'm assuming this is declared somewhere else
   cur = sqlDB.ExecQuery("SELECT * FROM TABLE_CONTACTS") 'To be safe I wouldn't call it "sql"
   For i = 0 To cur.RowCount - 1
       Dim tempContact As ContactObject
       cur.Position = i
       tempContact.contact_key = cur.GetInt("CONTACT_KEY")
       tempContact.contact_description = cur.GetString("DESCRIPTION")
       tempContact.contact_location = cur.GetString("LOCATION")
       tempContact.contact_note = cur.GetString("NOTE")
       contactsList.Add(tempContact)
   Next
   'I wouldn't normally bother calling cur.Close, because the object is declared locally & will be destroyed when the sub exits anyway.
End Sub


- Colin.
 
Last edited:
Upvote 0

BitsAndBytes

Active Member
Licensed User
FWIW - this is how I would write the load function:

B4X:
Sub loadContactsList()
    Private i As Int
    Private cur As Cursor

   contactsList.Initialize 'I'm assuming this is declared somewhere else
   cur = sqlDB.ExecQuery("SELECT * FROM TABLE_CONTACTS") 'To be safe I wouldn't call it "sql"
   For i = 0 To cur.RowCount - 1
       Dim tempContact As ContactObject
       cur.Position = i
       tempContact.contact_key = cur.GetInt("CONTACT_KEY")
       tempContact.contact_description = cur.GetString("DESCRIPTION")
       tempContact.contact_location = cur.GetString("LOCATION")
       tempContact.contact_note = cur.GetString("NOTE")
       contactsList.Add(tempContact)
   Next
   'I wouldn't normally bother calling cur.Close, because the object is declared locally & will be destroyed when the sub exits anyway.
End Sub


- Colin.
This fixed my problem. Awesome Forum and helpfull members. Thank you @Computersmith64 :) :) :)
 
Upvote 0

mc73

Well-Known Member
Licensed User
Longtime User
Did you try removing any unnecessary .beginTransaction like the one posted above?
 
Upvote 0
Top