Android Question B4XPages using sqlite - standard practice

Mark Stuart

Active Member
Licensed User
Longtime User
Hi all,
Well I'm finally giving B4XPages a go.
Pretty much all my apps are oriented towards data storage and retrieval.

Database Initialization Placement:
I'm used to the Starter Service_Start and it making a call to my DBCalls Code module to Initialize the SQLite database. And I also use the Starter Service_Destroy to close the database.

In B4XPages, what is the, or where is the equivalent places to initialize the database and to close the database?

And are there any other norms where database management is placed?

Just trying to figure all this out as I haven't found any documentation on using databases and B4XPages and normalization.

Regards,
Mark Stuart
 

LucaMs

Expert
Licensed User
Longtime User
You actually need to know the equivalent of Service_Start and Service_Destroy, which you use to open and close the DB, but this isn't strictly DB-related.

Open the DB by calling a method of your DBCalls (modDB) from within B4XPage_Created of the B4XMainPage. The actual entry point is the Initialize Sub of B4XMainPage, so this would be the most appropriate place.

First of all, in my opinion (!) closing a SQLite DB isn't essential, as it doesn't is / have a server; it's important to close always all the ResultSets.

Service_Destroy. The equivalent "should" be the B4XPage_CloseRequest Sub of the B4XMainPage, but that's not certain, as your project might want to close this page without closing the project.

I'm about to write something that could be not correct (confirmation from Erel would be needed) ...
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Service_Destroy. The equivalent "should" be the B4XPage_CloseRequest Sub of the B4XMainPage, but that's not certain, as your project might want to close this page without closing the project.

I might be writing something incorrectly (confirmation from Erel would be needed)
I think you should consider whether the page being closed (B4XPage_CloseRequest) is the last one currently open, which isn't always the B4XMainPage.

In a code module, such as modProjUtils, you could write a Public Sub CheckIsProjectClosing, called from within B4XPage_CloseRequest of each B4XPage.
B4X:
Public Sub CheckIsProjectClosing
    Dim Closing As Boolean
    Closing = B4XPages.GetManager.mStackOfPageIds.Size = 1
    If Closing Then
        ' here you should close the db (call modDB.CloseDB)
    End If
End Sub
 
Last edited:
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User
Where do you do all the SQL.Exec... statements?

Use in modules:
B4XPages.MainPage.db.Exec....

Remember that the db variable must be published on the B4XMainPage. I used the name of my db variable, but you can use SQL1 or any name you like.
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
You actually need to know the equivalent of Service_Start and Service_Destroy, which you use to open and close the DB, but this isn't strictly DB-related.

Open the DB by calling a method of your DBCalls (modDB) from within B4XPage_Created of the B4XMainPage. The actual entry point is the Initialize Sub of B4XMainPage, so this would be the most appropriate place.

First of all, in my opinion (!) closing a SQLite DB isn't essential, as it doesn't is / have a server; it's important to close always all the ResultSets.

Service_Destroy. The equivalent "should" be the B4XPage_CloseRequest Sub of the B4XMainPage, but that's not certain, as your project might want to close this page without closing the project.

I'm about to write something that could be not correct (Erel will need confirmation)...
LucaMS,
a. I put the DBCalls.Initialize in B4XMainPage - Public Sub Initialize (makes sense)
b. I added this to the B4XMainPage and it underlined the first line:

B4X:
Private Sub B4XPage_CloseRequest As ResumableSub
    DBCalls.DBClose
End Sub

Hovering over the first line showed a message:
Not all code paths return a value. Not sure what's up with that. Any idea?

Next: where do I put common files, like a SQLite database file and images, etc.?
I copied it to many folders under the project, and clicked Sync in the IDE. It didn't appear.

All new to me.

Mark Stuart
 
Last edited:
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
Use in modules:
B4XPages.MainPage.db.Exec....

Remember that the db variable must be published on the B4XMainPage. I used the name of my db variable, but you can use SQL1 or any name you like.
Are you sure about that? So B4XPages acts totally different that you can't have a DBCalls module where the SQL As SQL is defined in the Process_Globals?
I put my SQL As SQL in the DBCalls module and that's where all my database execute's are as well.
I haven't run the app with database functions as yet, as I'm still building it, but I will test it.

EDIT: that's weird, the Starter module did not appear as a Tab as all the other modules do, but it appears in the list of modules on the IDE Modules tab.
Why wouldn't the IDE show it in the Tabs on top?
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
a. I put the DBCalls.Initialize
DBCalls is a code module, so I'd name it "Init", to distinguish it from classes.

Hovering over the first line showed a message:
Not all code paths return a value. Not sure what's up with that. Any idea?
B4XPage_CloseRequest is a function, returns a Resumable.
Add:
Return True ' to close the page
or
Return False ' to prevent the page from closing.
 
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Next: where do I put common files, like a SQLite database file and images, etc.?
I copied it to many folders under the project, and clicked Sync in the IDE. It didn't appear.
1757559457665.png
 
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
I think you should consider whether the page being closed (B4XPage_CloseRequest) is the last one currently open, which isn't always the B4XMainPage.

In a code module, such as modProjUtils, you could write a Public Sub CheckIsProjectClosing, called from within B4XPage_CloseRequest of each B4XPage.
Try the project I'm attaching.
Just try the B4J project; for B4A and B4I, you'll need to create layouts and do other.
It's full of unnecessary things (because of my own project template, I've removed a lot of them!)

Its name: bu2 = "buttami 2" = throw me away 2 😁
 

Attachments

  • bu2.zip
    25.7 KB · Views: 53
Last edited:
Upvote 0

LucaMs

Expert
Licensed User
Longtime User
Did that too and it didn't appear.
1. place files (.db too) in the "Shared Files" folder
2. open the B4J or B4A or B4J project
3. Ctrl+Click on the comment line you find in B4XMainPage:
B4X:
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
(bu2 is the project I attached to my previous post)
1757563272415.png


1757563196107.png

B4J_X9YxU0IaOe.gif
 
Upvote 0

Lucas Siqueira

Active Member
Licensed User
Longtime User
Are you sure about that? So B4XPages acts totally different that you can't have a DBCalls module where the SQL As SQL is defined in the Process_Globals?
I put my SQL As SQL in the DBCalls module and that's where all my database execute's are as well.
I haven't run the app with database functions as yet, as I'm still building it, but I will test it.

EDIT: that's weird, the Starter module did not appear as a Tab as all the other modules do, but it appears in the list of modules on the IDE Modules tab.
Why wouldn't the IDE show it in the Tabs on top?


There's no rule; you can use it both ways (using a database variable in B4XMainPage or the DBCalls module, or even having B4XMainPage make the same calls you're using in Starter).

You can go either way: using the code module or using B4XMainPage to declare your database variable.

I choose to use the database variable in the main module of a B4XPAGES project, which is B4XMainPage.

Remember that if you use a code module as suggested by DBCalls, you'll always have to specify the location of your database variable.
 
Upvote 0

Mark Stuart

Active Member
Licensed User
Longtime User
1. place files (.db too) in the "Shared Files" folder
2. open the B4J or B4A or B4J project
3. Ctrl+Click on the comment line you find in B4XMainPage:
B4X:
'Ctrl + click to sync files: ide://run?file=%WINDIR%\System32\Robocopy.exe&args=..\..\Shared+Files&args=..\Files&FilesSync=True
(bu2 is the project I attached to my previous post)
View attachment 166730

View attachment 166729
View attachment 166731
Fixed the problem of the file not appearing in the IDE. I had copied the database file into the Shared Folder of a similarly named folder in another app - duh!
 
Upvote 0
Top