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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 31, 20262026-05-31T14:41:03+00:00 2026-05-31T14:41:03+00:00

I have a a databasehelper and an activity with codes given below. The problem

  • 0

I have a a databasehelper and an activity with codes given below. The problem I am having is that each time i run the application it reinserts the already inserted values. I don’t want this to happen. I would love som help.

 try {
        db.open();
        long id;

        id=db.insertTitle("0470285818", "C# programmer", "wrox");
        id=db.insertTitle("0907865", "Professional Windows", "wrox");


        Cursor c=db.getAllTitles();//using getTitle to get a particular row

        if(c.moveToFirst()){
            do{
                DisplayTitle(c);

            }while(c.moveToNext());
        }/*else   Toast.makeText(this, "No title found",  Use this when using getTitle
                Toast.LENGTH_LONG).show();*/
        db.close();
    } catch (SQLException e) {
        // TODO Auto-generated catch block
        e.printStackTrace();
    }

the DBhelper class croped to the inserttitle method

private static final String DATABASE_CREATE=
            "create table if not exists titles (_id integer primary key autoincrement, "
            +"isbn text not null, title text not null, "
                    +"publisher text not null)";
private final Context context;
private DatabaseHelper DBHelper;
private SQLiteDatabase db;

public DBAdapter(Context ctx){
this.context=ctx;
DBHelper=new DatabaseHelper(context);
}
private static class DatabaseHelper extends SQLiteOpenHelper{
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) {
    // TODO Auto-generated method stub
    Log.w(TAG, "Upgrading database from version "+oldVersion+" to "+newVersion+" which will distroy all old data");
    db.execSQL("DROP TABLE IF EXISTS titles");
    onCreate(db);


}
}
 public DBAdapter open()throws SQLException{
db=DBHelper.getWritableDatabase();
return this;

}
public void close(){
DBHelper.close();
}
public long insertTitle(String isbn,String title,String publisher){
ContentValues initialValues =new ContentValues();
initialValues.put(KEY_ISBN, isbn);
initialValues.put(KEY_TITLE, title);
initialValues.put(KEY_PUBLISHER, publisher);

return db.insertOrThrow(DATABASE_TABLE, null, initialValues);
}
   public boolean deleteTitle(long rowid){
return db.delete(DATABASE_TABLE, KEY_ROWID+"="+rowid, null)>0;
  }
  public Cursor getAllTitles() 
{
return db.query(DATABASE_TABLE, new String[] {
        KEY_ROWID, 
        KEY_ISBN,
        KEY_TITLE,
        KEY_PUBLISHER}, 
        null, 
        null, 
        null, 
        null, 
        null);
}
public Cursor getTitle(long rowid)throws SQLException{
Cursor mCursor=db.query(DATABASE_TABLE, new String[]{
        KEY_ROWID,KEY_ISBN,KEY_TITLE,KEY_PUBLISHER
}, KEY_ROWID+"="+rowid, null, null, null, null);

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

return mCursor;

}
   public boolean updateTitle(long rowid,String isbn,String title,String publisher){

ContentValues args=new ContentValues();
args.put(KEY_ISBN, isbn);
args.put(KEY_TITLE, title);
args.put(KEY_PUBLISHER, publisher);

return db.delete(DATABASE_TABLE, KEY_ROWID+"="+rowid, null)>0;

}
}
  • 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-31T14:41:04+00:00Added an answer on May 31, 2026 at 2:41 pm

    You’ll have to directly insert the values in the onCreate(SQLiteDatabase db) method of your SqliteOpenHelper implementation:

    @Override
    public void onCreate(SQLiteDatabase db) {
        db.execSQL(DATABASE_CREATE); 
        String[] isbn = {"isbn1", "isbn2", "isbn3"};
        String[] title = {"title1", "title2", "title3"};   
        String[] publisher = {"pub1", "pub2", "pub3"};
        for (int i = 0; i < isbn.length; i++) {
            ContentValues initialValues =new ContentValues();
            initialValues.put(KEY_ISBN, isbn[i]);
            initialValues.put(KEY_TITLE, title[i]);
            initialValues.put(KEY_PUBLISHER, publisher[i]);
            db.insertOrThrow(DATABASE_TABLE, null, initialValues);
        }
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an application that runs an activity with a service in a separate
I have a class called DatabaseHelper that wraps a DbConnection. What's the proper way
I have created an application in Android that needs to send emails to all
I have an activity that shows customer information. All the controls are drawn dynamically
I have been having this problem for a few days now, and really cant
I have a hard time getting fragments to update their views... Specifically, fragments that
I have a strange problem on an android application, I have no such table
I have a problem with SQLite, it doesn't work when i call DataBaseHelper(who store
Have you used VS.NET Architect Edition's Application and System diagrams to start designing a
Have a n-tire web application and search often times out after 30 secs. How

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.