Android Question SQLite Create Database

Declan

Well-Known Member
Licensed User
Longtime User
I am attempting to create a SQLite database.
My Code in Service_Create:
B4X:
    ' Make Database directory
    Dim TargetFolder As String
    If File.ExternalWritable Then TargetFolder = File.DirRootExternal Else TargetFolder = File.DirInternal
    File.MakeDir(TargetFolder, "/products/data/")
   
    ' Setup SQL
    SQL0.Initialize(File.DirRootExternal, "/products/data/products.db", True) <--- ERROR HERE

However, I receive the following error:
B4X:
android.database.sqlite.SQLiteCantOpenDatabaseException: unknown error (code 14): Could not open database
 

Declan

Well-Known Member
Licensed User
Longtime User
I have tied "SQL0.Initialize(TargetFolder, "/products/data/products.db", True)"
Same error.
I am running Android 6.0
 
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
try :
B4X:
SQL0.Initialize(File.DirRootExternal & "/products/data/", "products.db", True)
or better with your code

B4X:
SQL0.Initialize(TargetFolder & "/products/data/", "products.db", True)
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
I ha
try :
B4X:
SQL0.Initialize(File.DirRootExternal & "/products/data/", "products.db", True)
or better with your code

B4X:
SQL0.Initialize(TargetFolder & "/products/data/", "products.db", True)
I have tried that too, same Error.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
This code will do it for you:
B4X:
Sub Service_Create
    Dim TargetFolder As String
    If File.ExternalWritable Then TargetFolder = File.DirRootExternal Else TargetFolder = File.DirInternal
    File.MakeDir(TargetFolder, "products/data")
    SQL0.Initialize(TargetFolder & "/products/data", "products.db", True)
End Sub
 
Last edited:
Upvote 0

klaus

Expert
Licensed User
Longtime User
I tested your code from post#1 on my Samsung Galaxy S6 Android 6.0.1.
Attached my test project.

By the way, the code in your first post is incoherent.
You define a TargetFolder, but you dont use it.
With your code, if File.ExternalWritable = False the code won't work.
 
Upvote 0

Declan

Well-Known Member
Licensed User
Longtime User
This code will do it for you:
B4X:
Sub Service_Create
    Dim TargetFolder As String
    If File.ExternalWritable Then TargetFolder = File.DirRootExternal Else TargetFolder = File.DirInternal
    File.MakeDir(TargetFolder, "products/data")
    SQL0.Initialize(TargetFolder & "/products/data", "products.db", True)
End Sub
I still receive the same Error?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I tested it before posting and it worked just fine. Here is attached the full zipped project
 

Attachments

  • SQliteCreateDatabaseInitialize040117.zip
    7 KB · Views: 424
Upvote 0

imbault

Well-Known Member
Licensed User
Longtime User
Just an idea, can you make a try adding that in your manifest:

B4X:
AddManifestText(<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>)
 
Upvote 0
Top