1

My app needs to download a database from a link, heres the code I use to download the database adn save it on the sd card:

public void DownloadBD() {
    try {
        URL url = new URL(
                "http://mylink/dbHandler.axd?SqliteDbVersion=0");

        HttpURLConnection urlConnection = (HttpURLConnection) url
                .openConnection();

        urlConnection.setRequestMethod("GET");
        urlConnection.setDoOutput(true);
        urlConnection.connect();

        File SDCardRoot = Environment.getExternalStorageDirectory();
        File file = new File(SDCardRoot, "DirLaguna.db");

        FileOutputStream fileOutput = new FileOutputStream(file);

        InputStream inputStream = urlConnection.getInputStream();

        int totalSize = urlConnection.getContentLength();
        int downloadedSize = 0;

        byte[] buffer = new byte[1024];
        int bufferLength = 0;
        while ((bufferLength = inputStream.read(buffer)) > 0) {
            fileOutput.write(buffer, 0, bufferLength);
            downloadedSize += bufferLength;
        }
        fileOutput.close();
        location = file.getAbsolutePath();
    } catch (MalformedURLException e) {
        e.printStackTrace();
    } catch (IOException e) {
        e.printStackTrace();
    }
}

Everything seems to work fine, but at the moment I want to query the database (I'm using raw querys) an error appears saying that the tables don't exist.

After that, I tried debugging the application, and after that I get the error saying that the database is corrupted. I think that the downloading process is what is making the database corrupt.

Any help will be apreciated, if I'm missing some code to share please tell me so!

Thanks in advance!

Carlos Tirado
  • 297
  • 1
  • 5
  • 20

2 Answers2

2

1) The database you are downloading is sqlite database?

2) If yes, then you need to copy that in your application before you start using it.

The process will be something like you download the database in your Sd Card and as soon as its completed start copying it into your application so as it becomes part of your application, like this: http://www.reigndesign.com/blog/using-your-own-sqlite-database-in-android-applications/

Make this happen in background by separate thread and then you are good to go and use it. Let me know if you face any problem or issue or I didn't provided you info what you meant.

Thanks

abhy
  • 933
  • 9
  • 13
  • Your tutorial is good, the problem is you modify the table and then add it to the application, i need to add it first and then modify, I'm still gonna try this method, thanks! – Carlos Tirado Jul 05 '12 at 21:21
  • Apologies for the late reply, no we are not modifying and adding rather its opposite only. We cannot use databases directly. You will require to copy it programatically in your apk as you want to modify as you cannot directly interact with the db file and modify the content. It needs to become part of application so that Android can provide SQLite Apis to you so that you can run queries. Also, please consider Romins second link. It will clear your doubts – abhy Jul 09 '12 at 07:07
0

I suggest that first - you verify if the file is indeed a proper SQLite Database by moving it to your local machine filesystem and viewing it in a SQLite viewer application.

Several SQLite Database viewers are available. This post lists several of them : What are good open source GUI SQLite database managers?

This additional post discusses readymade database usage in Android : Ship an application with a database You can get several pointers from there.

Community
  • 1
  • 1
Romin
  • 8,708
  • 2
  • 24
  • 28