Android Question B4XPages scope of variables

Phayao

Active Member
Licensed User
Longtime User
Hello,

I am playing with the new concept of pages and got stuck using sql databases.
1) declaring a sql db and initializing in the starter or main page gives me the error that the db cannot be opened (despite using android.permission.WRITE_EXTERNAL_STORAGE)
2) initializing the db in the B4Xresults gives the error java.lang.reflect.InvocationTargetException at java.lang.reflect.Method.invoke(Native Method)

My question: where should i define and initialize a sql database that can be accessible by all pages ?

Thanks a lot in advance !

Chris
 

Phayao

Active Member
Licensed User
Longtime User
Thank you, but where to initialize ? Also in the main module, and where there ?
Before or after pm.initialize ?

Thank you
 
Upvote 0

mangojack

Expert
Licensed User
Longtime User
declaring a sql db and initializing in the starter or main page gives me the error that the db cannot be opened ....

... where should i define and initialize a sql database that can be accessible by all pages ?

In the main module, even without B4XPages the sql object should be placed there.


I have only had a quick play with B4XPages ... but previously in B4A , it was recommended to declare and initialize SQL (and other objects) in the starter service.

I can understand the thinking declaring SQL object in B4XMainPage etc, in a Cross Platform project (and maybe doing away with the starter service to reduce some code.)

But in a solely B4A app (with B4XPages) , would it not still be the recommended way to go ? (using Starter Service for declaring public objects etc.)


I don't know why @Phayao encountered the errors but surely if the Object is Declared and Called correctly all should be good .... No ?
 
Upvote 0

Phayao

Active Member
Licensed User
Longtime User
Thanks all for helping out !

In this special case the problem was that the runtime permission was missing prior to initialze SQL:

B4X:
Sub Activity_Create(FirstTime As Boolean)

    Dim rp As RuntimePermissions
    
    ' check write permissions BEFORE sql.ini
    rp.CheckAndRequest(rp.PERMISSION_WRITE_EXTERNAL_STORAGE)
    wait for Activity_PermissionResult(Permision As String, Result As Boolean)
    
    SQL1.Initialize(DBDir,DBFile,False)
    Dim pm As B4XPagesManager
    pm.Initialize(Activity)
Therefore the initialization of the sql did not work.
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
Don't write anything in the Main module. You don't need to.

In the main module, even without B4XPages the sql object should be placed there.
Wrong answer.

A good place for it is B4XMainPage. You should handle the runtime permission in B4XMainPage. Just change Activity with B4XPage.

BTW, why aren't you storing the database in File.DirInternal or RP.GetSafeDirDefaultExternal?
 
Upvote 0

Phayao

Active Member
Licensed User
Longtime User
Thanks a lot !

I store the database external, so it can be backuped manually - but maybe your solution is better for safety reasons - thanks again !

Chris
 
Upvote 0
Top