Downloading SQLite file from Internet problem!

DouglasNYoung

Active Member
Licensed User
Longtime User
Hi,
I am intending to distribute my app with an empty database, such that the reports will run, but just return 'no data'. The user then has the option of downloading a filled database to get the full system, as well as being able to update as and when required
.
The Original db File has a size of 612,352 bytes (created on my Android phone).

If I FTP it up to internet, and back down (just to check) it is still 612,352 bytes and the system works OK.

If however when I download with the application using HTTPUtils the file size after download is only 606,208 bytes! Some reports will still work, however some reports produce:-

'android.database.sqlite.SQLiteDatabaseCorruptException: database disk image is malformed'

Not really surprising since I've lost 6,000 bytes. However as some reports do still work I'm assuming that it is the tail end of the file that is missing, but I can't find anything obviously wrong.

<code>
Sub JobDone (Job As String)
Dim olddate, newdate As String
Dim sqlstring As String, sqlCursor As Cursor
Dim In As InputStream, Out As OutputStream

If HttpUtils.IsSuccess(dlfilename) Then
Select Job
Case "Job1"
'
Case "Job2"
' Handling input stream
File.copy2(HttpUtils.GetInputStream(dlfilename),File.OpenOutput(File.DirDefaultExternal, "update.db", False) )
File.copy(File.DirDefaultExternal,"grj.db",File.DirDefaultExternal,"old.db")
File.copy(File.DirDefaultExternal,"update.db",File.DirDefaultExternal,"grj.db")
'
ToastMessageShow("New Data downloaded OK!", False)
Button_InstallUpdate.Enabled = False

End Select
Else
Button_InstallUpdate.Enabled = False
ToastMessageShow("Can't Connect to Server: Check Internet Connection or Try Later!",True)
End If
End Sub
Sub Button_InstallUpdate_Click
dlfilename = datasrc
ToastMessageShow("Downloading Update - May take a minute or so. . .",True)
Log("Database=" & datasrc)

HttpUtils.CallbackActivity = "Main" 'Current activity name.
HttpUtils.CallbackJobDoneSub = "JobDone"
HttpUtils.Download("Job2", dlfilename)

End Sub
</code>

Any Ideas?

Douglas:confused::confused:
 

agraham

Expert
Licensed User
Longtime User
It might be because you don't explicitly close the OutputStream in your File.Copy2. I'd try a Flush and a Close on it and see if that works. File.Copy closes both the input and the output stream that it opens internally but Copy2 only closes the input stream given to it and leaves the ouput stream open.
 
Upvote 0

DouglasNYoung

Active Member
Licensed User
Longtime User
Problem Solved. . .

Many thanks - that fixed it. I remember seeing that File.Copy closed the streams and must have assumed copy2 did the same. As my wife tells me all the time 'One ought not assume!"
Once again - Many thanks!
 
Upvote 0

adamioan

Member
Licensed User
Longtime User
File.Copy2 leaves the output stream open to allow you to append other streams.
...and how do I close this stream?

EDIT: OK, I found it
B4X:
Dim OutStream As OutputStream

...

OutStream=File.OpenOutput(OutputFolder, RemoteFile, False)
OutStream.Flush 
File.copy2(HttpUtils.GetInputStream(URLCheck),OutStream )
OutStream.Close

Thanks anyway
 
Last edited:
Upvote 0

adamioan

Member
Licensed User
Longtime User
...and another problem came.
File is downloaded OK but with wrong character set.
I want to use windows-1253 and not UTF-8.
All the values written in Greek now are chinese :p
How can I get this thing to work?
 
Upvote 0

adamioan

Member
Licensed User
Longtime User
You should use TextReader to read a file encoded with a different encoding.

The matter is that I want to download an SQLite file (not a text file) from web and use it locally.
This file contains values with greek characters, that are not populated right.
Can you please tell me an example how to download it and then open in with the right encoding for use with SQL library?

Thanks in advance
 
Upvote 0

adamioan

Member
Licensed User
Longtime User
Thanks Erel, that was my mistake. I changed the character set in PHP before creating the database and now works.
Thanks again
 
Upvote 0
Top