B4J Question Open SQLCipher DB

Guenter Becker

Active Member
Licensed User
Longtime User
Hello,
I used the example from user mcqueccu taken from the forum.
I used it with the named and installed jdbc drivers.
I check that I'm able to open the Cipher-DB test.db with the given key - works fine with DBBrowser.
Startting the example I got the reply that DB is opened and sql is initialized.
I tested both initializing version of the example.
After getting the error I create a new Test.db with DBBrowser for SQLCipher using legacy 4 and key 1234.
Checking again, same result.
Example:
#Region Project Attributes
    #MainFormWidth: 600
    #MainFormHeight: 600
#End Region


'https://github.com/Willena/sqlite-jdbc-crypt/releases
'https://github.com/Willena/sqlite-jdbc-crypt/releases/tag/3.46.0.0

'Working
#AdditionalJar: sqlite-jdbc-3.30.0
#AdditionalJar: sqlite-jdbc-3.31.1
#AdditionalJar: sqlite-jdbc-3.35.5.1

'Using the latest Version, you need to Add the slf4j API, otherwise you will get Class not found
#AdditionalJar: slf4j-api-2.0.17
#AdditionalJar: slf4j-nop-1.7.36

Sub Process_Globals
    Private fx As JFX
    Private MainForm As Form
    Private xui As XUI
    Private Button1 As B4XView
  
    Dim dbName As String = "test.db"
    Dim password As String = "1234"
  
    Dim sql As SQL
End Sub

Sub AppStart (Form1 As Form, Args() As String)
    MainForm = Form1
    MainForm.RootPane.LoadLayout("Layout1")
    MainForm.Show
  
  
    If File.Exists(File.DirApp,dbName) = False Then
        File.Copy(File.DirAssets,dbName,File.DirApp,dbName)
        Log("file copied")      
    End If
  
    Sleep(0)
  
    If sql.IsInitialized = False Then
        dim d as string = file.combine(file.dirApp,dbName)
        '===========Database and App in one location======================
        sql.Initialize("org.sqlite.JDBC",$"jdbc:sqlite:file:${d}?cipher=sqlcipher&legacy=4&kdf_iter=256000&key=${Password}"$)
      
        '===========You can also specify Path to the Database============================
        'sql.Initialize("org.sqlite.JDBC",$"jdbc:sqlite:file:${d}?cipher=sqlcipher&legacy=4&key=${Password}&kdf_iter=256000"$)
        Log("DB Started")
    End If
End Sub

Sub Button1_Click
    If sql.IsInitialized Then
    Dim ver As String = sql.ExecQuerySingleResult("SELECT ID from Test")
  
    Log(ver)
  
    End If
End Sub
Starting the Selection I get this message:

Error:
Waiting for debugger to connect...
Program started.
DB Started
Error occurred on line: 57 (Main)
org.sqlite.SQLiteException: [SQLITE_NOTADB]  File opened that is not a database file (file is not a database)
    at org.sqlite.core.DB.newSQLException(DB.java:940)
    at org.sqlite.core.DB.newSQLException(DB.java:953)
    at org.sqlite.core.DB.throwex(DB.java:918)
    at org.sqlite.core.NativeDB.prepare_utf8(Native Method)
    at org.sqlite.core.NativeDB.prepare(NativeDB.java:134)
    at org.sqlite.core.DB.prepare(DB.java:257)
    at org.sqlite.core.CorePreparedStatement.<init>(CorePreparedStatement.java:47)
    at org.sqlite.jdbc3.JDBC3PreparedStatement.<init>(JDBC3PreparedStatement.java:30)
    at org.sqlite.jdbc4.JDBC4PreparedStatement.<init>(JDBC4PreparedStatement.java:19)
    at org.sqlite.jdbc4.JDBC4Connection.prepareStatement(JDBC4Connection.java:35)
    at org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:241)
    at org.sqlite.jdbc3.JDBC3Connection.prepareStatement(JDBC3Connection.java:205)
    at anywheresoftware.b4j.objects.SQL.ExecQuery2(SQL.java:365)
    at anywheresoftware.b4j.objects.SQL.ExecQuerySingleResult2(SQL.java:420)
    at anywheresoftware.b4j.objects.SQL.ExecQuerySingleResult(SQL.java:409)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at anywheresoftware.b4a.shell.Shell.runMethod(Shell.java:629)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:234)
    at anywheresoftware.b4a.shell.Shell.raiseEvent(Shell.java:167)
    at java.base/jdk.internal.reflect.DirectMethodHandleAccessor.invoke(DirectMethodHandleAccessor.java:104)
    at java.base/java.lang.reflect.Method.invoke(Method.java:578)
    at anywheresoftware.b4a.BA.raiseEvent2(BA.java:111)
    at anywheresoftware.b4a.shell.ShellBA.raiseEvent2(ShellBA.java:100)
    at anywheresoftware.b4a.BA$1.run(BA.java:236)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$10(PlatformImpl.java:457)
    at java.base/java.security.AccessController.doPrivileged(AccessController.java:399)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runLater$11(PlatformImpl.java:456)
    at javafx.graphics/com.sun.glass.ui.InvokeLaterDispatcher$Future.run(InvokeLaterDispatcher.java:96)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication._runLoop(Native Method)
    at javafx.graphics/com.sun.glass.ui.win.WinApplication.lambda$runLoop$3(WinApplication.java:184)
    at java.base/java.lang.Thread.run(Thread.java:1589)

Any Idea what is going wrong?
 

teddybear

Well-Known Member
Licensed User
The example works, where did you download the jdbc drivers? you shuold download them from here
 
Last edited:
Upvote 0

Guenter Becker

Active Member
Licensed User
Longtime User
Hey, I used your given Link and took the download from GitHub.
 
Upvote 0
Top