Sunday, August 14, 2011

Fixing an sqlite compile error from the wild

Despite a couple of attempts to patch it, I'm still getting reports of an sqlite error compiling and sqlite statement

android.database.sqlite.SQLiteException: no such table: v_all_words_by_level_freq_sequence: , while compiling: SELECT _id, level, number, kanji, hiragana, english, level_freq_sequence FROM v_all_words_by_level_freq_sequence WHERE level = ?

Let's start from scratch and make sure the table (view actually) exists, although it has to to, otherwise it wouldn't be working on my machine.

Yup, it's there.

So, what line did it crash on?

at android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1269)
at com.jlptquiz.app.DataBaseHelper.selectByLevel(DataBaseHelper.java:415)
at com.jlptquiz.app.InitUtils.initializeWordGroupings(InitUtils.java:425)
at com.jlptquiz.app.InitUtils.initialize(InitUtils.java:87)

Let's walk through it:


// InitUtils, line 87:

InitUtils.initialize(StartActivity.this);

// The last line of the sequence is InitUtils, line 425

DataBaseHelper myDbHelper = DataBaseHelper.createDB(context);

myDbHelper.openDataBase();

rows = myDbHelper.selectByLevel(appState.jlptLevel);

Let's take a look at createDb:


if (myDbHelper == null) {
myDbHelper = new DataBaseHelper(context);

try {

myDbHelper.createDataBase();

} catch (IOException ioe) {

throw new Error("Unable to create database");

}

try {

// we'll call open specifically from the client
myDbHelper.openDataBase();

} catch (SQLException sqle) {

throw sqle;

}
}

return myDbHelper;





First it calls "createDatabase" which is kind of confusing since the method name is "createDb". Then it calls openDb.


Let's see what createDatabase does:

/**
* Creates a empty database on the system and rewrites it with your own
* database.
*
* */
public void createDataBase() throws IOException {
// Log.d(TAG, "createDatabase entry");

boolean dbExist = checkDataBase();

SQLiteDatabase dbRead = null;

if (dbExist) {
// do nothing - database already exist
} else {

// By calling this method an empty database will be created into
// the default system path
// of your application so we are gonna be able to overwrite that
// database with our database.
dbRead = this.getReadableDatabase();
dbRead.close();

copyDataBase();

}

// Log.d(TAG, "createDatabase exit");

}


Essentially, all this is doing is checking if the database exists. If it doesn't exist, it then copies it, using InputStream, File, OutputStream, etc. It's easily findable in the internet.


Here's my solution - give it a bit of time:

public ArrayList selectByLevel(int levelIn) {

String strLevel = Integer.toString(levelIn);

if (mFirstTime) {
this.mFirstTime = false;
try {
Thread.sleep(100);
} catch (InterruptedException e) {
// TODO Auto-generated catch block
e.printStackTrace();
}
}




No comments:

Post a Comment