Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You should copy your DB file from assets folder to your installed application.<br/> for example:</p> <pre><code>public class DatabaseOpenHelper extends SQLiteOpenHelper { private final static String DB_NAME = "YourDatabaseFile.sqlite"; private static String DB_PATH = "/data/data/%s/databases/"; private final String ERROR_TAG = "error"; private final static int DB_VERSION = 1; private final int BUFFER_SIZE = 8 * 1024; private SQLiteDatabase databaseHandle; private final Context context; public DatabaseOpenHelper(Context context) { super(context, DB_NAME, null, DB_VERSION); this.context = context; DB_PATH = String.format(DB_PATH, context.getPackageName()); } public SQLiteDatabase openDataBase() { try { String databasePath = DB_PATH + DB_NAME; if (databaseHandle == null) { createDataBase(); databaseHandle = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READWRITE); } } catch (SQLiteException e) { throw new IllegalStateException(context.getResources().getString(R.string.err_opening_db), e); } return databaseHandle; } private boolean createDataBase() { try { if (!isDataBase()) { this.getReadableDatabase(); copyDataBase(); return true; } } catch (SQLiteException e) { throw new IllegalStateException(context.getResources().getString(R.string.err_opening_db), e); } catch (IOException e){ Log.e(ERROR_TAG, context.getResources().getString(R.string.err_close_stream), e); } return false; } public boolean isDataBase() { SQLiteDatabase verifiableDatabase = null; try { String databasePath = DB_PATH + DB_NAME; verifiableDatabase = SQLiteDatabase.openDatabase(databasePath, null, SQLiteDatabase.OPEN_READONLY); verifiableDatabase.close(); } catch (SQLiteException e) { Log.e(ERROR_TAG, context.getResources().getString(R.string.err_opening_db), e); return false; } return true; } private void copyDataBase() throws IOException { InputStream externalDbStream = null; OutputStream localDbStream = null; try { externalDbStream = context.getAssets().open(DB_NAME); localDbStream = new FileOutputStream(DB_PATH+DB_NAME); byte[] buffer = new byte[BUFFER_SIZE]; int bytesRead; while ((bytesRead = externalDbStream.read(buffer)) &gt; 0) { localDbStream.write(buffer, 0, bytesRead); } } catch (IOException e) { throw new IllegalStateException(context.getResources().getString(R.string.err_copying_db), e); } finally { if (localDbStream != null) localDbStream.close(); if (externalDbStream != null) externalDbStream.close(); } } @Override public void close() { databaseHandle.close(); } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { }} </code></pre> <p>Using:<br/> <code>DatabaseOpenHelper dbHelper = new DatabaseOpenHelper(context);<br> SQLiteDatabase database = dbHelper.openDataBase();</code></p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload