Android Question After switching to new Mobile, the SQLITE database cannot be opened with B4A-App

johnerikson

Active Member
Licensed User
Longtime User
Has anyone had the same problem after Mobile Switching?
After switching from a Samsung s8 to Onplus 8 Pro,
the B4A cannot open the SQLITE database. All data is structured in the same way.
With SQLITE Manager, I can open it and make sure that alt is OK.
With SQLITE Editor, I can't even see the database in the folder.

B4A-error as below:
android.database.sqlite.SQLiteCantOpenDatabaseException: Cannot open database '/storage/emulated/0/Camper/Camper_geo.db': File /storage/emulated/0/Camper/Camper_geo.db is not readable
Caused by: android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14 SQLITE_CANTOPEN): Could not open database
 

DonManfred

Expert
Licensed User
Longtime User
Where is the code you are using?
The path does not look like a path you are allowed to access. At least on higher Android.
 
Upvote 0

johnerikson

Active Member
Licensed User
Longtime User
Thanks for quick feedback!
This is the code I use to open the database for 5 years ago!

B4X:
Sub initDirAndDB(sFunc As String) As Boolean
    Dim bOk As Boolean
    Log(File.DirDefaultExternal)
    
    If sFunc = "DIRDB" Or sFunc = "DIR" Then
        Try
            If File.Exists(File.DirRootExternal & "/Camper", "") = False Then
                    File.MakeDir(File.DirRootExternal,  "Camper")
            End If
            If File.Exists(File.DirRootExternal & "/Camper/LAGER/", "") = False Then
                File.MakeDir(File.DirRootExternal, "/Camper/LAGER")
            End If
            If File.Exists(File.DirRootExternal & "/Camper/LAGER/SENT", "") = False Then
                File.MakeDir(File.DirRootExternal,  "/Camper/LAGER/SENT")
            End If
            If File.Exists(File.DirRootExternal & "/Camper/LAGER/DELETE", "") = False Then
                File.MakeDir(File.DirRootExternal,  "/Camper/LAGER/DELETE")
            End If
            If File.Exists(File.DirRootExternal & "/Camper/LAGER/DCIM", "") = False Then
                File.MakeDir(File.DirRootExternal,  "/Camper/LAGER/DCIM")
            End If
            If File.Exists(File.DirRootExternal & "/Camper/LAGER/RESIZED", "") = False Then
                File.MakeDir(File.DirRootExternal,  "/Camper/LAGER/RESIZED")
            End If
        Catch
            ToastMessageShow("DIR INIT FAIL", False)
            bOk = False
        End Try
    End If
    If sFunc = "DIRDB" Or sFunc ="DB" Then
        Try
            SQLAnd.Initialize(File.DirRootExternal & "/Camper/", "Camper_geo.db", True)
            ToastMessageShow("DB INIT SUCCESS", False)
            'SQLAnd.ExecNonQuery("PRAGMA journal_mode = wal")
            bOk = True
        Catch
            Dim sE As String = LastException.Message
            If sE.Contains("code 14") Then 
                ToastMessageShow("DB INIT FAIL", False)
                initDirAndDB("DIR")
                bOk = False
            End If
        End Try
    Else
        bOk = True
    End If
    Return bOk
End Sub
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
This is the code I use to open the database for 5 years ago!
Things have dramatically changed the last 5 years in folder permissions. Based on the folder shown in the error, that folder refers to File.DirRootExternal.. You need runtimepermissios for that folder or if the OS is 10 or 11 there are many threads that address that issue these days. You can start with this link:
https://www.b4x.com/android/forum/t...dk30-and-file-dirrootexternal-problem.129886/
I do not want to overwhelm you with links.
 
Upvote 0

Peter Simpson

Expert
Licensed User
Longtime User
  1. Use runtime permissions
  2. You could store the database in RP.GetSafeDirDefaultExternal
 
Upvote 0

johnerikson

Active Member
Licensed User
Longtime User
Thanks!
'Aj aj' what complicated to create a folder with access rights to save and use a SQLITE database.
The user should not choose the location of the database, it is carried out by the code in the app!

The question is:
How to convert the code I sent to create a folder for the database?
 
Upvote 0

johnerikson

Active Member
Licensed User
Longtime User
Thanks!
Has tried File.DirInternal, does not get stability in the database accesses. Often I get errors:
One table from the database works! In other cases, error is :
android.database.sqlite.SQLiteException: no such table: Defs (code 1 SQLITE_ERROR): , while compiling: SELECT KEY, DATA FROM Defs WHERE KEY = 'edPassord'
Defs exist, controlled.
The database is placed as below:
/storage/emulated/0/Android/data/Camper.ToServer/files
I can't access the database with SQLITE Editor or SQLLITE Manager for controls!
Those apps can't reach that folder, apparently.
Samsung S8 pro+ has Android version 9
Oneplus 8 has Android version 11, which then is the problem that is not so easy to solve!
 
Upvote 0

udg

Expert
Licensed User
Longtime User
Look at this.
You used reserved words in your SELECT statement.
If you want to use a keyword as a name, you need to quote it. There are four ways of quoting keywords in SQLite...
 
Upvote 0

johnerikson

Active Member
Licensed User
Longtime User
Thanks!

You mean that changes with sqllite have occurred recently, because this code has been used for at least 5 years without any problems and currently works in Android 9 versions. I'll have to check the keywords regarding Android 11!
 
Upvote 0

johnerikson

Active Member
Licensed User
Longtime User
I don't think I understand how XUI. DefaultFolder should be used, but File.DirInternal
What does the following line look like if XUI. DefaultFolder is used?

SQLAnd.Initialize (File. DirInternal & "/Camper/", "Camper_geo.db", True)
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
What does the following line look like if XUI. DefaultFolder is used?

SQLAnd.Initialize (File. DirInternal & "/Camper/", "Camper_geo.db", True)
XUI.DefaultFolder = File.Dirinternal in B4A. But it's recommended because it's cross platform
B4X:
SQLAnd.Initialize (XUI.DefaultFolder & "/Camper/", "Camper_geo.db", True)

From de DOC:

9.2.1 Copy a file from the Files folder
In this case we use:
xui.DefaultFolder
.
This folder is:
• B4A
same as File.DirInternal
• B4i
same as File.DirDocuments
•B4J
same as File.DirData. You must first call SetDataFolder once before you can use this
folder. In our case:
xui.SetDataFolder("B4XPagesSQLiteLight2")
 
Upvote 0

Erel

B4X founder
Staff member
Licensed User
Longtime User
SQLAnd.Initialize (XUI.DefaultFolder & "/Camper/", "Camper_geo.db", True)
Better like this:
B4X:
SQLAnd.Initialize (XUI.DefaultFolder, "Camper/Camper_geo.db", True)
'or
Dim folder As String = File.Combine(XUI.DefaultFolder, "Camper")
SQLAnd.Initialize(Folder, "Camper_geo.db", True)
 
Upvote 0

johnerikson

Active Member
Licensed User
Longtime User
Thanks a lot!
I will continue to work on this project later!
What reference lib. is required for XUI.DefaultFolder?
(I use B4A 8.50 and XUI-lib. No responce on XUI.DefaultFolder)
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
Uptade to B4A 10.7¡¡¡¡
 
Upvote 0

johnerikson

Active Member
Licensed User
Longtime User
Thanks!
Not so easy, have tried, but not been able to compile the project in B4A 10.70!

Below you see the manifest text that works with version B4A 8.50

AddManifestText(
<uses-sdk android:minSdkVersion="14" android:targetSdkVersion="30"/>
<supports-screens android:largeScreens="true"
android:normalScreens="true"
android:smallScreens="true"
android:anyDensity="true"/>)
SetApplicationAttribute(android:icon, "@drawable/icon")
SetApplicationAttribute(android:label, "$LABEL$")

SetApplicationAttribute(android:theme, "@style/MyAppTheme")
CreateResource(values, theme.xml,
<resources>
<style name="MyAppTheme" parent="Theme.AppCompat.Light.NoActionBar">
<item name="colorPrimary">#0098FF</item>
<item name="colorPrimaryDark">#007CF5</item>
<item name="colorAccent">#AAAA00</item>
<item name="windowNoTitle">true</item>
<item name="windowActionBar">false</item>
</style>
</resources>
)
'For open a new activity in another app:
AddManifestText(
<activity android:name="Camper.VideoTask.Camper.VideoTask.main">
<intent-filter>
<action android:name="android.intent.action.MAIN" />
<category android:name="android.intent.category.LAUNCHER" />
</intent-filter>
</activity>
)


Results in the following error after start compiling in B4A10.70:


B4A Version: 10.70
Parsing code. (0.05s)
Java Version: 8
Building folders structure. (0.01s)
Compiling code. (0.08s)
Compiling layouts code. (0.00s)
Organizing libraries. (0.00s)
(AndroidX SDK)
Compiling resources (0.05s)
Linking resources Error
AndroidManifest.xml:14: error: unexpected element <activity> found in <manifest>.

Have no idea what's wrong with the Manifest text! Therefore, I went back to B4A 8.50 as it can be compiled there without any problems.
After which I encountered the folder problem with Anroid version 11.
What could be wrong with the Manifest text?

When that problem is resolved, I can continue in ver. 10.70
 
Upvote 0

josejad

Expert
Licensed User
Longtime User
This is a new issue so you should open a new thread and it will be solve.

Anyway, probably related with THIS
 
Upvote 0
Top