B4J Question [B4XPages] How to create a sqlite for all platform

yfleury

Active Member
Licensed User
Longtime User
I begin in B4XPages. I start B4XPages project with B4J.

I create a database (sqlite) in (B4J)
When I go to B4A, I want to use same database without create it again. I don't need to share data between active app. Only database when I create app.

For this in B4J I create db like this
B4X:
Sub Class_Globals
    Private Root As B4XView
    Private xui As XUI
    Dim DBsql As SQL
End Sub

Sub CreateDB
    DBsql.InitializeSQLite(File.DirAssets, "tradebook.db", True)
    DBsql.ExecNonQuery("CREATE TABLE IF NOT EXISTS demo (id INTEGER,nom TEXT)")
    DBsql.ExecNonQuery("insert into demo (id, nom) values (3, 'test moi')")
End Sub

This code work in B4J but in B4A, I need to create it Starter.
Is it a better way to do with workless?
 
Solution
Unfortunately, you don't give enough information.
Where do you call Sub CreateDB from ?
If you call it in the B4XMainPage module in the B4XPage_Created routine, no problem it will also work in B4A, no need to do this in the Starter module.
But the initialization in B4J and B4A or B4i is not the same, you need this.

B4X:
#If B4J
    SQL1.InitializeSQLite(xui.DefaultFolder, "persons.db", True)
#Else
    SQL1.Initialize(xui.DefaultFolder, "persons.db", True)
#End If

Did you have a look at the B4X SQLite Database booklet.
There you find the B4XPages_SQLiteLight2 example.
It is not exactly what you want to do but the principle is the same.
In this example the default database is loaded from File.DirAssets instead...

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
You don't have to create the database, add the .db to your assets folder, at the start of your app copy the .db from file.dirassets to file.dirinternal.

Then initialize it from file.dirinternal this way you will be using an already existing db.
 
Upvote 0

yfleury

Active Member
Licensed User
Longtime User
I create db from zero in B4J (B4XPages project) When I go in B4A I have a lot of work to do.
import db file
go to Starter to add db file in create service sub

I want to know if another way to simplify the work like automaticly
 
Upvote 0

EnriqueGonzalez

Well-Known Member
Licensed User
Longtime User
Technically yes, if both apps somehow can communicate to each other then. You could create it in b4j and the b4a app would request it.
Its just an idea, but to be honest it seems like a lot of work, only if your process really requires it you should do it.

On the other hand another method could be to create a class that both projects share. Then when you work on b4j in auto... it would be in b4a, sharing classes is easy between platform and projects.
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Unfortunately, you don't give enough information.
Where do you call Sub CreateDB from ?
If you call it in the B4XMainPage module in the B4XPage_Created routine, no problem it will also work in B4A, no need to do this in the Starter module.
But the initialization in B4J and B4A or B4i is not the same, you need this.

B4X:
#If B4J
    SQL1.InitializeSQLite(xui.DefaultFolder, "persons.db", True)
#Else
    SQL1.Initialize(xui.DefaultFolder, "persons.db", True)
#End If

Did you have a look at the B4X SQLite Database booklet.
There you find the B4XPages_SQLiteLight2 example.
It is not exactly what you want to do but the principle is the same.
In this example the default database is loaded from File.DirAssets instead being created in the code.
So instead of loading the database you can create it, same principle.
 
Upvote 1
Solution
Cookies are required to use this site. You must accept them to continue using the site. Learn more…