B4J Question How do you use SQLite in B4J pages?

Credo

Member
I come from a web/PHP background and use MySql often. So I want to use SQLite since the syntax is pretty much the same, but the resources I found are either for B4A or is pretty confusing.
Does anyone have a thread or resource I can use as reference? Or can someone point me in the right direction?


These are what I've seen so far.



Thank you in advance!
 

Credo

Member
The same code will work with B4J. You just need to call SQL.InitializeSQLite and add this line to main module:
B4X:
#AdditionalJar: sqlite-jdbc-3.7.2
Hi Erel,

One more thing;
I already made a db file with db browser. How do I add it to code?

sql.InitializeSQLite(File.DirData("sql"),"patient.db",True)
WebView1.LoadHtml(DBUtils.ExecuteHtml(sql,"SELECT * FROM login",Null, -1, False))


This is the code I written based on the video you made. Am I able to use a pre-existing DB file in the application folder?

"...\B4J\Files\patient.db"

Thank you again!
 
Upvote 0

Credo

Member
Hi Erel,

One more thing;
I already made a db file with db browser. How do I add it to code?

sql.InitializeSQLite(File.DirData("sql"),"patient.db",True)
WebView1.LoadHtml(DBUtils.ExecuteHtml(sql,"SELECT * FROM login",Null, -1, False))


This is the code I written based on the video you made. Am I able to use a pre-existing DB file in the application folder?

"...\B4J\Files\patient.db"

Thank you again!
Got it to work. Was on the first chapter of the docs .T_T.

Apologies.

B4X:
    If File.Exists(File.DirData("sql"), "patient.db") = False Then
        'copy the default DB
        File.Copy(File.DirAssets, "patient.db", File.DirData("sql"), "patient.db")
    End If


    sql.InitializeSQLite(File.DirData("sql"), "patient.db", True)
    WebView1.LoadHtml(DBUtils.ExecuteHtml(sql,"SELECT * FROM login",Null, -1, False))
 
Upvote 0

Tim Chapman

Active Member
Licensed User
Longtime User
Hi Credo.
Here is my code for making and putting data into MySQL databases.

Cheers,
Tim
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
I also have a B4X project template using SQLite but it is using MiniORMUtils library.
You can use B4J to create a new project and run it as it is.
 
Upvote 0

aeric

Expert
Licensed User
Longtime User
If you plan to work with client-server environment, I have you covered with the following options:

1. [B4X] User Login Client where the SQLite or MySQL database is hosted on a [B4J] User Login Server
This solution is a bit complex as it involved user login security with access token.

2. [B4J] Web API Server (v3)
It is recommended to start with this server template. The first option above is based on this project template.
Understand how the Web API Server works and use [B4X] Web API Client to connect to the SQLite or MySQL database.

3. [B4J] jRDC2 Server (SQLite, MySQL, MS SQL, Firebird, Postgresql, DBF)
You can also use jRDC2 server. I have also created [B4X] jRDC2 Client (B4J, B4A, B4i) to connect to this server.
 
Upvote 0
Top