Android Question Copy DB from asset

Kiran Raotole

Active Member
Licensed User
Hi expert

I'm trying to copy file from asset folder it shows error

java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.temp/files/temp1.db (No such file or directory)

while Log(File.Exists(File.DirDefaultExternal,"temp1.db")) shows true

my code :

If File.ExternalWritable Then TargetDir = File.DirDefaultExternal Else TargetDir = File.DirInternal
If File.Exists(TargetDir, "temp1.db") = False Then
File.Copy(File.DirAssets, "temp1.db", TargetDir, "temp1.db")
End If
 

Computersmith64

Well-Known Member
Licensed User
Longtime User
Hi expert

I'm trying to copy file from asset folder it shows error

java.io.FileNotFoundException: /storage/emulated/0/Android/data/com.temp/files/temp1.db (No such file or directory)

while Log(File.Exists(File.DirDefaultExternal,"temp1.db")) shows true

my code :

If File.ExternalWritable Then TargetDir = File.DirDefaultExternal Else TargetDir = File.DirInternal
If File.Exists(TargetDir, "temp1.db") = False Then
File.Copy(File.DirAssets, "temp1.db", TargetDir, "temp1.db")
End If

File.DirDefaultExternal isn't the assets folder. What API are you targeting in your manifest? If it is 23+ you will have to implement runtime permissions (which I'm guessing is the cause of your error). Runtime Permissions (Android 6.0+ Permissions)

- Colin.
 
Upvote 0

Kiran Raotole

Active Member
Licensed User
My sqlite db has some record but,

as file copied records are delete.

Log(ctron.ExecQuerySingleResult("SELECT count(*) FROM co") = 0

my copy command is File.Copy(File.DirAssets,mfilename,mdbpath,mfilename)

whats wrong ?
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
Log(ctron.ExecQuerySingleResult("SELECT count(*) FROM co") = 0
It looks like you are overwriting the file every time. You need to check if it exists or not:
B4X:
If File.Exists(mdbpath,mfilename) =False Then
        File.Copy(File.DirAssets,mfilename,mdbpath,mfilename)
    End If
When you post, use code tags when you post your code and show more code so members can understand what you want.
 
Upvote 0

Kiran Raotole

Active Member
Licensed User
It looks like you are overwriting the file every time. You need to check if it exists or not:
B4X:
If File.Exists(mdbpath,mfilename) =False Then
        File.Copy(File.DirAssets,mfilename,mdbpath,mfilename)
    End If
When you post, use code tags when you post your code and show more code so members can understand what you want.


I already done that
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I already done that
When you write 2 words as your answer, it shows you are not cooperating. You need to show a lot more of your code or better yet, upload your project: In IDE, click 'File', then choose 'Export as zip'. I do not think your problem is insurmountable. I am sure members will figure out what you are doing wrong.
 
Upvote 0

Mahares

Expert
Licensed User
Longtime User
I'm sorry, here is my project.
There are no records in the ctron.db in your assets. I opened it with a Database program and it has 0 records.
But, this is how your code should be. I made a few corrections: the log shows 0 rows.
B4X:
Sub Activity_Create(FirstTime As Boolean)
    sql_show
End Sub

Sub sql_show
    Dim mdbpath,mfilename As String
    mdbpath=File.DirInternal
    mfilename="ctron.db"
  
    If File.Exists(mdbpath,mfilename) =False Then
            File.Copy(File.DirAssets,mfilename,mdbpath,mfilename)
    End If
  
    If sql1.IsInitialized = False Then
        sql1.Initialize(mdbpath,mfilename,False)
    End If
  
    Log(sql1.IsInitialized)
    LogColor("Number of rows = " & sql1.ExecQuerySingleResult("SELECT count(*) FROM co"),Colors.Blue)
End Sub
 
Upvote 0

Kiran Raotole

Active Member
Licensed User
temp_error.png
this is how ctron.db show on my screen
 
Upvote 0
Top