Android Question SQLite file locations

SteveAuckland

Member
Licensed User
Longtime User
Hi I have a problem testing an app I'm writing that uses an SQLite database. I want to be able to view what the app is doing to the database, and change values rerun test etc. However I cant find where the database is located. I am not talking about the location on my phone but when I'm using the emulator. I Created the database using a freeware SQLite application and copied it to the Files area for my app. Then the app copies it from DirAssets to DirInternal, never to be seen again. I can tell from my app that the database is being updated, but I can't find it.

I'm obviously doing something daft, so if someone could gently point me in the right direction I would be grateful.

Thanks

Steve
 

Harris

Expert
Licensed User
Longtime User
Hi I have a problem testing an app I'm writing that uses an SQLite database. I want to be able to view what the app is doing to the database, and change values rerun test etc. However I cant find where the database is located. I am not talking about the location on my phone but when I'm using the emulator. I Created the database using a freeware SQLite application and copied it to the Files area for my app. Then the app copies it from DirAssets to DirInternal, never to be seen again. I can tell from my app that the database is being updated, but I can't find it.

I'm obviously doing something daft, so if someone could gently point me in the right direction I would be grateful.

Thanks

Steve


Standard way to load and access a database...
DirDefaultExternal - is the answer to your question - after it is copied there from assets

B4X:
Sub LoadDB

    If File.Exists(File.DirDefaultExternal, "QInspect.db") = False Then
        File.Copy(File.DirAssets, "QInspect.db",File.DirDefaultExternal,"QInspect.db")
        Log(" Copying QInspect.db to SD card")
    End If

    If SQL1.IsInitialized = False Then
        SQL1.Initialize(File.DirDefaultExternal, "QInspect.db", False)
        Log(" ***** --- Initing QInspect.db if not already inited **********")
    End If

End Sub
 
Upvote 0

SteveAuckland

Member
Licensed User
Longtime User
Standard way to load and access a database...
DirDefaultExternal - is the answer to your question - after it is copied there from assets

B4X:
Sub LoadDB

    If File.Exists(File.DirDefaultExternal, "QInspect.db") = False Then
        File.Copy(File.DirAssets, "QInspect.db",File.DirDefaultExternal,"QInspect.db")
        Log(" Copying QInspect.db to SD card")
    End If

    If SQL1.IsInitialized = False Then
        SQL1.Initialize(File.DirDefaultExternal, "QInspect.db", False)
        Log(" ***** --- Initing QInspect.db if not already inited **********")
    End If

End Sub

Thanks for the reply, just need a bit more clarification if I could. To paraphrase the users guide, it says You cant access files in DirAssets so copy it to either DirInternal or DirRootExternal, can you explain the difference and also DirDefaultExternal, and will any of these directories be accessable in emulator mode on my PC?

Thanks for your patience

Steve
 
Upvote 0

SteveAuckland

Member
Licensed User
Longtime User
Thanks everyone for the help, onwards and upwards,

Regards

Steve
Sorry, I still have an issue, I have tried using DirDefaultExternal and also DirRootExternal and get the same compile error, ie
File not found exception mnt/sdcard/....... which implies you must have an sd card installed. However since this is running via the emulator this doesn't seem right!!

So my question is, is it possible to have an SQLite db in a file location where I can access it on my PC with a file explorer?

Thanks

Steve
 
Upvote 0

Eric H

Active Member
Licensed User
Longtime User
File.DirRootExternal is where I am currently storing my SQLite database and I just verified that I can view it in windows explorer when I am in MTP mode on my phone. File.DirRootExternal does not mean external SD card, it just means External in relation to the internal app storage that other apps cannot access. My windows explorer path to File.DirRootExternal is "SCH-I545\Phone" as opposed to "SCH-I545\Card". Can you post the current code you are having issues with?
 
Last edited:
Upvote 0

SteveAuckland

Member
Licensed User
Longtime User
File.DirRootExternal is where I am currently storing my SQLite database and I just verified that I can view it in windows explorer when I am in MTP mode on my phone. File.DirRootExternal does not mean external SD card, it just means External in relation to the internal app storage that other apps cannot access. My windows explorer path to File.DirRootExternal is "SCH-I545\Phone" as opposed to "SCH-I545\Card". Can you post the current code you are having issues with?
This is the key part, the error message highlights the FileCopy Line,
Steve
B4X:
Sub Activity_Create(FirstTime As Boolean)
   
Activity.LoadLayout("Main")
If File.Exists(File.DirDefaultExternal,"CarbInsulin.sql")=False Then
    File.Copy(File.DirAssets,"CarbInsulin.sql",File.DirDefaultExternal,"CarbInsulin.sql")
End If
If SQL1.IsInitialized=False Then
    SQL1.Initialize(File.DirDefaultExternal,"CarbInsulin.sql",False)
End If
Activity.Title="Insulin Calculator"
DBLoad
End Sub
 
Upvote 0

Eric H

Active Member
Licensed User
Longtime User
I didn't use dbutils to do it, but here is what I have for code in my project... maybe this will help you (even though I am not performing a copy):
B4X:
Sub Process_Globals
   
    Dim SQL1 As SQL

End Sub


Sub Activity_Create(FirstTime As Boolean)
   
    'create subdirectory 'ListDatabase' if it doesn't exist
    If File.Exists(File.DirRootExternal & "/ListDatabase/", "") = False Then
        File.MakeDir(File.DirRootExternal, "ListDatabase")
    End If
  
    'open database 'data.db' if it exists, create if necessary
    SQL1.Initialize(File.DirRootExternal & "/ListDatabase","data.db", True)
    Log("Database initialized? " & SQL1.IsInitialized)
          
    'create table 'list' to hold the list data if it doesn't exist
    SQL1.ExecNonQuery("CREATE TABLE IF NOT EXISTS list(ID INTEGER PRIMARY KEY, item BLOB, category TEXT, done BOOLEAN)")

End Sub

Edit: I just found out that File.MakeDir will only create a directory if it needs to (if it doesn't exist), so I guess I will try taking the 'If File.Exists' directory check out.

Source: http://www.b4x.com/android/wiki/index.php/Files#MakeDir_.28Parent_As_String.2C_Dir_As_String.29
 
Last edited:
Upvote 0

Eric H

Active Member
Licensed User
Longtime User
This is the key part, the error message highlights the FileCopy Line,
Steve
B4X:
Sub Activity_Create(FirstTime As Boolean)
  
Activity.LoadLayout("Main")
If File.Exists(File.DirDefaultExternal,"CarbInsulin.sql")=False Then
    File.Copy(File.DirAssets,"CarbInsulin.sql",File.DirDefaultExternal,"CarbInsulin.sql")
End If
If SQL1.IsInitialized=False Then
    SQL1.Initialize(File.DirDefaultExternal,"CarbInsulin.sql",False)
End If
Activity.Title="Insulin Calculator"
DBLoad
End Sub

see pic attached, it has a fix for a possible error you might see with your code.
 

Attachments

  • error.png
    error.png
    61.8 KB · Views: 239
Upvote 0
Top