dimanche 20 décembre 2015

Unable to start/create SQLite database in android

The thing is, I was working with a database with a player's name, number of victories and number of losses. Then I would call the database from the main activity, open, update or whatever and finally, close. And that was working just fine.

Then I added the other columns and kept working, however I installed the game in a real device and it didn't work. That problem wasn't happening in my emulator as it was kinda overwritting the existing database again and again. I decided to unistall the app from the emulator and then stopped working too. Looked like my game had found some trouble in creating a new database when first started.

After doing some tests I got different errors like "not null constrain" or "CursorIndexOutOfBounds". Therefore I could say that the problem is in the creation of the database but I have no idea of what's wrong. I don't think that the error is in the main activity as I've done always the same there and worked before but I've changed the database.

Maybe the problem is where I initialize the values in the onCreate function, like it's not really giving values to the database?

Anyway, maybe I've been focusing on the wrong point so I thought that new perspectives might help, here you have the database class...Thanks!

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

}

Aucun commentaire:

Enregistrer un commentaire