Ok so I'm really confused at the moment. I'm trying to add a new table to my program, but whenever I do I crashes on my, specifically at the onCreate method. I cannot for the love of me figure out why. Any suggestions?
Also this is an error I get, but I can't find categories.db anywhere
Caused by: android.database.sqlite.SQLiteException: no such table: categories.db (code 1): , while compiling: 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;
}
}
}
更改您的CREATE TABLE字符串以将IF NOT EXISTS
包括在内,
CREATE TABLE IF NOT EXISTS Table_name(Columns)
Delete your application from the device or emulator, and INSTALL/RUN again, and it should work.
I had the same problem, and I simple delete the application from device and it worked.
Try this..
While creating the second table, you had "INTEGER" + "PRIMARY KEY AUTOINCREMENT"..
I made that in one.
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)";
You should call 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);
}
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.