Getting single value from SQLite in android

I am developing my Android application for Android now, I use several tables to receive and insert data. during development and found that I get data from a table, it has only two columns STATS(_id, stat_name). What is my problem? I have activity with 10 buttons, and each button correlates with one stat_name. When users click one of the buttons, the application "goes" to the table STATSto get the correct one _id, and then enters this _idon the other table GAME_STATS(_id, PlayerId (fk), GameId(fk), StatsId(fk)(andmore))on STATS._id = GAME_STATS.StatsId, and I usually have to perform a similar operation for PlayerId.

Now I am doing this:

public String getStatId(String statName){
    String statId = "Error";
    Cursor c = mDb.query(STAT_TABLE, new String[] {AbstractDbAdapter.STAT_ID, AbstractDbAdapter.STAT_NAME}, AbstractDbAdapter.STAT_NAME+ " = " +statName, null, null, null, null);
    int count = c.getCount();
    if(count == 1){
        c.moveToFirst();
        statId = c.getString(c.getColumnIndex(AbstractDbAdapter.STAT_ID));
    }
    c.close();
    mDb.close();
    Log.d("FootballApp","StatId =" +statId);
    return statId;      
}

, , , Cursor . , , , , . 9 , , _id , , , .

- , ? :) !:)

+5
2

, . , :

public String getFromDb(String tableName, String select, String selectBy, String selectName){
    String selection = "Error";
    Cursor c = mDb.query(tableName, new String[] {select}, selectBy + "=" + selectName, null, null, null, null);
    if(c.getCount() == 1){
        c.moveToFirst();
        selection = c.getString(c.getColumnIndex(select));
    }
    c.close();
    mDb.close();
    Log.d("FootballApp", select + "=" + selection);
    return id;      
}

:

int statID = getFromDb(STAT_TABLE, AbstractDbAdapter.STAT_ID, AbstractDbAdapter.STAT_NAME, statName);
+7

, , Cursor.moveToFirst() false, , c.getCount() if(c.moveToFirst()). :)

+7

All Articles