1

好的,所以我现在真的很困惑。我正在尝试向我的程序添加一个新表,但每当我这样做时,我都会崩溃,特别是在 onCreate 方法中。我不知道为什么。有什么建议么?

这也是我得到的一个错误,但我在任何地方都找不到 categories.db

引起:android.database.sqlite.SQLiteException:没有这样的表:categories.db(代码1):,编译时:SELECT * FROM categories.db

private final static int DB_VERSION = 2;

public static String TABLE_NAME = "players";
public static String DB_NAME = "playerslist.db";
public static String SCORE = "score";
public static String ID = "_id";
public static String PLAYER_NAME = "name";

public static String CATEGORY_TABLE_NAME = "categories";
public static String CATEGORY_NAME = "category_name";
public static String CATEGORY_VALUE = "category_value";
public static String CATEGORY_ID = "category_id";
public static String CATEGORY_PLAYER_ID = "category_player_id";

public static String CREATE_PLAYER_TABLE = "CREATE TABLE "+TABLE_NAME+" ("+ID+" INTEGER PRIMARY KEY AUTOINCREMENT, "+PLAYER_NAME+" TEXT, "+
        SCORE+" TEXT)";
public static String CREATE_CATEGORY_TABLE = "CREATE TABLE "+CATEGORY_TABLE_NAME+"("+CATEGORY_ID+" INTEGER" +
        "PRIMARY KEY AUTOINCREMENT, "+CATEGORY_NAME+" TEXT, "+CATEGORY_VALUE+" INTEGER, "
        +CATEGORY_PLAYER_ID+" INTEGER)";

public SQLHelp(Context context) {
    super(context, DB_NAME, null, 2);
}

@Override
public void onCreate(SQLiteDatabase db) {
    db.execSQL(CREATE_PLAYER_TABLE);
    db.execSQL(CREATE_CATEGORY_TABLE);
}

@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    for(int i = oldVersion; i < newVersion; i++){
        switch(i){
            case 1:
                db.execSQL(CREATE_CATEGORY_TABLE);
            break;
        }
    }
}
4

3 回答 3

0

尝试这个..

在创建第二个表时,您有“INTEGER”+“PRIMARY KEY AUTOINCREMENT”..

我把它做成了一个。

public static String CREATE_CATEGORY_TABLE = "CREATE TABLE "+CATEGORY_TABLE_NAME+"("+CATEGORY_ID+ " INTEGER PRIMARY KEY AUTOINCREMENT, "+CATEGORY_NAME+" TEXT, "+CATEGORY_VALUE+" INTEGER, "
    +CATEGORY_PLAYER_ID+" INTEGER)";

你应该调用 SQLiteOpenHelper

private static class DatabaseHelper extends SQLiteOpenHelper{
    DatabaseHelper(Context context){
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }
    public void onCreate(SQLiteDatabase db){
          db.execSQL(CREATE_PLAYER_TABLE);
          db.execSQL(CREATE_CATEGORY_TABLE);
    }
    public void onUpgrade(SQLiteDatabase db, int arg1, int arg2{
          db.execSQL("DROP TABLE IF EXISTS " + CREATE_PLAYER_TABLE);
          db.execSQL("DROP TABLE IF EXISTS " + CREATE_CATEGORY_TABLE);
          onCreate(db);
    }
于 2013-09-14T23:57:38.267 回答
0

更改您的 CREATE TABLE 字符串以包含IF NOT EXISTS为,
CREATE TABLE IF NOT EXISTS Table_name(Columns)

于 2013-09-14T02:05:30.713 回答
0

从设备或模拟器中删除您的应用程序,然后再次安装/运行,它应该可以工作。

我遇到了同样的问题,我简单地从设备中删除了应用程序,它就可以工作了。

于 2013-09-14T03:59:55.157 回答