Replacing .db from internet gives error

kaputo

Member
Licensed User
Longtime User
Hi, I have the below code to replace an sqlite database file from internet. The code seems to run OK, but after I have replaced the .db, when I run a query against the database I receive an error that the database disk image is malformed. All help appreciated, thanks

Sub JobDone (Job As String)

Dim In As InputStream, Out As OutputStream

If HttpUtils.IsSuccess(dlfilename) Then

In = HttpUtils.GetInputStream(dlfilename)
Out = File.OpenOutput(dir, "update.db", False)
Out.Flush
File.copy2(HttpUtils.GetInputStream(dlfilename),File.OpenOutput(dir, "update.db", False) )
Out.Close
SQL1.Close
File.copy(dir,"fiestas.db",dir,"old.db")
File.copy(dir,"update.db",dir,"fiestas.db")

SQL1.Initialize(dir, "fiestas.db", False)

ToastMessageShow("¡Ya los datos estan actualizados!", False)
'Button_InstallUpdate.Enabled = False

Else
'Button_InstallUpdate.Enabled = False
ToastMessageShow("No se puede conectar con el Servidor: ¡Compruebe tu conexion de Internet!",True)​
End If
HttpUtils.Complete = False​
End Sub
 

DouglasNYoung

Active Member
Licensed User
Longtime User
kaputo,
Looks to me like there is a sequence error in your program flow - Try it like this:-

B4X:
Sub JobDone (Job As String)
Dim In As InputStream, Out As OutputStream

If HttpUtils.IsSuccess(dlfilename) Then
  In = HttpUtils.GetInputStream(dlfilename)
  Out = File.OpenOutput(dir, "update.db", False)
  File.copy2(In,Out)
  Out.Flush
  Out.Close
  SQL1.Close
  File.copy(dir,"fiestas.db",dir,"old.db")
  File.copy(dir,"update.db",dir,"fiestas.db")

  SQL1.Initialize(dir, "fiestas.db", False)

  ToastMessageShow("¡Ya los datos estan actualizados!", False)
  'Button_InstallUpdate.Enabled = False

Else
'Button_InstallUpdate.Enabled = False
  ToastMessageShow("No se puede conectar con el Servidor: ¡Compruebe tu conexion de Internet!",True)
  End If
  HttpUtils.Complete = False
End Sub

cheers,
Douglas
 
Upvote 0

kaputo

Member
Licensed User
Longtime User
Great! Now working, thanks a lot

Hi, although error in title now fixed, I also get (but only sometimes!) another error - java.io.FileNotFoundException: /update.db (Permission Denied). I have checked Manifest and have the following line:
<uses-permission android:name="android.permission.WRITE_EXTERNAL_STORAGE"/>
I am copying update.db to sd card, so any ideas why I might receive a permissions error (although as mentioned not every time the code is run) - thanks
 
Upvote 0

kaputo

Member
Licensed User
Longtime User
The sd card might not be writable. You can check it with File.ExternalWritable.
When the device is mounted to the desktop (in storage mode), it makes the storage card read-only.

Hi, I use dbutils.CopyDBFromAssets to specify directory to write to, and in debug I see that that the sd card is writeable. Also the device is not mounted in storage mode. The problem does not occur all the time, only some of the time, but I can't see any pattern as to when it fails:(
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
Hi, I have the below code to replace an sqlite database file from internet. The code seems to run OK, but after I have replaced the .db, when I run a query against the database I receive an error that the database disk image is malformed. All help appreciated, thanks

Sub JobDone (Job As String)

Dim In As InputStream, Out As OutputStream

If HttpUtils.IsSuccess(dlfilename) Then

In = HttpUtils.GetInputStream(dlfilename)
Out = File.OpenOutput(dir, "update.db", False)
Out.Flush
File.copy2(HttpUtils.GetInputStream(dlfilename),File.OpenOutput(dir, "update.db", False) )
Out.Close
SQL1.Close
File.copy(dir,"fiestas.db",dir,"old.db")
File.copy(dir,"update.db",dir,"fiestas.db")

SQL1.Initialize(dir, "fiestas.db", False)

ToastMessageShow("¡Ya los datos estan actualizados!", False)
'Button_InstallUpdate.Enabled = False

Else
'Button_InstallUpdate.Enabled = False
ToastMessageShow("No se puede conectar con el Servidor: ¡Compruebe tu conexion de Internet!",True)​
End If
HttpUtils.Complete = False​
End Sub

I have a similar message when I try to use the mailparser library to download and replace the .db file that is using my app.

N.B. Before doing this I close the db using SQL.Close method.
Could be related to the db cache (Memory table?)
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
The size (kb) is the same of the sent file.
The problem occurs sometimes but I probably understood the problem.

In my case the download attachment procedure HandlePart was interrupted.
I have to check when the download procedure end in order to known when the file il well formed on the filesystem.
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
The size (kb) is the same of the sent file.
The problem occurs sometimes but I probably understood the problem.

In my case the download attachment procedure HandlePart was interrupted.
I have to check when the download procedure end in order to known when the file il well formed on the filesystem.

I have only a question about this.
There is a way to read only the mail that are present in the mail client cache ?

The ParseMail library process all the mail that found in the server's mailbox while in my client I have only one mail (all the other mails has been deleted).
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
Check the size of the new database. Is it exactly the same as the file you sent?

The size of the db downloaded is smaller than the original one.
The original is about 539 mb and the downloaded db is about 940 kb.

I'm using SMTP to send the mail and create attachment.
 
Upvote 0

luke2012

Well-Known Member
Licensed User
Longtime User
Sorry Iim wrong. The original il 539 kb.

Yes I sending it from a device to another device.
I'll give you further details when I can access to my B4A IDE.
 
Upvote 0
Top