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

  • Home
  • SEARCH
  • 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 8191191
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 7, 20262026-06-07T03:51:52+00:00 2026-06-07T03:51:52+00:00

I have a database set up and I know I am inserting to it

  • 0

I have a database set up and I know I am inserting to it correctly. I am, however, having difficulty returning any data from it through a query. This is my main class and database helper class.

public class DBTesterActivity extends Activity {

MyDBAdapter db;
Cursor cursor;

/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
    Item item1;
    super.onCreate(savedInstanceState);
    setContentView(R.layout.main);


    db = new MyDBAdapter(this);
    db.open();
    db.insertEntry(item1 = new Item("Bathtub", "Bathroom", "Typical", "Clean", "fill, wash", "Round, deep", "Bathroom", "Toilet, Bathroom", R.drawable.ic_launcher));
    //this.populateDatabase();
    //Cursor entryCursor = db.getAllEntries();
    item1 = db.getEntry(1);
    Log.i("db", item1.toString());
}

and my database adapter class here…

public class MyDBAdapter {
private static final String DATABASE_NAME = "myDatabase.db";
private static final String DATABASE_TABLE = "mainTable";
private static final int DATABASE_VERSION = 1;

//main table columns
public static final String KEY_ITEM              = "itemName";
public static final String KEY_GROUP             = "itemGroup";
public static final String ITEM_CLASSIFICATION   = "classification";
public static final String KEY_USE               = "use";
public static final String KEY_ACTION            = "action";
public static final String KEY_PROPERTIES        = "properties";
public static final String KEY_ASSOCIATION       = "association";
public static final String KEY_IMG_ID            = "imgId";

// The index (key) column name for use in where clauses.
public static final String KEY_ID="_id";

// The name and column index of each column in your database.
public static final int NAME_COLUMN = 1;
public static final int GROUP_COLUMN = 2;
public static final int CLASSIFICATION_COLUMN = 3;
public static final int USE_COLUMN = 4;
public static final int ACTION_COLUMN = 5;
public static final int PROPERTIES_COLUMN = 6;
public static final int ASSOCIATION_COLUMN = 7;
public static final int IMG_ID_COLUMN = 8;
// TODO: Create public field for each column in your table.

// SQL Statement to create a new database.
private static final String DATABASE_CREATE = "create table " + 
        DATABASE_TABLE + " (" 
        + KEY_ID + " integer primary key autoincrement, " 
        + KEY_ITEM + " TEXT, " 
        + KEY_GROUP + " TEXT, " 
        + ITEM_CLASSIFICATION + " TEXT, " 
        + KEY_USE + " TEXT, " 
        + KEY_ACTION + " TEXT, " 
        + KEY_PROPERTIES + " TEXT, " 
        + KEY_ASSOCIATION + " TEXT, " 
        + KEY_IMG_ID + " INTEGER);";

// Variable to hold the database instance
private SQLiteDatabase db;
// Context of the application using the database.
private final Context context;
// Database open/upgrade helper
private myDbHelper dbHelper;

public MyDBAdapter(Context _context) {
    context = _context;
    dbHelper = new myDbHelper(context, DATABASE_NAME, null, DATABASE_VERSION);
}

public MyDBAdapter open() throws SQLException {
    try {  
        db = dbHelper.getWritableDatabase();
    } catch (SQLiteException e) {
        db = dbHelper.getReadableDatabase();
    }
    return this;
}

public void close() {
    db.close();
}

public void insertEntry(Item item) {
    // TODO: Create a new ContentValues to represent my row
    // and insert it into the database.
    SQLiteDatabase db = dbHelper.getWritableDatabase();

    ContentValues values = new ContentValues();
    values.put(KEY_ITEM, item.get_item_name());
    values.put(KEY_GROUP, item.get_group());
    values.put(ITEM_CLASSIFICATION, item.get_item_classification());
    values.put(KEY_USE, item.get_use());
    values.put(KEY_ACTION, item.get_action());
    values.put(KEY_PROPERTIES, item.get_properties());
    values.put(KEY_ASSOCIATION, item.get_association());
    values.put(KEY_IMG_ID, item.get_img_id());

    // insert row to table
    try{
        db.insertOrThrow(DATABASE_TABLE, null, values);
        Log.i("success", "insert was successful");
    }catch (Exception e){Log.w("insertFail", "insert failed: " + e.toString());}


}
  public Item getEntry(long _rowIndex) {
// TODO: Return a cursor to a row from the database and
// use the values to populate an instance of MyObject
  SQLiteDatabase db = dbHelper.getReadableDatabase();

  Cursor cursor = db.query(false, DATABASE_TABLE, new String[] { KEY_ID,
          KEY_ITEM, KEY_GROUP, ITEM_CLASSIFICATION, KEY_USE, KEY_ACTION, KEY_PROPERTIES, KEY_ASSOCIATION, KEY_IMG_ID}, KEY_ID + "=?",
          new String[] { String.valueOf(_rowIndex) }, null, null, null, null);
  if (cursor != null)
      cursor.moveToFirst();

  Item item = new Item(Integer.parseInt(cursor.getString(0)),
          cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8),  Integer.parseInt(cursor.getString(9)));
  // return contact
  return item;

}

I am getting these in my logCat —- Bad request for field slot 0,9. numRows = 1, numColumns = 9
and —- java.lang.IllegalStateException: get field slot from row 0 col 9 failed

  • 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-06-07T03:51:53+00:00Added an answer on June 7, 2026 at 3:51 am

    So you want to get all columns so i think more clearer way is tu use rawQuery method.
    You can do it like this:

    SELECT_QUERY = "Select * from " + DATABASE_TABLE + " where " + KEY_ID + " = ?";
    Cursor c = db.rawQuery(SELECT_QUERY, new String[] {String.valueOf(_rowIndex)});
    if (c.getCount() > 0 && c.moveToFirst()) {
       Item item = new Item(Integer.parseInt(cursor.getString(0)),
          cursor.getString(1), cursor.getString(2), cursor.getString(3), cursor.getString(4), cursor.getString(5), cursor.getString(6), cursor.getString(7), cursor.getString(8));
    }
    

    Note: But, you didn’t meant what kind of problem, any error?

    Update:

    Your problem is the most likely this:

    Integer.parseInt(cursor.getString(9))
    

    Numbering of columns start from 0 not from 1 so:

  2. _id – 0
  3. _itemGroup – 1
  4. classification – 2
  5. use – 3
  6. action – 4
  7. properties – 5
  8. association – 6
  9. imgId – 7
  10. properties – 8
  11. So you tried to get non existing column. Repair it and then it should works.

  • 0
  • Reply
  • Share
    Share
    • Share on Facebook
    • Share on Twitter
    • Share on LinkedIn
    • Share on WhatsApp
    • Report

Sidebar

Related Questions

I have a vector<set<char> > data structure (transactions database) and I want to know
Looking to have a database query set all the instance variables in a class:
I've recently written a custom API for inserting data into my database from multiple
I have written an application in Delphi 2010 that imports data from one database
I have my database query currently set up like so: Edit, By the way
I have a database set up with email and password, I'm trying to use
Suppose the following: I have a database set up on database.mywebsite.com , which resolves
I have an SQL database set up in which I would like to use
I have a legacy database set with NLS_LANG set to IW8ISO8859P8. This I cannot
I have a MySQL database set-up in a hierarchy style. There are 4 tables

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.