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 6805027
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 26, 20262026-05-26T19:32:14+00:00 2026-05-26T19:32:14+00:00

concerning android development, I’m simply trying to create an SQL database when the activity

  • 0

concerning android development, I’m simply trying to create an SQL database when the activity is launched for the first time (using preferences). The second time the activity is launched it should retrieve the data from the database and output a log message.

Ive managed to launch the activity for the first time (im assuming the database was created here) but the second time i get an IllegalStateException error: get field slot from row 0 to col -1 failed. Not really sure where i went wrong here. can someone please check? Thanks

Main class

public class MainMenu extends Activity implements OnClickListener{
private ModulesDbAdapter mpDbHelper;

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

    //create database here
    SharedPreferences initialPref = getSharedPreferences("INITIAL", 0);
    boolean firsttimer = initialPref.getBoolean("INITIAL", false);

    if (!firsttimer){
        //create database here
        mpDbHelper = new ModulesDbAdapter(this);
        mpDbHelper.open();

        long id1 = mpDbHelper.createReminder("Hello1", "Test 1", "Date1");
        long id2 = mpDbHelper.createReminder("Hello2", "Test 2", "Date2");
        long id3 = mpDbHelper.createReminder("Hello3", "Test 3", "Date3");

        /*Cursor c = mpDbHelper.fetchModules((String)"CS118");
        Log.d("TESTING",c.getString(c.getColumnIndex("KEY_MOD_NAME")));*/

        //get boolean preference to true
        SharedPreferences.Editor editorPref = initialPref.edit();
        editorPref.putBoolean("INITIAL", true);
        editorPref.commit();
    }else {
        fetchData();
    }
}

private void fetchData(){
    mpDbHelper = new ModulesDbAdapter(this);
    mpDbHelper.open();
    long input = 2;

    Cursor c = mpDbHelper.fetchReminder(input);
    startManagingCursor(c);
    Log.d("TESTING",c.getString(c.getColumnIndex("KEY_BODY")));
}

/*@Override
protected void onPause() {
    super.onPause();
    mpDbHelper.close();
}

@Override
protected void onStop() {
    super.onPause();
    mpDbHelper.close();
}*/

}
}

Adapter class

public class ModulesDbAdapter {
private static final String DATABASE_NAME = "data";
private static final String DATABASE_TABLE = "reminders";
private static final int DATABASE_VERSION = 1;

public static final String KEY_TITLE = "title";
public static final String KEY_BODY = "body";
public static final String KEY_DATE_TIME = "reminder_date_time";
public static final String KEY_ROWID = "_id";

private DatabaseHelper mDbHelper; 
private SQLiteDatabase mDb;

//defines the create script for the database
private static final String DATABASE_CREATE =
        "create table " + DATABASE_TABLE + " ("
        + KEY_ROWID + " integer primary key autoincrement, "
        + KEY_TITLE + " text not null, "
        + KEY_BODY + " text not null, "
        + KEY_DATE_TIME + " text not null);";

//Context object that will be associated with the SQLite database object
private final Context mCtx;

//The Context object is set via the constructor of the class
public ModulesDbAdapter(Context ctx) {
    this.mCtx = ctx;
}

//helps with the creation and version management of the SQLite database.
private static class DatabaseHelper extends SQLiteOpenHelper {

    //call made to the base SQLiteOpenHelper constructor. This call creates, opens, and/or manages a database
    DatabaseHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    }

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE); 
    }
    @Override
    public void onUpgrade(SQLiteDatabase db, int oldVersion,int newVersion) {
        // Not used, but you could upgrade the database with ALTER
        // Scripts
    }
}

//now create the database by calling the getReadableDatabase()
public ModulesDbAdapter open() throws android.database.SQLException {
    mDbHelper = new DatabaseHelper(mCtx);
    mDb = mDbHelper.getWritableDatabase();
    return this;
}

//close the database
public void close() {
    mDbHelper.close();
}

public long createReminder(String title, String body, String
    reminderDateTime) {
    ContentValues initialValues = new ContentValues();
    initialValues.put(KEY_TITLE, title);
    initialValues.put(KEY_BODY, body);
    initialValues.put(KEY_DATE_TIME, reminderDateTime);

    //insert row into database
    return mDb.insert(DATABASE_TABLE, null, initialValues);
}



public boolean deleteReminder(long rowId) {
    return mDb.delete(DATABASE_TABLE, KEY_ROWID + "=" + rowId, null) > 0;
}

//utilizes the query() method on the SQLite database to find all the reminders in the system
public Cursor fetchAllReminders() {
    return mDb.query(DATABASE_TABLE, new String[] {KEY_ROWID, KEY_TITLE,
    KEY_BODY, KEY_DATE_TIME}, null, null, null, null, null);
}

public Cursor fetchReminder(long rowId) throws SQLException { 
    Cursor mCursor =
    mDb.query(true, DATABASE_TABLE, new String[] {KEY_ROWID,
    KEY_TITLE, KEY_BODY, KEY_DATE_TIME}, KEY_ROWID + "=" +
    rowId, null, null, null, null, null);



    if (mCursor != null) {
        mCursor.moveToFirst();
    }
    return mCursor;
}

public boolean updateReminder(long rowId, String title, String body, String reminderDateTime) {
    ContentValues args = new ContentValues();
    args.put(KEY_TITLE, title);
    args.put(KEY_BODY, body);
    args.put(KEY_DATE_TIME, reminderDateTime);
    return mDb.update(DATABASE_TABLE, args, KEY_ROWID + "=" + rowId, null) > 0;
}
        // The SQLiteOpenHelper class was omitted for brevity
        // That code goes here.

}

  • 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-26T19:32:15+00:00Added an answer on May 26, 2026 at 7:32 pm

    The error is simple. You’ve defined KEY_BODY as a static string constant value in the DbAdapter class, but you’ve used the string literal “KEY_BODY” in your access code.

    Log.d("TESTING",c.getString(c.getColumnIndex("KEY_BODY")));
    

    should be

    Log.d("TESTING",c.getString(c.getColumnIndex(ModulesDbAdapter.KEY_BODY)));
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have a two part question concerning Android. The first, is if I have
I asked this question about using the system-drawables in an Android-app some time ago:
I want to create a rate system for an android application using java. Ive
My question is concerning SQL connection status, load, etc. based on the following code:
In Android documentation concerning code signing we can read: By signing multiple applications with
Using my new Android phone established a small problem: I'm living in country A
=== about me === I'm new to android development and barely able to speak
Possible Duplicate: converting pixels to dp in android I'm trying to convert pixels to
I am able to Record Android's Camera Using Mediarecorder Class. I heard that sipdroid
when i am trying to run a simple android application in netbeans 6.9.1 i

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.