1

我已经使用 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): 致命异常: main 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): 在 java.lang.reflect.Method.invokeNative(Native Method) 12-05 19:08:57.158: E/AndroidRuntime (13825): 在 java.lang.reflect.Method.invoke(Method.java:511) 12-05 19:08:57.158: E/AndroidRuntime(13825): 在 com.android.internal.os.ZygoteInit$MethodAndArgsCaller。运行(ZygoteInit.java:790)12-05 19:08:57.158:E/AndroidRuntime(13825):在 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): 在 java.lang.reflect.Method.invokeNative(Native Method) 12-05 19:08:57.158: E/AndroidRuntime(13825): 在 java.lang.reflect。方法。调用(Method.java:511) 12-05 19:08:57.158: E/AndroidRuntime(13825): at android.view.View$1.onClick(View.java:3064) 12-05 19:08:57.158: E /AndroidRuntime(13825): ... 11 more 12-05 19:08:57.158: E/AndroidRuntime(13825): Caused by: android.database.sqlite.SQLiteException: no such table: states: , while compile: SELECT 播放,五,一,二,第一 FROM states 12-05 19:08:57.158: E/AndroidRuntime(13825): at android.database.sqlite.SQLiteCompiledSql.native_compile(Native Method) 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): 在 com.kt.banoge.karodpati.Datak.updateRow(Datak.java:172) 12-05 19:08:57.158: E/AndroidRuntime(13825): 在 com.kt .banoge.karodpati.MainActivity.change(MainActivity.java:66) 12-05 19:08:57.158: E/AndroidRuntime(13825): ... 14 更多 }66) 12-05 19:08:57.158: E/AndroidRuntime(13825): ... 14 更多}66) 12-05 19:08:57.158: E/AndroidRuntime(13825): ... 14 更多}

4

1 回答 1

0

你能替换 db.execSQL("DROP TABLE IF EXISTS title"); by db.execSQL("如果存在则删除表" + DATABASE_TABLE);

这可能会有所帮助;)

哔叽

于 2012-12-03T15:36:34.620 回答