我正在使用一个包含玩家姓名,胜利数量和损失数量的数据库表.
然后我会从主要活动,打开,更新或其他任何内容调用数据库,最后关闭.
这工作得很好.
然后我添加了其他列并继续工作.
但是,我将游戏安装到真实设备上并且无效.
我的模拟器中没有发生这个问题,因为它有点一次又一次地覆盖现有数据库.
我决定从模拟器中取消应用程序,然后停止工作.看起来我的游戏在首次启动时创建新数据库时遇到了一些麻烦.
在做了一些测试之后,我得到了不同的错误,比如"not null constrain"或"CursorIndexOutOfBounds".
因此,我可以说问题在于数据库的创建 - 但我不知道出了什么问题.
我不认为错误是在主Activity中,因为我在那里总是做同样的事以前它曾经工作,但我已经改变了数据库.
也许问题是我在onCreate()
函数中初始化值的地方,就像它没有真正给数据库赋值?
无论如何,也许我一直在关注错误的观点,所以我认为新的观点可能有所帮助.
这里有数据库类:
public class DatabaseAdapter { private final Context context; public static final String C_COLUMNA_ID = "_id"; public static final String C_COLUMNA_NAME = "menu_player"; public static final String C_COLUMNA_VICTORY = "menu_victory"; public static final String C_COLUMNA_LOSS = "menu_loss"; public static final String C_COLUMNA_POINTS = "menu_points"; public static final String C_COLUMNA_UNLOCKED = "menu_unlocked"; public static final String C_COLUMNA_LEVEL = "menu_level"; private static final String NAME_TABLE = "menu"; private static final String DATABASE_NAME = "data"; private static final String TAG = "DBAdapter"; private String INSERT_OR_UPDATE_RECORD; private DatabaseHelper DatabaseHelper; private SQLiteDatabase db; public DatabaseAdapter(Context context){ this.context = context; DatabaseHelper = new DatabaseHelper(context); } private static class DatabaseHelper extends SQLiteOpenHelper{ private static final int DATABASE_VERSION = 1; public DatabaseHelper(Context context){ super(context, DATABASE_NAME, null, DATABASE_VERSION); } @Override public void onCreate(SQLiteDatabase database){ database.execSQL( "CREATE TABLE MENU(" + " _id INTEGER PRIMARY KEY," + " menu_player TEXT NOT NULL, " + " menu_victory INTEGER, " + " menu_loss INTEGER," + " menu_points INTEGER," + " menu_unlocked INTEGER," + " menu_level INTEGER)"); //initialize database.execSQL("INSERT INTO MENU(_id, menu_player) VALUES(1,'Pla Yer Uan')"); database.execSQL("INSERT INTO MENU(_id, menu_victory) VALUES(1,0)"); database.execSQL("INSERT INTO MENU(_id, menu_loss) VALUES(1,0)"); database.execSQL("INSERT INTO MENU(_id, menu_points) VALUES(1,0)"); database.execSQL("INSERT INTO MENU(_id, menu_unlocked) VALUES(1,0)"); database.execSQL("INSERT INTO MENU(_id, menu_level) VALUES(1,1)"); } @Override public void onUpgrade(SQLiteDatabase database, int oldVersion, int newVersion){ database.execSQL("DROP TABLE IF EXISTS todo"); onCreate(database); } } public DatabaseAdapter open() throws SQLException{ db = DatabaseHelper.getWritableDatabase(); return this; } public void close(){ DatabaseHelper.close(); } public boolean updateRecord(long rowId, String columna, String newPlayerName, int value){ ContentValues args = new ContentValues(); switch (columna){ case "nombre": args.put(C_COLUMNA_NOMBRE, newPlayerName); break; case "victory": args.put(C_COLUMNA_VICTORY,value); break; case "loss": args.put(C_COLUMNA_LOSS,value); break; case "royals": args.put(C_COLUMNA_POINTS,value); break; case "unlocked": args.put(C_COLUMNA_UNLOCKED,value); break; case "level": args.put(C_COLUMNA_LEVEL,value); break; } return db.update(NAME_TABLE,args,C_COLUMNA_ID + "=" + rowId,null)>0; } public void backToDefault(){ ContentValues args = new ContentValues(); args.put(C_COLUMNA_NAME, "Pla Yer Uan"); args.put(C_COLUMNA_VICTORIAS,0); args.put(C_COLUMNA_DERROTAS,0); args.put(C_COLUMNA_ROYALS,0); args.put(C_COLUMNA_UNLOCKED,0); args.put(C_COLUMNA_LEVEL,1); db.update(NAME_TABLE,args,C_COLUMNA_ID + "=" + 1,null); } public Cursor getRecord(long rowId, String columna) throws SQLException{ Cursor cursor = db.query(true, NAME_TABLE, new String[]{ C_COLUMNA_ID, columna }, C_COLUMNA_ID + "=" + rowId, null, null, null, null, null); if (cursor != null){ cursor.moveToFirst(); } return cursor; } }
得到此错误:
android.database.CursorIndexOutOfBoundsException: Index 0 requested, with a size of 0
Fantômas.. 6
这个
//initialize database.execSQL("INSERT INTO MENU(_id, menu_player) VALUES(1,'Pla Yer Uan')"); database.execSQL("INSERT INTO MENU(_id, menu_victory) VALUES(1,0)"); database.execSQL("INSERT INTO MENU(_id, menu_loss) VALUES(1,0)"); database.execSQL("INSERT INTO MENU(_id, menu_points) VALUES(1,0)"); database.execSQL("INSERT INTO MENU(_id, menu_unlocked) VALUES(1,0)"); database.execSQL("INSERT INTO MENU(_id, menu_level) VALUES(1,1)");
真的错了
您必须一次插入一个完整的行,如下所示:
//initialize database.execSQL("INSERT INTO MENU(menu_player, menu_victory, menu_loss, menu_points, menu_unlocked, menu_level) VALUES('Pla Yer Uan', 0, 0, 0, 0, 1)");
另请注意,您不必插入_id
字段,因为它是自动增量主键
这个
//initialize database.execSQL("INSERT INTO MENU(_id, menu_player) VALUES(1,'Pla Yer Uan')"); database.execSQL("INSERT INTO MENU(_id, menu_victory) VALUES(1,0)"); database.execSQL("INSERT INTO MENU(_id, menu_loss) VALUES(1,0)"); database.execSQL("INSERT INTO MENU(_id, menu_points) VALUES(1,0)"); database.execSQL("INSERT INTO MENU(_id, menu_unlocked) VALUES(1,0)"); database.execSQL("INSERT INTO MENU(_id, menu_level) VALUES(1,1)");
真的错了
您必须一次插入一个完整的行,如下所示:
//initialize database.execSQL("INSERT INTO MENU(menu_player, menu_victory, menu_loss, menu_points, menu_unlocked, menu_level) VALUES('Pla Yer Uan', 0, 0, 0, 0, 1)");
另请注意,您不必插入_id
字段,因为它是自动增量主键