0

All the tutorials I have seen about using a SQLite db in Android create the database themselves. It sort of seems like this is a requirement. Is is not possible to create the db using a SQLite workbench, and install it in the Android apps folders, in the correct location?

ProfK
  • 49,207
  • 121
  • 399
  • 775

2 Answers2

3

IT is possible.

Just create your SQLite database and place it in your asset folder.

When the app is first launched simply check whether or not the db exists and create it if doesn't exist.

    public String DB_PATH = null;
    public final static String DB_NAME = "mye.db"; //take note this is in your asset folder
    private static final int DB_VERSION = 1;
    private SQLiteDatabase myDatabase;
    private Context myContext = null;

        public DBAdapter(Context context) {
        super(context, DB_NAME, null, DB_VERSION);
        this.myContext = context;
        DB_PATH = "/data/data/" + context.getPackageName() + "/databases/";
    }

        /**
     * Creates a empty database on the system and rewrites it with your own
     * database.
     * */
    public void createDataBase() throws IOException {

        this.getReadableDatabase();

        try {
            copyDatabase();
        } catch (IOException e) {

            throw new Error("Error copying database");

        }
    }

    /**
     * Copy DB from ASSETS
     */

    public void copyDatabase() throws IOException {

        // Open your local db as the input stream
        InputStream myInput = myContext.getAssets().open(DB_NAME);

        // Path to the just created empty db
        String outFileName = DB_PATH + DB_NAME;

        // Open the empty db as the output stream
        OutputStream myOutput = new FileOutputStream(outFileName);

        // transfer bytes from the inputfile to the outputfile
        byte[] buffer = new byte[1024];
        int length;
        while ((length = myInput.read(buffer)) > 0) {
            myOutput.write(buffer, 0, length);
        }

        // Close the streams
        myOutput.flush();
        myOutput.close();
        myInput.close();
    }

    /**
     * Opens Database . Method is synhcronized
     * @throws SQLException
     */
    public synchronized void openDatabase() throws SQLException {
        String dbPath = DB_PATH + DB_NAME;
        myDatabase = SQLiteDatabase.openDatabase(dbPath, null,
                SQLiteDatabase.OPEN_READWRITE);
    }

    /**
     * Closes database. 
     * Method is synchronized for protection
     */
    @Override
    public synchronized void close() {
        if (myDatabase != null) {
            myDatabase.close();
        }
        super.close();
    }


    /**
     * Check if the database exists
     * 
     * @param cntx
     * @return true / false
     */
    public boolean checkDatabase(Context cntx) {
        File dbFile = cntx.getDatabasePath(DB_NAME);
//      Log.e("zeus", "Check db returns : " + dbFile.exists());
        return dbFile.exists();

    }

In your main activity or class that extends application just get an instance to your DBAdapter and check if the db exists.

mDBAdapter = new DBAdapter(getApplicationContext());
if (!mDBAdapter.checkDatabase(getApplicationContext())) {
    try {
        mDBAdapter.createDataBase();
    } catch (IOException e) {
        e.printStackTrace();
    }
}
Daniel M.
  • 486
  • 5
  • 7
  • And how to I place it in my asset folder? When I connect the phone to my PC I can find no such folder? – ProfK Oct 18 '14 at 18:52
  • I'm not sure you understood what I've written. In your app folder structure you have src / libs / res , etc. There should also be a folder named "assets" , if there isn't one , create it and place your database there . – Daniel M. Oct 19 '14 at 12:55
0

Is is not possible to create the db using a SQLite workbench, and install it in the Android apps folders, in the correct location?

You are welcome to do that, using SQLiteAssetHelper as your means of packaging and deploying the database.

Usually, though, this is only done if you have significant data that you want to package with the app.

CommonsWare
  • 986,068
  • 189
  • 2,389
  • 2,491