B4J Question SQLite/SQLCipher create Database by code

Guenter Becker

Active Member
Licensed User
Longtime User
Hello
as for B4A the initialize statement includes the possibility to create an empty database if the named database file is not found.
create and open SQLite database:
sql.initializeSQLite(file.dirApp,Filename,true)
I did not find the same in the statement to be used for B4J.
open cipher db:
SQL.Initialize("org.sqlite.JDBC","jdbc:sqlite:file:data.db?cipher=sqlcipher&legacy=1&kdf_iter=4000&key=YOURKEY")

I need to kow if there is a way in B4J to c r e a t e a database file by code as it it is possible in B4A or in another way.
B4J snipped would be nice.
Thankyou
 
Last edited:

Swissmade

Well-Known Member
Licensed User
Longtime User
Hello
as for B4A the initialize statement includes the possibility to create an empty database if the named database file is not found.
create and open SQLite database:
sql.initializeSQLite(file.dirApp,Filename,true)
I did not find the same in the statement to be used for B4J.
open cipher db:
SQL.Initialize("org.sqlite.JDBC","jdbc:sqlite:file:data.db?cipher=sqlcipher&legacy=1&kdf_iter=4000&key=YOURKEY")

I need to kow if there is a way in B4J to create a database file by code as it it is possible in B4A or in another way.
B4J snipped would be nice.
Thankyou

Maybe this can help for B4J

Init SQLLite:
Public Sub InitSQLite(SQLRef As SQL, tmpDBLocation As String, DBName As String, FasterWrites As Boolean) As Boolean 'As ResumableSub
    Try
        Dim DBExists As Boolean = File.Exists(tmpDBLocation, DBName)
        If DBExists Then
            Dim fsize As Long = File.Size(tmpDBLocation, DBName)
            If fsize < 1024 Then Log("⚠️ Warning: DB file is very small, might be corrupted.")
        End If
        SQLRef.InitializeSQLite(tmpDBLocation, DBName, Not(DBExists))  'If true we have the DB so make it false no new table
    
        SQLRef.ExecNonQuery("PRAGMA journal_mode = WAL")       ' Enable Write-Ahead Logging for concurrency
        SQLRef.ExecNonQuery("PRAGMA busy_timeout = 3000")      ' Allow 3s wait if DB is locked
        If FasterWrites = True Then
            SQLRef.ExecNonQuery("PRAGMA synchronous = NORMAL")
            Log("SQLite: Using FAST write mode (synchronous=NORMAL)")
        Else
            SQLRef.ExecNonQuery("PRAGMA synchronous = FULL")
            Log("SQLite: Using SAFE write mode (synchronous=FULL)")
        End If
        Return True
    Catch
        modFunctions.ShowLog("Error: Database is corrupt", True, LastException.Message)
        Return False
    End Try
End Sub
 
Upvote 0

Guenter Becker

Active Member
Licensed User
Longtime User
Thankyou for the quick response.
But it does not meet the question! The question is not to know how to open an existing database. It is about to know how a SQLCipher Database may be created by code.
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
Thankyou for the quick response.
But it does not meet the question! The question is not to know how to open an existing database. It is about to know how a SQLCipher Database may be created by code.
If no db is there it will create a new DB.

Sorry not to send the right answer.
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
Thankyou for the quick response.
But it does not meet the question! The question is not to know how to open an existing database. It is about to know how a SQLCipher Database may be created by code.
Maybe you can ask ChatGPT
 
Upvote 0

Chris2

Active Member
Licensed User
Longtime User
Thankyou for the quick response.
But it does not meet the question! The question is not to know how to open an existing database. It is about to know how a SQLCipher Database may be created by code.

As was shown in the code posted by @Swissmade, B4J SQL (jSQL library) has an InitializeSQLite method with the same parameters as B4A:
1762953187928.png
 
Upvote 0

Guenter Becker

Active Member
Licensed User
Longtime User
Hey
I'm astonished I asked AI of DuckDuckGo and got the answer. Thankyou for the notice.
 
Upvote 0

Guenter Becker

Active Member
Licensed User
Longtime User
Code from DuchDuckGo AI:
' B4J code to create and manage an SQLCipher database

Sub Process_Globals
    ' These modules are available globally.
    Private db As SQL
End Sub

Sub App_Start
    ' Create SQLCipher database
    Dim dbFile As String = File.Combine(File.DirApp, "encrypted_db.db")
    db.Initialize2(dbFile, "your_password", True) ' True indicates to use SQLCipher

    ' Create a table
    Dim sql As String
    sql = "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)"
    db.ExecNonQuery(sql)

    ' Insert sample data
    sql = "INSERT INTO users (name, age) VALUES ('Alice', 30)"
    db.ExecNonQuery(sql)
    sql = "INSERT
 
Upvote 0

Swissmade

Well-Known Member
Licensed User
Longtime User
Code from DuchDuckGo AI:
' B4J code to create and manage an SQLCipher database

Sub Process_Globals
    ' These modules are available globally.
    Private db As SQL
End Sub

Sub App_Start
    ' Create SQLCipher database
    Dim dbFile As String = File.Combine(File.DirApp, "encrypted_db.db")
    db.Initialize2(dbFile, "your_password", True) ' True indicates to use SQLCipher

    ' Create a table
    Dim sql As String
    sql = "CREATE TABLE IF NOT EXISTS users (id INTEGER PRIMARY KEY, name TEXT, age INTEGER)"
    db.ExecNonQuery(sql)

    ' Insert sample data
    sql = "INSERT INTO users (name, age) VALUES ('Alice', 30)"
    db.ExecNonQuery(sql)
    sql = "INSERT
Top thanks
 
Upvote 0

Chris2

Active Member
Licensed User
Longtime User
B4X:
' Create SQLCipher database
 Dim dbFile As String = File.Combine(File.DirApp, "encrypted_db.db")
 db.Initialize2(dbFile, "your_password", True) ' True indicates to use SQLCipher
If this works for you then great, but I'm confused by it.
In B4J sql.Initialize2 takes four parameters, none of which set it to SQLCipher:
1762960153232.png


I see no mention of this creasting the database which is what your original post asked about.

Maybe these can help:
 
Upvote 0
Top