After a SQLite database file has been successfully downloaded, it is to be copied to another directory and opened there.
Although download and transport to the other directory are successful, a crash occurs when the SQLite object is initialized:
For the directories "SharedFolder" from Erel's FileProvider class was used. Since the target file is stored without errors in the designated folder, I exclude a permission problem at the moment.
Can someone help me to get a grip on solving the sql open problem?
A small testproject is attached.
Although download and transport to the other directory are successful, a crash occurs when the SQLite object is initialized:
B4X:
~i:** Activity (main) Resume **
~i:** Activity (main) Pause, UserClosed = false **
~i:*** Service (starter) Create ***
Using FileProvider? true
~i:** Service (starter) Start **
~i:** Activity (main) Create, isFirst = true **
~i:** Activity (main) Resume **
#-
#-
#-Sub btnDownloadDb_Click
#-Sub DownloadFile
#- FileURL : https://drive.google.com/open?id=1qgnalqWv-cchSMCF63R6Uo9QZgJ50p92
#- LocalPath : /data/user/0/b4a.example/files/shared/temp
#- LocalFilename: myfile
~i:*** Service (httputils2service) Create ***
~i:** Service (httputils2service) Start **
#-
#-
#-Sub LogFilesInFolder, strFolder=/data/user/0/b4a.example/files/shared/temp
#- myfile
#-
#-
#-Sub LogFilesInFolder, strFolder=/data/user/0/b4a.example/files/shared/data1
#- datablock1.db
#-
#-
#-Sub ActivateDb, strDbFile=/data/user/0/b4a.example/files/shared/data1/datablock1.db
#- FileExists=true
~e:main$ResumableSub_ActivateDbresume (java line: 411)
~e:android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
~e: at android.database.sqlite.SQLiteConnection.nativeOpen(Native Method)
~e: at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:210)
~e: at android.database.sqlite.SQLiteConnection.open(SQLiteConnection.java:194)
~e: at android.database.sqlite.SQLiteConnectionPool.openConnectionLocked(SQLiteConnectionPool.java:498)
~e: at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:205)
~e: at android.database.sqlite.SQLiteConnectionPool.open(SQLiteConnectionPool.java:197)
~e: at android.database.sqlite.SQLiteDatabase.openInner(SQLiteDatabase.java:933)
~e: at android.database.sqlite.SQLiteDatabase.open(SQLiteDatabase.java:913)
~e: at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:809)
~e: at android.database.sqlite.SQLiteDatabase.openDatabase(SQLiteDatabase.java:758)
~e: at anywheresoftware.b4a.sql.SQL.Initialize(SQL.java:44)
~e: at b4a.example.main$ResumableSub_ActivateDb.resume(main.java:411)
~e: at b4a.example.main._activatedb(main.java:342)
~e: at b4a.example.main$ResumableSub_btnDownloadDb_Click.resume(main.java:534)
~e: at anywheresoftware.b4a.BA.checkAndRunWaitForEvent(BA.java:245)
~e: at anywheresoftware.b4a.BA.raiseEvent2(BA.java:185)
~e: at anywheresoftware.b4a.BA$2.run(BA.java:365)
~e: at android.os.Handler.handleCallback(Handler.java:790)
~e: at android.os.Handler.dispatchMessage(Handler.java:99)
~e: at android.os.Looper.loop(Looper.java:180)
~e: at android.app.ActivityThread.main(ActivityThread.java:6861)
~e: at java.lang.reflect.Method.invoke(Native Method)
~e: at com.android.internal.os.RuntimeInit$MethodAndArgsCaller.run(RuntimeInit.java:438)
~e: at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:837)
B4X:
Sub btnDownloadDb_Click
Log("#-")
Log("#-")
Log("#-Sub btnDownloadDb_Click")
' Cleanup the Testfolders before start
EmptyFolder(Starter.Folder_temp)
EmptyFolder(Starter.Folder_data1)
Dim strUrl As String = "https://drive.google.com/open?id=1qgnalqWv-cchSMCF63R6Uo9QZgJ50p92" ' db, public link
Dim strTempFile As String = "myfile"
Dim strTargetFile As String = "datablock1.db"
' Download the file, check it and transfer to workfolder
wait For (DownloadFile(strUrl, Starter.Folder_temp, strTempFile)) complete(strReturn As String)
LogFilesInFolder(Starter.Folder_temp)
'wait for (<some screening checks of the file are done>) complete(bolRet as Boolean)
wait for (File.CopyAsync(Starter.Folder_temp, strTempFile, Starter.Folder_data1, strTargetFile)) complete(Success As Boolean)
LogFilesInFolder(Starter.Folder_data1)
wait for (ActivateDb(File.Combine(Starter.Folder_data1, strTargetFile))) complete(Success As Boolean)
MsgboxAsync("Download and open result=" & Success, "Result")
End Sub
'
Sub DownloadFile(FileURL As String, LocalPath As String, LocalFilename As String) As ResumableSub
Log("#-Sub DownloadFile")
Log("#- FileURL : " & FileURL)
Log("#- LocalPath : " & LocalPath)
Log("#- LocalFilename: " & LocalFilename)
Dim h_dl As HttpJob
h_dl.Initialize("",Me)
h_dl.Download(FileURL)
Wait For (h_dl) JobDone(h_dl As HttpJob)
If h_dl.Success Then
Dim inStr As InputStream
Dim out As OutputStream
If LocalPath= "" Then LocalPath= Starter.Provider.SharedFolder
inStr = h_dl.GetInputStream
out = File.OpenOutput(LocalPath, LocalFilename, False)
wait for (File.Copy2Async(inStr, out)) complete(Success As Boolean)
out.Close
h_dl.Release
Return "{OK}"
Else
h_dl.Release
Return "{ERR}"
End If
End Sub
'
Sub ActivateDb(strDbFile As String) As ResumableSub
Log("#-")
Log("#-")
Log("#-Sub ActivateDb, strDbFile=" & strDbFile)
Log("#- FileExists=" & File.Exists("", strDbFile) )
If Not(File.Exists("", strDbFile)) Then
Return False
End If
If sqlx.IsInitialized Then sqlx.Close
sqlx.Initialize(Starter.Folder_data1, strDbFile, False)
Dim res As ResultSet = sqlx.ExecQuery("SELECT name FROM sqlite_master WHERE type='table'")
Do While res.NextRow
Log("#- " & res.GetString("name"))
Loop
Log("#-")
Log("#-")
Return True
End Sub
For the directories "SharedFolder" from Erel's FileProvider class was used. Since the target file is stored without errors in the designated folder, I exclude a permission problem at the moment.
Can someone help me to get a grip on solving the sql open problem?
A small testproject is attached.
Attachments
Last edited: