Android Question Database not open

AHilberink

Active Member
Licensed User
Longtime User
Hi,

The first time I open my app, everything works fine. I do have a button to close the App which does an Activity.Finish.

Opening the App direct again it crash with the message "Database not open".

Does anyone has an option?

Best regards,
André
 

AHilberink

Active Member
Licensed User
Longtime User
No, without seeing the code.

It is a large code.

I experience that if I change
B4X:
Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        SQL1.Close        'if the user closes the program we close the database
    End If
End Sub

to

B4X:
Sub Activity_Pause (UserClosed As Boolean)
End Sub

The problem is gone. But is this the correct way? How to close or reopen the database on Activity.Finish?

Best regards,
André
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Check if you open the DB in Activity_Create under the FirstTime boolean evaluation.
If that is your case, bear in mind that FirstTime evaluates True only when the process (not the Activity) is executed for the "first time". This means that if you close an app and soon after that you launch it again, it is highly probable that the process is still in memory and FirstTime will return False.

udg
 
  • Like
Reactions: eps
Upvote 0

AHilberink

Active Member
Licensed User
Longtime User
Check if you open the DB in Activity_Create under the FirstTime boolean evaluation.
If that is your case, bear in mind that FirstTime evaluates True only when the process (not the Activity) is executed for the "first time". This means that if you close an app and soon after that you launch it again, it is highly probable that the process is still in memory and FirstTime will return False.

udg

Hi,

Thanks. I experience this, so I already change the structure. Now it runs every restart.

This is in my Activity Create now:
B4X:
    If FirstTime Then
    End If
  
    If SQL1.IsInitialized = False Then
        File.MakeDir(File.DirRootExternal,"XXXXXXX")
        InitTable
        SQL1.Initialize(FilePath, FileName, True, Wachtwoord, "")
        SQLTableCreate
    End If

I use SQLCypher if this could creates this problem.

Still have the problem :(

Best regards,
André
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
B4X:
If SQL1.IsInitialized = False Then
    If Not(File.Exists(File.DirRootExternal,"XXXXXXX")) Then
        File.MakeDir(File.DirRootExternal,"XXXXXXX")
        InitTable
        SQLTableCreate
    End If
    SQL1.Initialize(FilePath, FileName, False)  
End If

B4X:
Sub Activity_Pause (UserClosed As Boolean)
    If UserClosed Then
        SQL1.Close        'if the user closes the program we close the database
    End If
End Sub
 
Upvote 0

eps

Expert
Licensed User
Longtime User
Personally I wouldn't close the database. I would close the cursors, which I'm sure you do right after executing each SQL statement where a cursor is opened.

ETA : I do something like this

B4X:
                If FirstTime = True Then

                If File.Exists(File.DirInternal,"thedatabase.db") = False Then

                   

                    'ToastMessageShow("Database hasn't been copied to read/write area",True)

                    'Msgbox("Database hasn't been copied to read/write area","Debug")

                   

                    File.Copy(File.DirAssets,"themsc.db",File.DirInternal,"thedatabase.db")


End If

End If

                If SQL1.IsInitialized = False Then

                    SQL1.Initialize(File.DirInternal,"thedatabase.db", True)

                End If

I don't close the DB but as mentioned do close the cursors and end transaction if writing information to the DB. I also allow the PKs of User data to be written to a file and then the database reloaded if the DB is copied over anew. HTH
 
Last edited:
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Personally I wouldn't close the database. I would close the cursors, which I'm sure you do right after executing each SQL statement where a cursor is opened.
1. If you do not close the database, what happens if you start a second application that uses the same database for instance graphing data from a table. If you open second application for the FirstTime, wouldn't that try to initialize the database and may produce an error since the database is already initialized by not being closed in the first application?
2. Suppose we have an open CURSOR1 and immediately create a new CURSOR1 using a different SQL statement. Would you still need to close the first one, or would the memory held by first one be released for the second instance.
In my applications, I have been closing CURSOR1 and SQL1 only whenever I finish with a given application. I have been getting away with it if it is not the proper way. I would like your opinion and others on this important topic.
Thank you
 
Upvote 0

eps

Expert
Licensed User
Longtime User
1. If you do not close the database, what happens if you start a second application that uses the same database for instance graphing data from a table. If you open second application for the FirstTime, wouldn't that try to initialize the database and may produce an error since the database is already initialized by not being closed in the first application?
2. Suppose we have an open CURSOR1 and immediately create a new CURSOR1 using a different SQL statement. Would you still need to close the first one, or would the memory held by first one be released for the second instance.
In my applications, I have been closing CURSOR1 and SQL1 only whenever I finish with a given application. I have been getting away with it if it is not the proper way. I would like your opinion and others on this important topic.
Thank you

The code above shows that it only initialises the DB if it's not already initialised.

Another thought, of course on exit the Activity Persists, should the DB closed in this instance?
 
Upvote 0

mangojack

Well-Known Member
Licensed User
Longtime User
Checking all apps utilizing db's reveals the cursor is created then closed after each query.. and the db finally closed prior to closing the app.
As I learn't SQl from B4a examples etc ,I just thought this is how it should be and have had no problems. Certainly don't have any
argument's / opinions as to the correct way.
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Checking all apps utilizing db's reveals the cursor is created then closed after each query.. and the db finally closed prior to closing the app.
As I learn't SQl from B4a examples etc ,I just thought this is how it should be and have had no problems. Certainly don't have any
argument's / opinions as to the correct way.

This may be an incorrect behavior only in case of concurrency, in case of simultaneous access to a record by multiple users. If the database is local, in the device, this is fine.
 
Upvote 0
Top