B4J Question Error sql.initialize (JDBC with file path)

Guenter Becker

Active Member
Licensed User
Longtime User
B4J: 10.30
Windows 11 Pro 25H2

Hello,
have some problems initializing sql object.
Database is created with DBBrowser SQLiteCipher Cipher 4 and Password 1234. Working well.

Drivers in Main:
    '# libraries to manage sqlite/sqlcipher database
    #AdditionalJar: sqlite-jdbc-3.30.0
    #AdditionalJar: sqlite-jdbc-3.31.1
    #AdditionalJar: sqlite-jdbc-3.35.5.1
    #AdditionalJar:    sqlite-jdbc-3.46.0.0
    #AdditionalJar: slf4j-api-2.0.17
    #AdditionalJar: slf4j-nop-1.7.36
Used statement sql.initialize:
' DBN = ExampleCIPH.db'
' DBinfo.Password = 1234'
Dim d As String = File.combine(File.DirApp,DBN)
sql.Initialize("org.sqlite.JDBC",$"jdbc:sqlite:file:${d}?cipher=sqlcipher&legacy=4&key=${DBinfo.password}&kdf_iter=256000"$

Trying to initialize the sql I get the error::
Waiting for debugger to connect...
Program started.
G:\Android_b4j\GBEextDBUtils\Objects\ExampleCIPH.db
Error occurred on line: 124 (GBEextDBUtils)
java.sql.SQLException: path to 'file:G:\Android_b4j\GBEextDBUtils\Objects\ExampleCIPH.db?cipher=sqlcipher&legacy=4&key=1234&kdf_iter=256000': 'G:\Android_b4j\GBEextDBUtils\Objects\file:G:' does not exist
    at org.sqlite.Conn.open(Conn.java:103)
    at org.sqlite.Conn.<init>(Conn.java:57)
    at org.sqlite.JDBC.createConnection(JDBC.java:77)
    at org.sqlite.JDBC.connect(JDBC.java:64)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:683)
    at java.sql/java.sql.DriverManager.getConnection(DriverManager.java:230)
    at anywheresoftware.b4j.objects.SQL.Initialize2(SQL.java:58)
    at anywheresoftware.b4j.objects.SQL.Initialize(SQL.java:47)
    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.runVoidMethod(Shell.java:673)
    at anywheresoftware.b4a.shell.Shell.raiseEventImpl(Shell.java:240)
    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.debug.Debug.delegate(Debug.java:64)
    at b4j.example.gbeextdbutils._opendb(gbeextdbutils.java:152)
    at b4j.example.main._appstart(main.java:83)
    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:237)
    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.raiseEvent(BA.java:98)
    at b4j.example.main.start(main.java:38)
    at javafx.graphics/com.sun.javafx.application.LauncherImpl.lambda$launchApplication1$9(LauncherImpl.java:847)
    at javafx.graphics/com.sun.javafx.application.PlatformImpl.lambda$runAndWait$12(PlatformImpl.java:484)
    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)

If I ommit the database path and I use only the database name (d="ExampleCIPH.db") then everthing works well.
This showes that the problem is within the format of the used string of the folder (G:\Android_b4j\GBEextDBUtils\Objects\).

Testing other path strings like:
d = "G:\\Android_b4j\\GBEextDBUtils\\Objects\\ExampleCIPH.db" same error.
d = "\\G:\\Android_b4j\\GBEextDBUtils\\Objects\\ExampleCIPH.db" same error.
d = "G:\Android_b4j\GBEextDBUtils\Objects\ExampleCIPH.db" same error.

I need help to understands what is wrong with the used path format .
 

Guenter Becker

Active Member
Licensed User
Longtime User
HEUREKA it is not possible I found a working solution!!
Sorry placing the post but as a long way of trial and error I found this:
Code raising the error::
' DBN = ExampleCIPH.db'
' DBinfo.Password = 1234'
Dim d As String = File.combine(File.DirApp,DBN)
sql.Initialize("org.sqlite.JDBC",$"jdbc:sqlite:file:${d}?cipher=sqlcipher&legacy=4&key=${DBinfo.password}&kdf_iter=256000"$
Code without error:
Dim d As String = File.Combine(DBinfo.Path,DBN)
Dim sb As StringBuilder: sb.initialize
sb.Append("jdbc:sqlite:file:").append(d).Append("?cipher=sqlcipher&legacy=4&key=")
sb.Append(DBinfo.password).Append("&kdf_iter=256000")

sql.Initialize("org.sqlite.JDBC", sb.tostring)
 
Upvote 0
Cookies are required to use this site. You must accept them to continue using the site. Learn more…