Android SQLite数据库表,仅在某些设备上没有此类表错误

时间:2014-05-25 17:54:13

标签: java android database eclipse sqlite

我搜索了档案,试图找到解决问题的办法,但似乎都没有关系。自从我的初始应用程序启动以来,我已经对我的数据库进行了一些升级,包括添加了2个新表。在我最近的发布后,我收到了多个错误报告,说明没有这样的表格#34;存在,当我知道它确实存在时。我无法在我的任何个人设备上重新创建报告的错误,我不确定我能改变什么。 这是我收到的错误的副本以及创建和升级db表的DB Helper类的副本。

java.lang.RuntimeException: Unable to start activity ComponentInfo{com.skylandersdb/com.skylandersdb.CharacterDetailActivity}: android.database.sqlite.SQLiteException: no such table: tblCollectorGroups (code 1): , while compiling: SELECT collectName FROM tblCollectorGroups
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2305)
at android.app.ActivityThread.handleLaunchActivity(ActivityThread.java:2363)
at android.app.ActivityThread.access$900(ActivityThread.java:161)
at android.app.ActivityThread$H.handleMessage(ActivityThread.java:1265)
at android.os.Handler.dispatchMessage(Handler.java:102)
at android.os.Looper.loop(Looper.java:157)
at android.app.ActivityThread.main(ActivityThread.java:5356)
at java.lang.reflect.Method.invokeNative(Native Method)
at java.lang.reflect.Method.invoke(Method.java:515)
at com.android.internal.os.ZygoteInit$MethodAndArgsCaller.run(ZygoteInit.java:1265)
at com.android.internal.os.ZygoteInit.main(ZygoteInit.java:1081)
at dalvik.system.NativeStart.main(Native Method)
Caused by: android.database.sqlite.SQLiteException: no such table: tblCollectorGroups (code 1): , while compiling: SELECT collectName FROM tblCollectorGroups
at android.database.sqlite.SQLiteConnection.nativePrepareStatement(Native Method)
at android.database.sqlite.SQLiteConnection.acquirePreparedStatement(SQLiteConnection.java:1113)
at android.database.sqlite.SQLiteConnection.prepare(SQLiteConnection.java:690)
at android.database.sqlite.SQLiteSession.prepare(SQLiteSession.java:588)
at android.database.sqlite.SQLiteProgram.<init>(SQLiteProgram.java:58)
at android.database.sqlite.SQLiteQuery.<init>(SQLiteQuery.java:37)
at android.database.sqlite.SQLiteDirectCursorDriver.query(SQLiteDirectCursorDriver.java:44)
at android.database.sqlite.SQLiteDatabase.rawQueryWithFactory(SQLiteDatabase.java:1430)
at android.database.sqlite.SQLiteDatabase.rawQuery(SQLiteDatabase.java:1369)
at com.skylandersdb.CharacterDetailActivity.getListCollectorGroup(CharacterDetailActivity.java:925)
at com.skylandersdb.CharacterDetailActivity.loadCollectorGroup(CharacterDetailActivity.java:908)
at com.skylandersdb.CharacterDetailActivity.onCreate(CharacterDetailActivity.java:400)
at android.app.Activity.performCreate(Activity.java:5426)
at android.app.Instrumentation.callActivityOnCreate(Instrumentation.java:1105)
at android.app.ActivityThread.performLaunchActivity(ActivityThread.java:2269)
... 11 more`

这是我创建和升级数据库的DBHelper类

   import android.content.Context;
   import android.database.sqlite.SQLiteDatabase;
   import android.database.sqlite.SQLiteOpenHelper;
   import android.provider.BaseColumns;
   import android.util.Log;

    public class DBHelper_Skylanders extends SQLiteOpenHelper {

private static final String TAG = DBHelper_Skylanders.class.getSimpleName();

public static final String DB_NAME = "skylandersDB";
public static final int DB_VERSION = 6;

public static final String TABLE_skylanders = "tblSkylanders";
public static final String C_ID = BaseColumns._ID;
public static final String C_skyid = "skyid";
public static final String C_depth = "depth";
public static final String C_preName = "preName";
public static final String C_mainName = "mainName";
public static final String C_descName = "descName";
public static final String C_element = "element";
public static final String C_figureType = "figureType";
public static final String C_pic = "pic";
public static final String C_primePower = "primePower";
public static final String C_noOfFigures = "noOfFigures";
public static final String C_game = "game";
public static final String C_attackStat = "attackStat";
public static final String C_attackBase = "attackBase";
public static final String C_agilityStat = "agilityStat";
public static final String C_agilityBase = "agilityBase";
public static final String C_defenseStat = "defenseStat";
public static final String C_defenseBase = "defenseBase";
public static final String C_luckStat = "luckStat";
public static final String C_luckBase = "luckBase";
public static final String C_origin = "origin";
public static final String C_phrase = "phrase";

public static final String TABLE_cGroups = "tblCollectorGroups";
public static final String C_cGroupName = "collectName";
public static final String C_cGroup1 = "collection1";
public static final String C_cGroup2 = "collection2";
public static final String C_cGroup3 = "collection3";

public static final String TABLE_collect = "tblCollection";
public static final String C_skylanderid = "skylanderID";
public static final String C_collectid = "collectid";
public static final String C_collectName = "collectName";
public static final String C_collection = "collection";

public String sql = null;

public final Context myContext;

public DBHelper_Skylanders(Context context) {
    super(context, DB_NAME, null, DB_VERSION);
    this.myContext = context;
}


@Override
public void onCreate(SQLiteDatabase db) {
    sql = String.format("create table %s (%s INTEGER PRIMARY KEY AUTOINCREMENT, %s INTEGER, %s INTEGER, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s INTEGER,  %s INTEGER, %s INTEGER, %s TEXT, %s INTEGER, %s INTEGER, %s INTEGER, %s INTEGER, %s INTEGER, %s INTEGER, %s INTEGER, %s INTEGER, %s TEXT, %s TEXT, %s TEXT)", 
            TABLE_skylanders, C_ID, C_skyid, C_depth, C_preName, C_mainName, C_descName, C_element, C_figureType, C_pic, C_primePower, C_noOfFigures, C_game, C_attackStat, C_attackBase, C_agilityStat, C_agilityBase, C_defenseStat, C_defenseBase, C_luckStat, C_luckBase, C_origin, C_phrase, C_collection);
    //test script
    Log.d(TAG, "onCreate sql: "+sql);
    //executes SQL statement
    db.execSQL(sql);
    sql = null;

    sql = String.format("create table %s (%s INTEGER PRIMARY KEY AUTOINCREMENT, %s TEXT, %s TEXT, %s TEXT, %s TEXT)", 
            TABLE_cGroups, C_ID, C_cGroupName, C_cGroup1, C_cGroup2, C_cGroup3);
    //test script
    Log.d(TAG, "onCreate sql: "+sql);
    //executes SQL statement
    db.execSQL(sql);
    sql = null;

    sql = String.format("create table %s (%s INTEGER PRIMARY KEY AUTOINCREMENT, %s INTEGER, %s INTEGER, %s TEXT, %s TEXT)", 
            TABLE_collect, C_ID, C_skylanderid, C_collectid, C_collectName, C_collection);
    //test script
    Log.d(TAG, "onCreate sql: "+sql);
    //executes SQL statement
    db.execSQL(sql);
    sql = null;
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {

    switch(oldVersion)
    {
        case 3:
            db.execSQL("drop table if exists " + TABLE_skylanders);
            Log.d(TAG, "onUpdate dropped table " + TABLE_skylanders);

            db.execSQL("drop table if exists " + TABLE_cGroups);
            Log.d(TAG, "onUpdate dropped table " + TABLE_cGroups);

            db.execSQL("drop table if exists " + TABLE_collect);
            Log.d(TAG, "onUpdate dropped table " + TABLE_collect);

            onCreate(db);

        case 4:
            //only drops tblSkylander
            db.execSQL("drop table if exists " + TABLE_skylanders);
            Log.d(TAG, "onUpdate dropped table " + TABLE_skylanders);

            //only recreates tblSkylander
            sql = String.format("create table %s (%s INTEGER PRIMARY KEY AUTOINCREMENT, %s INTEGER, %s INTEGER, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s INTEGER,  %s INTEGER, %s INTEGER, %s TEXT, %s INTEGER, %s INTEGER, %s INTEGER, %s INTEGER, %s INTEGER, %s INTEGER, %s INTEGER, %s INTEGER, %s TEXT, %s TEXT, %s TEXT)", 
                    TABLE_skylanders, C_ID, C_skyid, C_depth, C_preName, C_mainName, C_descName, C_element, C_figureType, C_pic, C_primePower, C_noOfFigures, C_game, C_attackStat, C_attackBase, C_agilityStat, C_agilityBase, C_defenseStat, C_defenseBase, C_luckStat, C_luckBase, C_origin, C_phrase, C_collection);
            Log.d(TAG, "onCreate sql: "+sql);
            db.execSQL(sql);

            sql = String.format("create table if not exists %s (%s INTEGER PRIMARY KEY AUTOINCREMENT, %s TEXT, %s TEXT, %s TEXT, %s TEXT)", 
                    TABLE_cGroups, C_ID, C_cGroupName, C_cGroup1, C_cGroup2, C_cGroup3);
            //test script
            Log.d(TAG, "onCreate sql: "+sql);
            //executes SQL statement
            db.execSQL(sql);

            sql = String.format("create table if not exists %s (%s INTEGER PRIMARY KEY AUTOINCREMENT, %s INTEGER, %s INTEGER, %s TEXT, %s TEXT)", 
                    TABLE_collect, C_ID, C_skylanderid, C_collectid, C_collectName, C_collection);
            //test script
            Log.d(TAG, "onCreate sql: "+sql);
            //executes SQL statement
            db.execSQL(sql);

        case 5:
            //only drops tblSkylander
            db.execSQL("drop table if exists " + TABLE_skylanders);
            Log.d(TAG, "onUpdate dropped table " + TABLE_skylanders);

            //only recreates tblSkylander
            sql = String.format("create table %s (%s INTEGER PRIMARY KEY AUTOINCREMENT, %s INTEGER, %s INTEGER, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s TEXT, %s INTEGER,  %s INTEGER, %s INTEGER, %s TEXT, %s INTEGER, %s INTEGER, %s INTEGER, %s INTEGER, %s INTEGER, %s INTEGER, %s INTEGER, %s INTEGER, %s TEXT, %s TEXT, %s TEXT)", 
                    TABLE_skylanders, C_ID, C_skyid, C_depth, C_preName, C_mainName, C_descName, C_element, C_figureType, C_pic, C_primePower, C_noOfFigures, C_game, C_attackStat, C_attackBase, C_agilityStat, C_agilityBase, C_defenseStat, C_defenseBase, C_luckStat, C_luckBase, C_origin, C_phrase, C_collection);
            Log.d(TAG, "onCreate sql: "+sql);
            db.execSQL(sql);

            sql = String.format("create table if not exists %s (%s INTEGER PRIMARY KEY AUTOINCREMENT, %s TEXT, %s TEXT, %s TEXT, %s TEXT)", 
                    TABLE_cGroups, C_ID, C_cGroupName, C_cGroup1, C_cGroup2, C_cGroup3);
            Log.d(TAG, "onCreate sql: "+sql);
            db.execSQL(sql);

            sql = String.format("create table if not exists %s (%s INTEGER PRIMARY KEY AUTOINCREMENT, %s INTEGER, %s INTEGER, %s TEXT, %s TEXT)", 
                    TABLE_collect, C_ID, C_skylanderid, C_collectid, C_collectName, C_collection);
            Log.d(TAG, "onCreate sql: "+sql);
            db.execSQL(sql);
    }   

}

}

0 个答案:

没有答案