【发布时间】:2011-06-07 11:16:47
【问题描述】:
我的应用程序在资产文件夹中有一个 SQLite 数据库。当用户启动我的应用程序时,会创建数据库和表。
这适用于很多设备(Nexus One、Htc Magic、SGS、X10……甚至是 Htc Desire HD v2.2)。 我的应用程序适用于所有版本的 Android(在我的设备(1.6、2.2、2.2.1 Htc Magic)和模拟器(v1,5 直到 v2.3)上测试。
我对 HTC DESIRE HD v2.2.1 1.72.405.3 有问题。
日志猫:
android.database.sqlite.SQLiteException: no such table: LISTE: , while compile: select _id from LISTE 在 android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2833) 在 android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2854) 在 android.app.ActivityThread.access$2300(ActivityThread.java:136) 在 android.app.ActivityThread$H.handleMessage(ActivityThread.java:2179) 在 android.os.Handler.dispatchMessage(Handler.java:99) 在 android.os.Looper.loop(Looper.java:143) 在 android.app.ActivityThread.main(ActivityThread.java:5068) 在 java.lang.reflect.Method.invokeNative(Native Method) 在 java.lang.reflect.Method.invoke(Method.java:521) 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:858) 在 com.android.internal.os.ZygoteInit.main(ZygoteInit.java:616) 在 dalvik.system.NativeStart.main(本机方法) 引起:android.database.sqlite.SQLiteException:没有这样的表:LISTE:,编译时:从LISTE中选择_id 在 android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 在 android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91) 在 android.database.sqlite.SQLiteCompiledSql.(SQLiteCompiledSql.java:64) 在 android.database.sqlite.SQLiteProgram.(SQLiteProgram.java:80) 在 android.database.sqlite.SQLiteQuery.(SQLiteQuery.java:46) 在 android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53) 在 android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1417) 在 android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1387) ... 11 更多
我的应用程序创建了数据库,但它没有复制 data\data\packagename\databases\mydatabase 中资产文件夹文件的表。
我的代码:
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();
}
我认为copydatabase 函数有问题但我没有看到。
此代码适用于除 HTC DESIRE HD v2.2.1 1.72.405.3 以外的所有设备。
上面给定版本的 HTC Desire 可能存在哪些问题?如何解决这个问题?
【问题讨论】:
-
您发布的代码用于创建数据库。您的错误涉及数据库中表中的 SELECT 语句。
-
是的,我的错误涉及 SELECT 语句,因为我的数据库是在用户发出第一个 sql 请求时创建的。例如创建了数据库但没有创建 LISTE 表,所以 SELECT 语句错误