应用程序数据库相关活动在模拟器中工作但不在设备中工作

时间:2012-12-03 15:23:59

标签: android sqlite database-connection

我使用Android(api 15)创建了一个Android应用程序,android:minSdkVersion是“8”。我正在使用4.1模拟器来运行此应用程序。它在模拟器中工作正常,并在具有api 4.1的设备中成功安装...但是当使用我的datak类(数据库连接文件)启动活动时,应用程序将停止工作。

该应用程序是一个测验,其中有四个问题和一个可能的答案。数据库名称为“KBC”,当我按下游戏时检索...但在设备上没有检索到数据库。我认为设备找不到我的数据库文件“KBC”位置,其中包含我在模拟器中推送的所有表格和问题...

在虚拟设备文件资源管理器中,它在“data / data / android.kt.banoge.karodpati / databases”位置显示数据库kbc

我的datak课程的代码包含在下面。我已经看过很多其他问题,但它们没有帮助:

有人可以给我一个解决这个问题的理由或解决方案吗?

public class Datak 
{
public static final String QUE = "question";
public static final String A = "optionA";
public static final String B = "optionB";
public static final String C = "optionC";
public static final String D = "optionD";
public static final String ANS = "answer";
public static final String LEV = "level";

private static final String TAG = "DBAdapter";

public static final String played = "played";
public static final String five = "five";
public static final String one = "one";
public static final String second = "second";
public static final String first = "first";


private static final String DATABASE_NAME = "KBC";
private static final String DATABASE_TABLE = "main";
private static final String DATABASE_TABLE2 = "main2";
private static final String DATABASE_TABLE3 = "main3";
private static final int DATABASE_VERSION = 1;

private static final String DATABASE_CREATE =
    "create table "+ DATABASE_TABLE +"("
    + QUE + " text not null,"
    + A + " text not null, " 
    + B + " text not null,"
    + C + " text not null,"
    + D + " text not null," 
    + ANS + " text not null,"
    + LEV + " text not null); ";

private final Context context; 

private DatabaseHelper DBHelper;

private SQLiteDatabase db;

public Datak(Context ctx) 
{
    this.context = ctx;
    DBHelper = new DatabaseHelper(context);
}

private static class DatabaseHelper extends SQLiteOpenHelper 
{
    DatabaseHelper(Context context) 
    {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) 
    {
        db.execSQL(DATABASE_CREATE);

    }

    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion, 
    int newVersion) 
    {
        Log.w(TAG, "Upgrading database from version " + oldVersion 
                + " to "
                + newVersion + ", which will destroy all old data");
        db.execSQL("DROP TABLE IF EXISTS titles");
        onCreate(db);
    }
}    

public Datak open() throws SQLException 
{
    db = DBHelper.getWritableDatabase();
    return this;
}


public void close() 
{
    DBHelper.close();
}

public Cursor getValue()
{
     return db.query(DATABASE_TABLE, new String[] {
            QUE, 
            A,
            B,
            C,
            D,
            ANS,LEV
            },
            null,
            null,
            null,
            null,
            null,null
             );


}
public Cursor getValue2()
{
     return db.query(DATABASE_TABLE2, new String[] {
            QUE, 
            A,
            B,
            C,
            D,
            ANS,LEV
            },
            null,
            null,
            null,
            null,
            null,null
             );


}
public Cursor getValue3()
{
     return db.query(DATABASE_TABLE3, new String[] {
            QUE, 
            A,
            B,
            C,
            D,
            ANS,LEV
            },
            null,
            null,
            null,
            null,
            null,null
             );


}
public Cursor getState()
{
     return db.query("states", new String[] {
                played, 
                five,
                one,
                second,
                first
                },
                null,null,null,null,null);


}

public void updateRow(int a) 
{
    final Cursor c1;
    int pl;
 ContentValues CV=new ContentValues();
 c1=db.query("states", new String[] {
        played, 
        five,
        one,
        second,
        first
        },
        null,null,null,null,null);
    c1.moveToPosition(0);

    if(a==1)
    {
        pl=c1.getInt(0);
        pl++;
        CV.put(played, pl); 
    }
    if(a==2)
    {
        pl=c1.getInt(1);
        pl++;
        CV.put(five, pl);
    }
    if(a==3)
    {
        pl=c1.getInt(2);
        pl++;
        CV.put(one, pl);    
    }
    if(a==4)
    {
        pl=c1.getInt(3);
        pl++;
        CV.put(second, pl); 
    }
    if(a==5)
    {
        pl=c1.getInt(4);
        pl++;
        CV.put(first, pl);  
    }

    db.update("states", CV, null, null);
    c1.close();

}

12-05 19:08:57.158:E / AndroidRuntime(13825):致命异常:主要 12-05 19:08:57.158:E / AndroidRuntime(13825):java.lang.IllegalStateException:无法执行活动的方法 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.view.View $ 1.onClick(View.java:3069) 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.view.View.performClick(View.java:3591) 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.view.View $ PerformClick.run(View.java:14263) 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.os.Handler.handleCallback(Handler.java:605) 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.os.Handler.dispatchMessage(Handler.java:92) 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.os.Looper.loop(Looper.java:137) 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.app.ActivityThread.main(ActivityThread.java:4507) 12-05 19:08:57.158:E / AndroidRuntime(13825):at java.lang.reflect.Method.invokeNative(Native Method) 12-05 19:08:57.158:E / AndroidRuntime(13825):at java.lang.reflect.Method.invoke(Method.java:511) 12-05 19:08:57.158:E / AndroidRuntime(13825):at com.android.internal.os.ZygoteInit $ MethodAndArgsCaller.run(ZygoteInit.java:790) 12-05 19:08:57.158:E / AndroidRuntime(13825):at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:557) 12-05 19:08:57.158:E / AndroidRuntime(13825):at dalvik.system.NativeStart.main(Native Method) 12-05 19:08:57.158:E / AndroidRuntime(13825):引起:java.lang.reflect.InvocationTargetException 12-05 19:08:57.158:E / AndroidRuntime(13825):at java.lang.reflect.Method.invokeNative(Native Method) 12-05 19:08:57.158:E / AndroidRuntime(13825):at java.lang.reflect.Method.invoke(Method.java:511) 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.view.View $ 1.onClick(View.java:3064) 12-05 19:08:57.158:E / AndroidRuntime(13825):... 11更多 12-05 19:08:57.158:E / AndroidRuntime(13825):引起:android.database.sqlite.SQLiteException:没有这样的表:states :,编译时:SELECT播放,五,一,二,第一个FROM状态 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.database.sqlite.SQLiteCompiledSql.native_compile(Native方法) 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.database.sqlite.SQLiteCompiledSql。(SQLiteCompiledSql.java:68) 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.database.sqlite.SQLiteProgram.compileSql(SQLiteProgram.java:143) 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.database.sqlite.SQLiteProgram.compileAndbindAllArgs(SQLiteProgram.java:361) 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:127) 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.database.sqlite.SQLiteProgram。(SQLiteProgram.java:94) 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.database.sqlite.SQLiteQuery。(SQLiteQuery.java:53) 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:47) 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1690) 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.database.sqlite.SQLiteDatabase.queryWithFactory(SQLiteDatabase.java:1575) 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1531) 12-05 19:08:57.158:E / AndroidRuntime(13825):在android.database.sqlite.SQLiteDatabase.query(SQLiteDatabase.java:1611) 12-05 19:08:57.158:E / AndroidRuntime(13825):at com.kt.banoge.karodpati.Datak.updateRow(Datak.java:172) 12-05 19:08:57.158:E / AndroidRuntime(13825):at com.kt.banoge.karodpati.MainActivity.change(MainActivity.java:66) 12-05 19:08:57.158:E / AndroidRuntime(13825):... 14更多     }

1 个答案:

答案 0 :(得分:0)

你可以替换db.execSQL(“DROP TABLE IF EXISTS titles”); 通过db.execSQL(“DROP TABLE IF EXISTS”+ DATABASE_TABLE);

这可能会有所帮助;)

塞尔