My application has a SQLite database in the asset folder. When the user launches my application, the database is created and the tables too.
This works fine with a lot of devices (Nexus One, Htc Magic, SGS, X10… and even Htc Desire HD v2.2). My application works with all versions of Android (tested on my device (1.6, 2.2, 2.2.1 Htc Magic) and on the emulator (v1,5 until v2.3).
I have just a problem with HTC DESIRE HD v2.2.1 1.72.405.3.
The logcat:
android.database.sqlite.SQLiteException: no such table: LISTE: , while compiling: select _id from LISTE at androi开发者_JAVA百科d.app.ActivityThread.performLaunchActivity(ActivityThread.java:2833) at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2854) at android.app.ActivityThread.access$2300(ActivityThread.java:136) at android.app.ActivityThread$H.handleMessage(ActivityThread.java:2179) at android.os.Handler.dispatchMessage(Handler.java:99) at android.os.Looper.loop(Looper.java:143) at android.app.ActivityThread.main(ActivityThread.java:5068) at java.lang.reflect.Method.invokeNative(Native Method) at java.lang.reflect.Method.invoke(Method.java:521) at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) at dalvik.system.NativeStart.main(Native Method) Caused by: android.database.sqlite.SQLiteException: no such table: LISTE: , while compiling: select _id from LISTE at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91) at android.database.sqlite.SQLiteCompiledSql.(SQLiteCompiledSql.java:64) at android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:80) at android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:46) at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53) at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1417) at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1387) ... 11 more
My application create the database but it doesn’t copy the tables of the file of the asset folder in data\data\packagename\databases\mydatabase
.
My code:
public void createDataBase() throws IOException{
boolean dbExist = checkDataBase();
if(dbExist){
//do nothing - database already exist
}else{
//By calling this method and 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.
this.getReadableDatabase();
try {
copyDataBase();
} catch (IOException e) {
throw new Error("Error copying database");
}
}
}
private void copyDataBase() throws IOException{
//Open your local db as the input stream
InputStream myInput = myContext.getAssets().open(DB_NAME);
// Path to the just created empty db
String outFileName = DB_PATH + DB_NAME;
//Open the empty db as the output stream
OutputStream myOutput = new FileOutputStream(outFileName);
//transfer bytes from the inputfile to the outputfile
byte[] buffer = new byte[1024];
int length;
while ((length = myInput.read(buffer))!= -1){
if (length > 0){
myOutput.write(buffer, 0, length);
} }
//Close the streams
myOutput.flush();
myOutput.close();
myInput.close();
}
I think that the copydatabase
function has a problem but I don't see.
This code works fine with all devices except the HTC DESIRE HD v2.2.1 1.72.405.3.
What problems might exist here for the HTC Desire with the given version above? How can this be remedied?
This is a bit of a guess and would imply there is a 'bug' in that version of Android for the Desire HD so I may be wildly out.
I'm wondering if that version of Android doesn't create the database where it should do. You're assuming that DB_PATH is \data\data\packagename\databases\ as it normally would be but what if it isn't? The result would be the following...
this.getReadableDatabase()
would create an empty database at \some\incorrect\path\mydatabase- Using
new FileOutputStream(outFileName)
simply creates a new empty binary file because the above didn't create a database at the place you expect. - The copy happens successfully
- Before performing the SELECT request, access to the database is requested using either
getReadableDatabase()
orgetWriteableDatabase()
but both of those will simply open the empty database that was created in Step 1.
In short, the copy process may well have worked correctly but the DB that is being operated on is empty and in a different place to where you expect it.
Just an idea (I've seen similar things happen over the years).
精彩评论