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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T10:12:53+00:00 2026-05-16T10:12:53+00:00

This is my SimpleDBAdapter Class that encapsulates all the complexities of accessing the database

  • 0

This is my SimpleDBAdapter Class that encapsulates all the complexities of accessing the database and the inner class SimpleDBHelper takes care of CRUD operations,now the Problem that I am facing here is that when I deploy the code, the tables were getting created, but I am unable to insert the values in to the table, the id is returning -1 that depicts that there is an error,while inserting the values.

id = db.insert(TABLE_SIMPLETABLE_CLIENT1,null,contentvalues);

SimpleDBAdapter.java

import android.content.ContentValues;
import android.content.Context;
import android.database.SQLException;
import android.database.sqlite.SQLiteDatabase;
import android.database.sqlite.SQLiteOpenHelper;
import android.util.Log;
import android.view.ViewGroup.MarginLayoutParams;

public class SimpleDBAdapter {
    // Define all the constants that are to used in the Program
    private static final String DATABASE_NAME = "SimpleDB.db";
    private static final String TABLE_SIMPLETABLE_CLIENT1 = "SimpleTable1";
    private static final int DATABASE_VERSION = 1;

    private static final String KEY_EMPLOYEE_ID = "Employee_id";
    private static final String KEY_EMPLOYEE_NAME = "Name";
    private static final String KEY_EMPLOYEE_DESIGNATION = "Designation";
    private static final String KEY_EMPLOYEE_MARITUALSTATUS = "Maritual Status";
    private static final String KEY_EMPLOYEE_DOJ = "Date of Joining";

    private static final String TAG = "SimpleDBAdapter";

    String CREATE_TABLE_SIMPLETABLE = "CREATE TABLE IF NOT EXISTS "
        + TABLE_SIMPLETABLE_CLIENT1
        + " (Employee_ID INTEGER PRIMARY KEY AUTOINCREMENT, "
        + "  Employee_Name VARCHAR(20)," + "  JoinedDateTime DATETIME,"
        + "  Designation VARCHAR(20),"
        + "  Maritual_Status BOOLEAN DEFAULT 'FALSE');";

    private final Context ctx;
    private SQLiteDatabase db;
    private SimpleDBHelper simpledbhelper;

    public SimpleDBAdapter(Context context) {
    Log.i(TAG,"SimpleDBAdapter Constructor");
     ctx = context;
     simpledbhelper = new SimpleDBHelper(ctx);
    }

      class SimpleDBHelper extends SQLiteOpenHelper {
        private static final String TAG = "SimpleDBHelper";
        public SimpleDBHelper(Context context) {
        super(context, DATABASE_NAME, null, DATABASE_VERSION);
    Log.i(TAG,"Calling.. super(context, DATABASE_NAME, null, DATABASE_VERSION)");
        }   

        @Override
        public void onCreate(SQLiteDatabase db) {
            Log.i(TAG,"Creating Table named CREATE_TABLE_SIMPLETABLE");
            db.execSQL(CREATE_TABLE_SIMPLETABLE);
               }



        @Override
        public void onUpgrade(SQLiteDatabase db, int oldversion, int newversion) {
            Log.w(TAG, "Upgarding from" + oldversion + "to" + newversion
                    + "could remove all the old data");
            db.execSQL("DROP TABLE IF EXISTS TABLE_SIMPLETABLE_CLIENT1");
            onCreate(db);
        }
    }

    //Opens the DB
    public void open() throws SQLException {
        Log.i(TAG,"Open() Called...");
        db = simpledbhelper.getWritableDatabase();
        simpledbhelper.onCreate(db);

    }

    //closes the DB
    public void close() {
        Log.i(TAG,"Close() Called...");
        simpledbhelper.close();
    }

    //inserting the values in to Table
    public long insertEntry(String Name,String Designation,Boolean MaritualStatus,String date){
        Log.i(TAG,"Insert Entry ()");
        long id = 0;
        try{
        ContentValues contentvalues = new ContentValues();
        contentvalues.put(KEY_EMPLOYEE_NAME, Name);
        contentvalues.put(KEY_EMPLOYEE_DOJ,date);
        contentvalues.put(KEY_EMPLOYEE_DESIGNATION, Designation);
        contentvalues.put(KEY_EMPLOYEE_MARITUALSTATUS, MaritualStatus);
        id = db.insert(TABLE_SIMPLETABLE_CLIENT1,null,contentvalues);
        }
        catch(Exception err){
            Log.i(TAG,"Error Encountered...");
            Log.e(TAG,String.valueOf(err).toString());
        }
        return id;
    }

}

In the MainClass file (DBHandler.java), i am creating instance of the code and calling the insert method of this class as follows,

DBHandler.java

import android.app.Activity;
import android.os.Bundle;
import android.util.Log;

public class SimpleDBApplication extends Activity {
private static final String TAG = "SimpleDBApplication";
/** Called when the activity is first created. */
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
SimpleDBAdapter DBAdapter = new SimpleDBAdapter(this);
Log.i(TAG,"Called SimpleDBAdapter");
//call to open the DB
DBAdapter.open();
long id;
// add an entry
id = DBAdapter.insertEntry("Vinayaka","CEO",false,"17-7-2010");
Log.w(TAG,"Tuple Inserted");
Log.w(TAG," The Value of ID :" +String.valueOf(id).toString());

//call to close the DB
DBAdapter.close();

}
}

Thanks in Advance…

  • 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-16T10:12:53+00:00Added an answer on May 16, 2026 at 10:12 am

    As I can see so far, your ContentValues keys are not the same as the column names, which should be as I recall.

    So your constants would have to become;

    private static final String KEY_EMPLOYEE_ID = "Employee_ID";
    private static final String KEY_EMPLOYEE_NAME = "Employee_Name";
    private static final String KEY_EMPLOYEE_DESIGNATION = "Designation";
    private static final String KEY_EMPLOYEE_MARITUALSTATUS = "Maritual_Status";
    private static final String KEY_EMPLOYEE_DOJ = "JoinedDateTime";
    

    if that doesn’t solve your problem, try to post the stacktrace, so that we have more details to help you.

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

Sidebar

Related Questions

This past summer I was developing a basic ASP.NET/SQL Server CRUD app, and unit
This error just started popping up all over our site. Permission denied to call
This is beyond both making sense and my control. That being said here is
I want to use a temp directory that will be unique to this build.
This is a bit of a long shot, but if anyone can figure it
This is starting to vex me. I recently decided to clear out my FTP,
This is kinda oddball, but I was poking around with the GNU assembler today
This might seem like a stupid question I admit. But I'm in a small
This is a difficult and open-ended question I know, but I thought I'd throw
This is my first post here and I wanted to get some input from

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.