如何阻止我的数据库重新插入已手动插入的项目?

时间:2012-03-21 16:44:22

标签: android sqliteopenhelper

我有一个databasehelper和一个活动,下面给出了代码。我遇到的问题是,每次运行应用程序时,它都会重新插入已插入的值。我不希望这种情况发生。我很乐意帮忙。

 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();
    }

DBhelper类错误地插入了inserttitle方法

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;

}
}

2 个答案:

答案 0 :(得分:1)

您必须直接在onCreate(SQLiteDatabase db)实施的SqliteOpenHelper方法中插入值:

@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);
    }
}

答案 1 :(得分:0)

您可以在执行插入之前查询数据库,并查看内容是否已存在。如果是,请不要再次插入。