SQLiteException - 只是发生在某些设备 [英] SQLiteException - only happens on some devices

查看:288
本文介绍了SQLiteException - 只是发生在某些设备的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我最近发布的应用到市场。从开发者控制台,似乎我的用户约1-2%的有这个问题。 1-2%虽小,但人们更倾向于留下评论当事情不工作,而不是当它可能下载负面影响。

I recently published an app to the market place. From the developers console it seems about 1-2% of my users are having this issue. 1-2% is small but people are more inclined to leave comments when something doesn't work rather than when it does which could negatively effect downloads.

不幸的是,开发者控制台仅列出平台作为'其他',但我的应用程序是提供给那些与SDK 1.6或更高版本。我也无法重新创建这个问题,并没有任何用户直接与我联系,所以我无法得到关于它的失败对设备的任何详细信息。

Unfortunately the developers console only lists the platform as 'other', but my app is available to those with SDK 1.6+. I'm also unable to recreate this issue and no user has contacted me directly so I'm unable to get any more information about the devices it fails on.

下面是堆栈

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)是指

getQuestionSetFromDb(SplashActivity.java:109) refers to

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水平/设备。

Does anyone know of a probable cause? It's strange this is only happening with a small amount of installs and being unable to determine the SDK level/device they are using makes it more difficult.

任何帮助是pciated AP $ P $

Any help is appreciated

编辑:由于响应我,包括数据库的创建方式。

Due to the responses I'm including how the db is created.

首先,我的活动有这个(这是导致崩溃的同样的方法)

First, my activity has this (this is the same method which causes the crash)

    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()调用

myDBhelper.createdatabase() calls

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()

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()

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,我真的应该最初包括本

Apoligies, I really should have included this initially

推荐答案

我只是觉得你是创建数据库的方式是造成问题的原因。没有什么不妥之处;正如你所说的它的工作原理上的设备的98%。

I just feel the way you are creating the database is causing the problem. There is nothing wrong with it; as you said it works on 98% of the devices.

在我看来,你可能会更好过延长 SQLiteOpenHelper 。该负责创建数据库,如果不存在,您不必手动做任何检查,你做到这一点。

In my opinion, you are probably better off extending SQLiteOpenHelper. This takes care of creating your database if it doesn't exist and you don't have to do any checks manually that you do.

这篇关于SQLiteException - 只是发生在某些设备的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆