If you want to copy database from assets at first time running and to open database from data/data/program. Name/database, you should use the code something like this.
Up vote 3 down vote favorite 2 share g+ share fb share tw.
I have my database stored in my assets folder and copied over at run time. I currently have a simple Activity that makes a simple database call: DBAdapter adapter = new DBAdapter(HomeActivity. This); final SQLiteDatabase db = adapter.
GetReadableDatabase(); final Cursor c = db. Query("exercises", new String { "name" }, null, null, null, null, null); startManagingCursor(c); if (c.moveToFirst()) Log. E(TAG, c.
GetString(0)); else Log. E(TAG,"No dice"); And below is my DBAdapter. Java (which extends open helper): public class DBAdapter extends SQLiteOpenHelper { private static final int DB_VERSION = 1; private static String DB_PATH = ""; private static final String DB_NAME = "gymrat.
Db"; private final Context myContext; private static final String TAG = "GymRat. DBAdapter"; /** * Constructor Takes and keeps a reference of the passed context in order to * access to the application assets and resources. * * @param context */ public DBAdapter(Context context) { super(context, DB_NAME, null, DB_VERSION); this.
MyContext = context; DB_PATH = "/data/data/" + context. GetApplicationContext().getPackageName() + "/databases/"; } /** * Copies your database from your local assets-folder to the just created * empty database in the system folder, from where it can be accessed and * handled. This is done by transferring bytestream.
* */ private void copyDatabase() throws IOException { InputStream myInput = myContext.getAssets(). Open(DB_NAME); String outFileName = DB_PATH + DB_NAME; OutputStream myOutput = new FileOutputStream(outFileName); byte buffer = new byte1024; int length; while ((length = myInput. Read(buffer)) > 0) { myOutput.
Write(buffer, 0, length); } myOutput.flush(); myOutput.close(); myInput.close(); } /** * Call on creating data base for example for creating tables at run time */ @Override public void onCreate(SQLiteDatabase db) { Log. E(TAG, "onCreate"); try { copyDatabase(); } catch (IOException e) { Log. E(TAG,"IOException copying Database"); } } @Override public void onOpen(SQLiteDatabase db) { Log.
E(TAG, "onOpen"); try { db. RawQuery("select * from exercises", null); } catch ( SQLiteException e) { Log. E(TAG,"DB copy didn't work"); try { copyDatabase(); } catch (IOException e1) { Log.
E(TAG,"IOException recopying DB"); } } } @Override public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) { Log. E(TAG, "Upgrading"); } } I took this adapter from here and modified it since it didn't seem to use the OpenHelper functionality. My Problem: Whenever my app is run for the first time after installing, or after wiping data on the app, onCreate gets called first as I would expect, and calls the copyDatabase method.
The problem is that the database does not get copied -- or if it does it is immediately overwritten. Instead a default database with only an "android meta_data" table is created, causing the query in my activity to throw an exception because the table doesn't exist. If I copy the database again in the onOpen method it works fine.
Am I doing something out of order or missing some call that is causing the SQLiteOpenHelper to create a default database? The default database created uses the same name and version number specified in the constructor. Right now, as you can see, I am resorting to using a dummy query in onOpen to see if the expected table exists and if not going ahead and recopying the database.
Another option is just setting a flag when onCreate is called signalling onOpen to copy the database over. Obviously these are both a bit hacky and I'm really curious what is going on. I plan on moving the actual database calls to separate helper classes outside of the Activity, I was merely calling the db directly to test it.
Android sqlite link|improve this question asked Mar 20 '11 at 3:53Dan49038.
If you want to copy database from assets at first time running and to open database from data/data/program. Name/database, you should use the code something like this: public class YourDatabase extends SQLiteOpenHelper public YourDatabase(Context context) { super(context, DatabaseName, null, DATABASE_VERSION); this. DbContext = context; // checking database and open it if exists if (checkDataBase()) { openDataBase(); } else { try { this.
GetReadableDatabase(); copyDataBase(); this.close(); openDataBase(); } catch (IOException e) { throw new Error("Error copying database"); } Toast. MakeText(context, "Initial database is created", Toast. LENGTH_LONG).show(); } } Full answer you can read from this question.
Thanks, I ended up doing something like this. I just hated the fact that all of these examples use SQLiteOpenHelper but don't use the onDatabaseCreate (or the other interface methods) for its implied purpose. I guess that method just assumes you are using the automatically created database.
– Dan Apr 21 '11 at 17:12.
I cant really gove you an answer,but what I can give you is a way to a solution, that is you have to find the anglde that you relate to or peaks your interest. A good paper is one that people get drawn into because it reaches them ln some way.As for me WW11 to me, I think of the holocaust and the effect it had on the survivors, their families and those who stood by and did nothing until it was too late.