If we take a look back at that, we see that it actually checks for the existence of a database, and if it's not there, it creates it. Since there's one already there, we need to figure out how to resolve this problem. Of course, we could go and manually delete it, once I figure out how root the phone, but I'm still feeling a bit gun-shy about this. It's silly, of course, since the Nexus One is designed to be easily rooted, but I just don't feel like mucking around with that just yet.
Instead, let's explore the code used for creating the database. There was also another blog I mentioned which talked about backing up an existing database, creating the new one from resources, copy the old data into it, and then deleting the backup, here:
http://stackoverflow.com/questions/513084/how-to-ship-an-android-application-with-a-database.
Let's examine this code. My comments will be on the left margins, starting with "//"
package android.example;
import android.app.Activity;
import android.database.sqlite.SQLiteDatabase;
import android.os.Bundle;
// This is a separate activity which handles the database functionality
public class Database extends Activity {
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
DatabaseHelper myDbHelper;
SQLiteDatabase myDb = null;
// most likely the clase that extends SQLiteOpenHelper
myDbHelper = new DatabaseHelper(this);
/*
* Database must be initialized before it can be used. This will ensure
* that the database exists and is the current version.
*/
myDbHelper.initializeDataBase();
try {
// I should check my code - I think it's only using the database as read, which is ok for now but may change.
// A reference to the database can be obtained after initialization.
myDb = myDbHelper.getWritableDatabase();
/*
* Place code to use database here.
*/
} catch (Exception ex) {
ex.printStackTrace();
} finally {
try {
myDbHelper.close();
} catch (Exception ex) {
ex.printStackTrace();
} finally {
myDb.close();
}
}
}
}
The main code is here. Let's break it down.
package android.example;
import java.io.FileOutputStream;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import android.content.Context;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
// As I suspected, this extends SQLLiteOpenHelper.
public class DatabaseHelper extends SQLiteOpenHelper {
/*
* The Android's default system path of the application database in internal
* storage. The package of the application is part of the path of the
* directory.
*/
private static String DB_DIR = "/data/data/android.example/databases/";
private static String DB_NAME = "database.sqlite";
private static String DB_PATH = DB_DIR + DB_NAME;
// notice the old path that will be backed up to.
private static String OLD_DB_PATH = DB_DIR + "old_" + DB_NAME;
private final Context myContext;
private boolean createDatabase = false;
private boolean upgradeDatabase = false;
// good explanation of why it needs the context.
/**
* Constructor Takes and keeps a reference of the passed context in order to
* access to the application assets and resources.
*
* @param context
*/
public DatabaseHelper(Context context) {
super(context, DB_NAME, null, context.getResources().getInteger(
R.string.databaseVersion));
myContext = context;
// Get the path of the database that is based on the context.
DB_PATH = myContext.getDatabasePath(DB_NAME).getAbsolutePath();
}
// This is what we're interested in.
/**
* Upgrade the database in internal storage if it exists but is not current.
* Create a new empty database in internal storage if it does not exist.
*/
public void initializeDataBase() {
/*
* Creates or updates the database in internal storage if it is needed
* before opening the database. In all cases opening the database copies
* the database in internal storage to the cache.
*/
// We will need to check what this does. It probably sets these booleans:
// private boolean createDatabase = false;
// private boolean upgradeDatabase = false;
getWritableDatabase();
if (createDatabase) {
// This is the technique we are using. But, I'm not sure what the "caching" reference means.
/*
* If the database is created by the copy method, then the creation
* code needs to go here. This method consists of copying the new
* database from assets into internal storage and then caching it.
*/
try {
/*
* Write over the empty data that was created in internal
* storage with the one in assets and then cache it.
*/
// Once the database is created, do a file copy from assets.
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
} else if (upgradeDatabase) {
/*
* If the database is upgraded by the copy and reload method, then
* the upgrade code needs to go here. This method consists of
* renaming the old database in internal storage,
// ok, we need to do that. But how?
* create an empty new database in internal storage,
// yes
* copying the database from assets to the new database in internal storage,
// Yes
* caching the new database from internal storage
// Caching question remains
* loading the data from the old database into the new database in the cache
// In the cache? In storage, then?
* and then deleting the old database from internal storage.
// This last may be all we need to do - at least for now.
try {
// copy the old file
FileHelper.copyFile(DB_PATH, OLD_DB_PATH);
//? * renaming the old database in internal storage,
//? * create an empty new database in internal storage,
// * copying the database from assets to the new database in internal storage,
copyDataBase();
SQLiteDatabase old_db = SQLiteDatabase.openDatabase(OLD_DB_PATH, null, SQLiteDatabase.OPEN_READWRITE);
SQLiteDatabase new_db = SQLiteDatabase.openDatabase(DB_PATH,null, SQLiteDatabase.OPEN_READWRITE);
// It would help to fill this in this part, doncha think?
/*
* Add code to load data into the new database from the old
* database and then delete the old database from internal
* storage after all data has been transferred.
*/
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
/**
* 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 transfering bytestream.
* */
private void copyDataBase() throws IOException {
/*
* Close SQLiteOpenHelper so it will commit the created empty database
* to internal storage.
*/
close();
/*
* Open the database in the assets folder as the input stream.
*/
InputStream myInput = myContext.getAssets().open(DB_NAME);
/*
* Open the empty db in interal storage as the output stream.
*/
OutputStream myOutput = new FileOutputStream(DB_PATH);
/*
* Copy over the empty db in internal storage with the database in the
* assets folder.
*/
FileHelper.copyFile(myInput, myOutput);
/*
* Access the copied database so SQLiteHelper will cache it and mark it
* as created.
*/
getWritableDatabase().close();
}
/*
* This is where the creation of tables and the initial population of the
* tables should happen, if a database is being created from scratch instead
* of being copied from the application package assets.
* Copying a database
* from the application package assets to internal storage inside this
* method will result in a corrupted database.
*
* NOTE: This method is normally only called when a database has not already
* been created.
// there is apparently some kind of check against the cache, and if the
// database has been changed, this method is called.
* When the database has been copied, then this method is
* called the first time a reference to the database is retrieved after the
* database is copied since the database last cached by SQLiteOpenHelper is
* different than the database in internal storage.
*/
@Override
public void onCreate(SQLiteDatabase db) {
/*
* Signal that a new database needs to be copied. The copy process must
* be performed after the database in the cache has been closed causing
* it to be committed to internal storage. Otherwise the database in
* internal storage will not have the same creation timestamp as the one
* in the cache causing the database in internal storage to be marked as
* corrupted.
*/
// This is basically used to set the onCreate flag. The database would have been renamed, causing this method to be called.
createDatabase = true;
/*
* This will create by reading a sql file and executing the commands in
* it.
*/
// try {
// InputStream is = myContext.getResources().getAssets().open(
// "create_database.sql");
//
// String[] statements = FileHelper.parseSqlFile(is);
//
// for (String statement : statements) {
// db.execSQL(statement);
// }
// } catch (Exception ex) {
// ex.printStackTrace();
// }
}
/**
* Called only if version number was changed and the database has already
* been created. Copying a database from the application package assets to
* the internal data system inside this method will result in a corrupted
* database in the internal data system.
*/
// So, basically, this needs to just be used to set a flag to do whatever needs to be done on upgrade.
// Otherwise, the dreaded corruption happens.
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
/*
* Signal that the database needs to be upgraded for the copy method of
* creation. The copy process must be performed after the database has
* been opened or the database will be corrupted.
*/
upgradeDatabase = true;
/*
* Code to update the database via execution of sql statements goes
* here.
*/
/*
* This will upgrade by reading a sql file and executing the commands in
* it.
*/
// try {
// InputStream is = myContext.getResources().getAssets().open(
// "upgrade_database.sql");
//
// String[] statements = FileHelper.parseSqlFile(is);
//
// for (String statement : statements) {
// db.execSQL(statement);
// }
// } catch (Exception ex) {
// ex.printStackTrace();
// }
}
/**
* Called everytime the database is opened by getReadableDatabase or
* getWritableDatabase. This is called after onCreate or onUpgrade is
* called.
*/
@Override
public void onOpen(SQLiteDatabase db) {
super.onOpen(db);
}
/*
* Add your public helper methods to access and get content from the
* database. You could return cursors by doing
* "return myDataBase.query(....)" so it'd be easy to you to create adapters
* for your views.
*/
}
Ok. So it looks like it's all triggered by the version number. If it's different, then upgrade will be called, and a bunch more stuff has to be done than if it's not there.
One thing I could do is reload the app with a version number set, and if it's different, just delete the database and then use the create. If I ever need to upgrade, the structure will be in place. But first I need to figure out, how to delete the database. Let's take a look at the file helper from the same post.
Nope. That just has file copy methods.
Ok - it looks like it may be this simple:
context.deleteDatabase(DATABASE_NAME);
First, let's add a database version number to res/values/strings.xml, as recommended in the above-mentioned link:
Actually that's different then the straight string format I've been using. Well, using the "do the minimum" concept of TDD, let's just go with the tried and true:
And in the DataBaseHelper code:
public void createDataBase() throws IOException {
String myPath = DB_PATH + DB_NAME;
int myDatabaseVersion = Integer.parseInt(myContext.getString(R.string.databaseVersion));
if (1 == myDatabaseVersion){
myContext.deleteDatabase(myPath);
}
boolean dbExist = checkDataBase();
Let's copy our database over to resources, fire up the nexus one, and give it a try!
Ok, it seems to be working ok, and we have our kanji display back. You may recall, way back, when we were having some trouble getting the kanji to display due to not setting the utf-8 encoding on the SQLite database. Let's comment out the database delete code, and run the unit tests.
Uh oh. Insufficient storage!
For now I just killed all the apps and re-ran and they ran. But, I'll have to figure out how to check the available memory and disks space - and probably how to delete that database resource. I'll need to know how much space my app is taking up, and if it is running in core or on the SD card.
Ok, one unit test failed:
junit.framework.ComparisonFailure: expected:<青> but was:<大分>
Ouch, when I rerun the test, to see if it was a randomization problem, I get a null pointer exception. I'm wondering, though, if I've done the test correctly. I've converted from XML to the database, but IIRC, I only converted at one point in the program. The rest is still looking at XML. Still, that was just a test to see if what was retrieved from the database matched the display.
Ok, what I'll do for now is comment out the test, so I can check in, and tackle it again in my next post. It's very likely a test issue, since my manual check was fine.
No comments:
Post a Comment