开发者

Android: loading from SQLite Database

So I'm still building a Database to support a project of mine. There are two different things to be saved: first, attribute values of some player objects and second, simple values stored in a java class.

ATM my problem lies in the process of loading values of a player object and writing it in the respective class.

Now let's see some code:

Following you see the method I want to use for saving the values in the database. That works fine atm, but I just realized I'm still passing the contentValues object an extra value for the 'ID' , which I did set - and planned to keep that way - as autoincrement. Any Idea how to work this in accordingly?

public void savePlayer(Player player[]) {


    for (int i = 0; i <= 3; i++) {


     playerValues.put("ID", i);
     playerValues.put("Name", player[i].getName());
     playerValues.put("HP", player[i].getHp());
     playerValues.put("Satisfaction", player[i].getsatisfaction());
     playerValues.put("Hygiene", player[i].isHygieneInt());
     playerValues.put("IsAlive", player[i].isAliveInt());

     }
     db.insert("playertable", null, playerValues);
}

Okay, hold on to your hats because this 开发者_如何转开发might look a bit like spaghetti - the load-method:

public void loadPlayer() {
    String[] namecolumn = { "Name" };
    String[] intcolumn = { "ID, HP, Satisfaction, Hygiene, IsAlive" };
    String[] namesToString = new String[4];


    for (int j = 0; j <= 3; j++) {
        Cursor playerCursorName = db.query("playertable", namecolumn, "ID="
                + j, null, null, null, null);
        namesToString = cursorToString(playerCursorName);
        Resource.playerArray[j].setName(namesToString[j]);
    }

    for (int i = 0; i < 3; i++) {
        int[] restToInt;
        Cursor playerCursorInt = db.query("playertable", intcolumn, "ID="
                + i, null, null, null, null);
        restToInt = cursorToInt(playerCursorInt, 4);
        Resource.playerArray[i].setHp(restToInt[i]);
        Resource.playerArray[i].setsatisfaction(restToInt[i]);
        Resource.playerArray[i].setHygieneInt(restToInt[i]);
        Resource.playerArray[i].setAliveInt(restToInt[i]);

    }
}

Yeah, I know this looks pretty ugly but let me explain it:

Because there are 4 player objects I planned on iterating through the database entries by using the ID as identifier to get exactly one row at a time and writing the name and the other values of this object in the java class where I want to manage them within my project.

Note: same problem with autoincrement here than in the save method

In addition, I get a CursorIndexOutOfBoundsException when calling loadPlayer because Index -1 is being requested - isn't that the result of an operation on the database resulting in an error?

Yeah that's pretty much it, I'll provide you with additional code if requested, hope someone can help me


You are using Cursors in a slightly odd way here.

The point of a Cursor is to ask SQLite to do the hard work of fetching data for you, and your job is simply to use the cursor to iterate through the returned values.

Firstly, I would change the query here to ask for all values in the table (and perhaps put some condition to constrain what you get back), to make sure your cursor then contains all your values.

Then, I would loop through the cursor's values by using a while loop, (with cursor.moveToPosition(-1) before the loop) moving along the cursor by using cursor.moveToNext().

See the API for more information:

http://developer.android.com/reference/android/database/Cursor.html

With regard to the autoincrement problem, as far as I can remember you can leave out the ID and use db.insert() without that field and the database will provide an ID for you.

You shouldn't have the same issue in your load method because it doesn't make sense to autoincrement when loading, you just get back what's in the database.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜