2011-08-25 97 views
5

我最近向市场发布了一个应用程序。从开发者控制台看来,大约有1-2%的用户遇到此问题。 1-2%很小,但是当有些东西不起作用时,人们更倾向于留言,而不是在可能对下载产生负面影响的时候。SQLiteException - 仅发生在某些设备上

不幸的是,开发者控制台只将平台列为“其他”,但我的应用程序适用于那些SDK 1.6+。我也无法重新创建这个问题,并且没有用户直接与我联系,所以我无法获得关于它失败的设备的更多信息。

这里是堆栈

android.database.sqlite.SQLiteException: no such table: QUESTIONS: , while compiling: SELECT * FROM QUESTIONS WHERE DIFFICULTY=2 ORDER BY RANDOM() LIMIT 20 
at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 
at android.database.sqlite.SQLiteCompiledSql.compile(SQLiteCompiledSql.java:91) 
at android.database.sqlite.SQLiteCompiledSql.<init>(SQLiteCompiledSql.java:64) 
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:80) 
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:46) 
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:53) 
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1434) 
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1404) 
at com.app.myapp.db.DBHelper.getQuestionSet(DBHelper.java:140) 
at com.app.myapp.SplashActivity.getQuestionSetFromDb(SplashActivity.java:109) 
at com.app.myapp.SplashActivity.onClick(SplashActivity.java:58) 
at android.view.View.performClick(View.java:2421) 
at android.view.View$PerformClick.run(View.java:8867) 
at android.os.Handler.handleCallback(Handler.java:587) 
at android.os.Handler.dispatchMessage(Handler.java:92) 
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) 

getQuestionSetFromDb(SplashActivity.java:109)是指

List<Question> questions = myDbHelper.getQuestionSet(diff, numQuestions); 

其中提到

public List<Question> getQuestionSet(int difficulty, int numQ){ 
    List<Question> questionSet = new ArrayList<Question>(); 
    Cursor c = myDataBase.rawQuery("SELECT * FROM QUESTIONS WHERE DIFFICULTY=" + difficulty + 
      " ORDER BY RANDOM() LIMIT " + numQ, null); 
    while (c.moveToNext()){ 
     //Log.d("QUESTION", "Question Found in DB: " + c.getString(1)); 
     Question q = new Question(); 
     q.setQuestion(c.getString(1)); 
     q.setAnswer(c.getString(2)); 
     q.setOption1(c.getString(3)); 
     q.setOption2(c.getString(4)); 
     q.setOption3(c.getString(5)); 
     q.setRating(difficulty); 
     questionSet.add(q); 
    } 
    return questionSet; 
} 

}

有谁知道的可能的原因?奇怪的是,这只发生在少量的安装上,无法确定他们正在使用的SDK级别/设备使其更加困难。

任何帮助表示赞赏

编辑:由于我包括如何创建数据库的响应。

首先,我的活动都有这个(这是导致飞机坠毁的同样的方法)()

private List<Question> getQuestionSetFromDb() throws Error { 
    int diff = getDifficultySettings(); 
    int numQuestions = getNumQuestions(); 
    DBHelper myDbHelper = new DBHelper(this); 
    try { 
     myDbHelper.createDataBase(); 
    } catch (IOException ioe) { 
     throw new Error("Unable to create database"); 
    } 
    try { 
     myDbHelper.openDataBase(); 
    }catch(SQLException sqle){ 
     throw sqle; 
    } 
    List<Question> questions = myDbHelper.getQuestionSet(diff, numQuestions); 
    myDbHelper.close(); 
    return questions; 
} 

myDBhelper.createdatabase()调用

public void createDataBase() throws IOException{ 

    boolean dbExist = checkDataBase(); 
    if(!dbExist) 
    { 
     //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"); 
     } 
    } 
} 

checkDatabase

private boolean checkDataBase(){ 
    SQLiteDatabase checkDB = null; 
    try{ 
     String myPath = DB_PATH + DB_NAME; 
     checkDB = SQLiteDatabase.openDatabase(myPath, null, SQLiteDatabase.OPEN_READONLY); 
    }catch(SQLiteException e){ 
     //database does't exist yet. 
    } 
    if(checkDB != null){ 
     checkDB.close(); 
    } 

    return checkDB != null ? true : false; 
} 

copyDatabase()

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))>0){ 
     myOutput.write(buffer, 0, length); 
    } 

    //Close the streams 
    myOutput.flush(); 
    myOutput.close(); 
    myInput.close(); 

} 

变量

private static String DB_PATH = "/data/data/com.app.myapp/databases/"; 
private static String DB_NAME = "questionsDb"; 
private SQLiteDatabase myDataBase; 
private final Context myContext; 

Apoligies,我真的应该包括在这一最初

+0

您最近是否升级数据库模式以包含问题表? – hooked82

+0

不,数据库表总是相同的。自从我上传到市场后,根本没有任何数据库更改。 – PapaJon

+0

那么这个错误似乎很明显;你的桌子找不到。所以我会开始查看它创建的部分,确保它完全被创建。此外,它可能是一个想法,赶上这个具体的错误,然后创建表,如果它发生。 –

回答

0

关于丢失 “问题” 数据库中的错误报告。 DB_NAME是“questionsDb”。

整数难度,附加+到一个字符串,也让我感到困惑(已经从另一个用户在此线程中早些时候报告过)。

0

我只是觉得你创建数据库的方式导致了这个问题。没有什么问题;正如你所说的,它可以在98%的设备上工作。

在我看来,你可能最好延长SQLiteOpenHelper。如果数据库不存在,并且不需要手动执行任何检查,就会负责创建数据库。

相关问题