Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 6629343
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 25, 20262026-05-25T22:14:49+00:00 2026-05-25T22:14:49+00:00

So I’m still building a Database to support a project of mine. There are

  • 0

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

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-05-25T22:14:50+00:00Added an answer on May 25, 2026 at 10:14 pm

    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
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

We're building an app, our first using Rails 3, and we're having to build
I've tracked down a weird MySQL problem to the two different ways I was
I have a reasonable size flat file database of text documents mostly saved in
Let's say I'm outputting a post title and in our database, it's Hello Y&#8217;all
link Im having trouble converting the html entites into html characters, (&# 8217;) i
I'm parsing an RSS feed that has an &#8217; in it. SimpleXML turns this
I know there's a lot of other questions out there that deal with this
I'm trying to convert HTML to plain text. I get many &\#8217; &\#8220; etc.
I have a view passing on information from a database: def serve_article(request, id): served_article
I am writing an app with both english and french support. The app requests

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.