Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>You have a function to copy the DB, but you do not show <em>where</em> you are calling it from... that can affect the outcome. I usually call such a thing in the constructor of my db helper class (and it is all contained in there too).</p> <p><strong>EDIT</strong></p> <p>Full example</p> <pre><code>public class DBAdapter { // DB info public static final String MAIN_DATABASE_NAME = "Bowers"; public static final int MAIN_DATABASE_VERSION = 1; public static String MAIN_DB_PATH = "/data/data/cdc.workshopapps.BowersMfgHelper/databases/"; // static strings removed for brevity // database control variables private DatabaseHelper mDbHelper; private static SQLiteDatabase mDb; private static Context mCtx; private static class DatabaseHelper extends SQLiteOpenHelper { DatabaseHelper(Context context, String dbname, int dbversion) { super(context, dbname, null, dbversion); if (checkDataBase(dbname)) { openDataBase(dbname); } else { try { this.getReadableDatabase(); copyDataBase(dbname); this.close(); openDataBase(dbname); } catch (IOException e) { throw new Error("Error copying database"); } Toast.makeText(context, "Initial " + dbname + " database has been created", Toast.LENGTH_LONG).show(); } } @Override public void onCreate(SQLiteDatabase db) { } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { } } public DBAdapter(Context ctx) { DBAdapter.mCtx = ctx; } public DBAdapter open(String dbname, int dbversion) throws SQLException { mDbHelper = new DatabaseHelper(mCtx, dbname, dbversion); mDb = mDbHelper.getWritableDatabase(); return this; } public void close() { mDbHelper.close(); } private static void copyDataBase(String dbname) throws IOException { InputStream myInput = mCtx.getAssets().open(dbname); String outFileName = MAIN_DB_PATH + dbname; OutputStream myOutput = new FileOutputStream(outFileName); byte[] buffer = new byte[1024]; int length; while ((length = myInput.read(buffer)) &gt; 0) { myOutput.write(buffer, 0, length); } myOutput.flush(); myOutput.close(); myInput.close(); } private static boolean checkDataBase(String dbname) { SQLiteDatabase checkDB = null; boolean exist = false; try { String db = MAIN_DB_PATH + dbname; checkDB = SQLiteDatabase.openDatabase(db, null, SQLiteDatabase.OPEN_READONLY); } catch (SQLiteException e) { Log.v("db log", "database does't exist"); } if (checkDB != null) { exist = true; checkDB.close(); } return exist; } private static void openDataBase(String dbname) throws SQLException { String dbPath = MAIN_DB_PATH + dbname; mDb = SQLiteDatabase.openDatabase(dbPath, null, SQLiteDatabase.OPEN_READWRITE); } // insert, update, delete and cursor methods follow, removed for brevity } </code></pre> <p>I've used this up to Android 3.2...</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.
    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