Where is this database?

isk2837

Member
Licensed User
Longtime User
I created a database to access using this line of code -
B4X:
Sql1.Initialize(File.DirInternal,"Breakout.db",True)
The program can access the database no problem. However, I have an issue - I have no idea where this database is on my computer! I've tried searches for a file called Breakout.db without success. Eventually I'm going to be transferring this app onto a mobile phone, and I assume I'll need to know where the database file is so that I can transfer it over as well. So, where is File.DirInternal pointing to in my computer?
 

isk2837

Member
Licensed User
Longtime User
Changed the line to this -
B4X:
Sql1.Initialize(File.DirRootExternal,"Breakout.db",True)
and got this response - android.database.sqlite.SQLiteCantOpenDatabaseException:unable to open database file. Why would it be unable to open a database file?
 
Upvote 0

isk2837

Member
Licensed User
Longtime User
You must copy the .db file to the RootExternal path first before you try to initialize it.

B4X:
If File.Exists(File.DirRootExternal, "Breakout.db") = False Then
   File.Copy(File.DirInternal, "Breakout.db", File.DirRootExternal, "Breakout.db")
End If   
Sql1.Initialize(File.DirRootExternal,"Breakout.db",True)

Entered that code, and got this message - LastException java.io.FileNotFoundException: /mnt/sdcard/Breakout.db: open failed: EACCES (Permission denied) How do I get permission?
 
Upvote 0

isk2837

Member
Licensed User
Longtime User

Good news - changing my emulator so SD card size is 16MiB seems to have solved the problem of permission.

Bad news - I'm now getting this error message - LastException java.lang RuntimeException: Object should first be initialized (Label) for this line of code -
B4X:
lblPlayerName1.Text = PlayerName(0)
I've never been asked to initialize lblPlayer1 before altering the code - it didn't ask when it was still
B4X:
Activity.LoadLayout("HighScores")
Sql1.Initialize(File.DirInternal,"Breakout.db",True)
So, how do I initialize these labels?
 
Upvote 0

isk2837

Member
Licensed User
Longtime User
Are you sure that the name of the Label in the layout file is lblPlayerName1 ?
This error message Object should first be initialized (Label) means that you haven't defined lblPlayerName1 in the layout.

Best regards.

OK, I've done some experimenting, and it seems that the issue had something to do with this bit of code -
B4X:
If File.Exists(File.DirRootExternal, "Breakout.db") = False Then
    File.Copy(File.DirInternal, "Breakout.db", File.DirRootExternal, "Breakout.db")
End If
I say that because when I commented out that bit of code, the program ran fine, with no talk of needing to initialize anything. Now that I've created this database and successfully set it up so that my emulator can access it, what do I do when it's time to transfer the app to a mobile phone? I can't keep running it off my computer forever, sooner or later I will need to transfer it to a phone, so how do I make sure that the database is brought along to be accessed?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
To import the database to the device (it could be the same for the Emulator) you should add it to the project file folder, in the IDE Files Tab AddFiles.
Use a code like this:
B4X:
If File.Exists(File.DirRootExternal, "Breakout.db") = False Then
  File.Copy(File.DirAssets, "Breakout.db", File.DirRootExternal, "Breakout.db")
End If
File.DirAssets is the project file folder, but it's not writable. The database must be copied to another folder to use it.

But the Label initialize error has nothing to do with this.

Best regards.
 
Upvote 0

isk2837

Member
Licensed User
Longtime User
To import the database to the device (it could be the same for the Emulator) you should add it to the project file folder, in the IDE Files Tab AddFiles.
How do I do that when I don't know where the file I'm adding is? I know the file is in File.DirRootExternal, but still don't know where on my computer that is.
Use a code like this:
B4X:
If File.Exists(File.DirRootExternal, "Breakout.db") = False Then
  File.Copy(File.DirAssets, "Breakout.db", File.DirRootExternal, "Breakout.db")
End If
File.DirAssets is the project file folder, but it's not writable. The database must be copied to another folder to use it.

But the Label initialize error has nothing to do with this.

Best regards.
My code now looks like this:
B4X:
Sub Activity_Resume
   If File.Exists(File.DirRootExternal, "Breakout.db") = False Then
        File.Copy(File.DirAssets, "Breakout.db", File.DirRootExternal, "Breakout.db")
   End If
   Sql1.Initialize(File.DirRootExternal,"Breakout.db",True)
   Cursor1 = Sql1.ExecQuery("SELECT * FROM HighScores")
   For i = 0 To 4
      Cursor1.Position = i
      PlayerName(i) = Cursor1.GetString("PlayerName")
      PlayerScore(i) = Cursor1.GetInt("PlayerScore")
   Next
   Cursor1.Close
   lblPlayerName1.Text = PlayerName(0)
   lblPlayerScore1.Text = PlayerScore(0)
   lblPlayerName2.Text = PlayerName(1)
   lblPlayerScore2.Text = PlayerScore(1)
   lblPlayerName3.Text = PlayerName(2)
   lblPlayerScore3.Text = PlayerScore(2)
   lblPlayerName4.Text = PlayerName(3)
   lblPlayerScore4.Text = PlayerScore(3)
   lblPlayerName5.Text = PlayerName(4)
   lblPlayerScore5.Text = PlayerScore(4)
End Sub
When I run it, it gets stuck on the "File.Copy(File.DirAssets, "Breakout.db", File.DirRootExternal" line, saying: LastException java.io.FileNotFoundException:breakout.db
What changes would you recommend I make to it to achieve my goal? My goal is to make this app one that will work both on this computer and on a mobile phone. I can make it work fine on the computer, by accessing a database in File.DirRootExternal, but what about when it's on a phone?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
With the explanations you give now I would suggest you the following.
Save the datatbase in File.DirRootExternal on the device.
If connect your device with USB you have direct access to File.DirRootExternal, it's the root folder of the SDcard.
You could even use a subfolder.
You must turn on USB storage to transfer the file and set USB storage to off when running the program.
There are probably other possibilities but I have no other experience.
Doing this you don't need any File.Copy code.

Best regards.
 
Upvote 0

isk2837

Member
Licensed User
Longtime User
With the explanations you give now I would suggest you the following.
Save the datatbase in File.DirRootExternal on the device.
If connect your device with USB you have direct access to File.DirRootExternal, it's the root folder of the SDcard.
You could even use a subfolder.
You must turn on USB storage to transfer the file and set USB storage to off when running the program.
There are probably other possibilities but I have no other experience.
Doing this you don't need any File.Copy code.

Best regards.
Let me see if I understand what you're saying. You're saying I should connect the phone to my computer using the USB, then transfer the program into the part of the phone's memory for apps, and the database into the phone's SD card. And the phone will take File.DirRootExternal to mean "phone SD card" once the program is in the phone? So if the program is on the phone and running from there, and the database is in the SD card, then it will work on the phone?
 
Upvote 0

klaus

Expert
Licensed User
Longtime User
Yes !
Before posting my answer I tested it on my computer and the device.
I made a small txt file on the PC, copied it to the SDcard of the device via USB (even with a subfolder \Test).
The set USB storage to off and read the content of the file from a program on the device (I tested it with File.DirRootExternal and also with a subfolder File.DirRootExternal& "/Test").

Best regards.
 
Upvote 0

isk2837

Member
Licensed User
Longtime User
Yes !
Before posting my answer I tested it on my computer and the device.
I made a small txt file on the PC, copied it to the SDcard of the device via USB (even with a subfolder \Test).
The set USB storage to off and read the content of the file from a program on the device (I tested it with File.DirRootExternal and also with a subfolder File.DirRootExternal& "/Test").

Best regards.

Ok, that's good. There's just one remaining issue: where is the database I want to copy to the SD card? All I know is that it's stored in whatever part of my computer File.DirRootExternal points to when the program is being run through the emulator on the computer, but I don't know where it is. I don't think I have any external storage devices in my computer now, no pen drives or SD cards, so where has the computer put my database? I tried running the line
B4X:
Msgbox(File.DirRootExternal,"FE")
in the hope it would tell me, but all I got was a text box saying "/mnt/sdcard". So where is "/mnt/sdcard" on a computer?
 
Upvote 0

isk2837

Member
Licensed User
Longtime User
The emulator uses its own file system. You cannot directly access it from Windows. You can use a tool such as 'ddms' or 'adb' to copy files from the emulator.

You mean Dalvik Debug Monitor Server or Android Debug Bridge? So I download one of those and they'll let me copy my database file from in the emulator (I assume that /mnt/sdcard is somewhere in the emulator) to a SD card?
 
Upvote 0
Top