How to use a ready database on Android with Sqlite

Asked

Viewed 343 times

0

I am developing an application that downloads a database according to the state (UF) of the user. Does anyone know how to enter the data into the database? Note: the database file, can reach there 200 MB.

2 answers

0

Look, I’ve done something similar but I don’t know if it’ll help, but come on:

Feed the database manually using JSON I did the load in the database using a service. This service returned a paged JSON of 1000 in 1000 records and I included it in the database.

Load the binary database into your app soen source

You can use this method to record in your private Storage

private static boolean downloadDatabase(Context context) {
            try {
                   // Log.d(TAG, "downloading database");
                    URL url = new URL("http://some-url.com/db/" + "db_name.s3db");
                    /* Open a connection to that URL. */
                    URLConnection ucon = url.openConnection();
                    /*
                     * Define InputStreams to read from the URLConnection.
                     */
                    InputStream is = ucon.getInputStream();
                    BufferedInputStream bis = new BufferedInputStream(is);
                    /*
                     * Read bytes to the Buffer until there is nothing more to read(-1).
                     */
                    ByteArrayBuffer baf = new ByteArrayBuffer(50);
                    int current = 0;
                    while ((current = bis.read()) != -1) {
                            baf.append((byte) current);
                    }

                    /* Convert the Bytes read to a String. */
                    FileOutputStream fos = null;
                    // Select storage location
                    fos = context.openFileOutput("db_name.s3db", Context.MODE_PRIVATE); 

                    fos.write(baf.toByteArray());
                    fos.close();
                   // Log.d(TAG, "downloaded");
            } catch (IOException e) {
                    Log.e(TAG, "downloadDatabase Error: " , e);
                    return false;
            }  catch (NullPointerException e) {
                    Log.e(TAG, "downloadDatabase Error: " , e);
                    return false;
            } catch (Exception e){
                    Log.e(TAG, "downloadDatabase Error: " , e);
                    return false;
            }
            return true;
    }
  • I thought of turning the database into JSON and insert with an Activity, but I think it’s too big, the application might close. I don’t know, I’m new. But if the file takes too long to open on my computer, imagine processing the text on a smartphone, but if it worked for you, it should work. Now little found a class that I think will solve the problem. https://github.com/jgilfelt/android-sqlite-asset-helper

  • @Jorgyanribeiro If you turn the database into JSON you must load the records in block. But just below has an example to download the sqlite database to your app.

0


Browser other questions tagged

You are not signed in. Login or sign up in order to post.