gpt4 book ai didi

java - 尝试添加第二个表时应用程序崩溃

转载 作者:行者123 更新时间:2023-12-01 04:22:16 25 4
gpt4 key购买 nike

好吧,我现在真的很困惑。我试图向我的程序添加一个新表,但每当我这样做时,我都会崩溃,特别是在 onCreate 方法中。我无法弄清楚为什么。有什么建议吗?

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

引起:android.database.sqlite.SQLiteException:没有这样的表:categories.db(代码1):,编译时:SELECT * FROMcategories.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;
}
}
}

最佳答案

试试这个..

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

我把它做成了一个。

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);
}

关于java - 尝试添加第二个表时应用程序崩溃,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/18796645/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com