在添加android之前搜索

时间:2013-12-20 07:45:52

标签: android database eclipse sqlite

我有一个图书数据库,用户可以在其中添加新书,但在添加之前我想通过isbn编号检查这本书是否在数据库中..怎么做?

这是我的数据库助手:

   package com.example.mobilib;

   import com.example.mobilib.LoginDataBaseAdapter;

   import android.content.Context;
   import android.database.Cursor;
   import android.database.sqlite.SQLiteDatabase;
   import android.database.sqlite.SQLiteOpenHelper;
   import android.util.Log;

   public class DbHelper extends SQLiteOpenHelper {
static String DATABASE_NAME="M";
public static final String TABLE_NAME="Book";
public static final String KEY_NAME="name";
public static final String KEY_AUTHOR="author";
public static final String KEY_INFO="info";
public static final String KEY_ISBN="isbn";
public static final String KEY_COPIES="copy";
public static final String KEY_ID="id"; 
static String CREATE_TABLE="CREATE TABLE "+TABLE_NAME+" ("+KEY_ID+" INTEGER PRIMARY KEY, "+KEY_NAME+" TEXT, "+KEY_AUTHOR+" TEXT, "+KEY_INFO+" TEXT, "+KEY_ISBN+" TEXT, "+KEY_COPIES+" TEXT)";

public DbHelper(Context context) {
    super(context, DATABASE_NAME, null, 1);

}



@Override
public void onCreate(SQLiteDatabase db) 

{
    try{
        db.execSQL(LoginDataBaseAdapter.DATABASE_CREATE);          
        db.execSQL(CREATE_TABLE);
    }catch( Exception e){
        Log.e("dbAdapter", e.getMessage().toString());
    }

}








@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
    Log.w("TaskDBAdapter", "Upgrading from version " +oldVersion + " to " +newVersion + ", which will destroy all old data");
    db.execSQL("DROP TABLE IF EXISTS "+TABLE_NAME);
    onCreate(db);


   }

}

这是我的添加功能:

       // saveButton click event 
public void onClick(View v) {
    name=edit_name.getText().toString().trim();
    author=edit_author.getText().toString().trim();
    isbn=edit_isbn.getText().toString().trim();
    copy=edit_copy.getText().toString().trim();
    info=edit_info.getText().toString().trim();
    if(name.length()>0 && author.length()>0 && isbn.length()>0 && copy.length()>0 && info.length()>0)
    {
        saveData();
    }
    else
    {
        AlertDialog.Builder alertBuilder=new AlertDialog.Builder(AdminAddBook.this);
        alertBuilder.setTitle("Invalid Data");
        alertBuilder.setMessage("Please, Enter valid data");
        alertBuilder.setPositiveButton("Ok", new DialogInterface.OnClickListener() {

            public void onClick(DialogInterface dialog, int which) {
            dialog.cancel();

            }
        });
        alertBuilder.create().show();
    }

}

/**
 * save data into SQLite
 */
private void saveData(){
    dataBase=mHelper.getWritableDatabase();
    ContentValues values=new ContentValues();

    values.put(DbHelper.KEY_NAME,name);
    values.put(DbHelper.KEY_AUTHOR,author );
    values.put(DbHelper.KEY_ISBN,isbn );
    values.put(DbHelper.KEY_COPIES,copy );
    values.put(DbHelper.KEY_INFO,info );

    System.out.println("");
    if(isUpdate)
    {    
        //update database with new data 
        dataBase.update(DbHelper.TABLE_NAME, values, DbHelper.KEY_ID+"="+id, null);
    }
    else
    {
        //insert data into database
        dataBase.insert(DbHelper.TABLE_NAME, null, values);
    }
    //close database
    dataBase.close();
    finish();


}

   }

我想要的是在添加之前搜索..如果数据库中不存在这个isbn数字然后添加它,如果它存在则不保存它。

有人能帮帮我吗? 谢谢!

2 个答案:

答案 0 :(得分:0)

初始化您的活动DBHelper中的onCreate

DBHelper dbHelper = new DBHelper(YourActivity.this);

您的保存功能

// saveButton click event
public void onClick(View v) {
    name = edit_name.getText().toString().trim();
    author = edit_author.getText().toString().trim();
    isbn = edit_isbn.getText().toString().trim();
    copy = edit_copy.getText().toString().trim();
    info = edit_info.getText().toString().trim();
    String existingIsbn = dbHelper.getSinlgeEntry(name);
    // User not exist
    if(existingIsbn.equals("NOT EXIST")){
        //save the data
    }
}

答案 1 :(得分:0)

在你的治疗师类中写一个函数来检查ISBN上的现有记录,该记录将返回true为false。 在插入记录之前调用此函数,如果返回true则表示其他记录不存在。如果返回false,则插入记录。

 protected void onCreate(Bundle savedInstanceState) {
        ......
        DBHelper dbHelper = new DBHelper(YourActivity.this);
   }
数据库助手中的

public boolean isExist(String isbnNo)
{
    SQLiteDatabase db;
    db = this.getWritableDatabase();
    String query= "select * from TABLE_NAME where KEY_ISBN='"+isbnNo+"' ";      

   Cursor cursor=db.rawQuery(query, null);

    if(cursor.getCount()<1) // UserName Not Exist
    {
        cursor.close();
        return false;
    }

    cursor.close();
    return true;                
}

saveData()中调用上述函数,即

private void saveData(){
    dataBase=mHelper.getWritableDatabase();
    ContentValues values=new ContentValues();

    values.put(DbHelper.KEY_NAME,name);
    values.put(DbHelper.KEY_AUTHOR,author );
    values.put(DbHelper.KEY_ISBN,isbn );
    values.put(DbHelper.KEY_COPIES,copy );
    values.put(DbHelper.KEY_INFO,info );

    System.out.println("");
    if(isUpdate)
    {    
        //update database with new data 
        dataBase.update(DbHelper.TABLE_NAME, values, DbHelper.KEY_ID+"="+id, null);
    }
    else
    {
        //insert data into database
    if(!dbHelper.isExist(isbn)
            dataBase.insert(DbHelper.TABLE_NAME, null, values);
    }
    //close database
    dataBase.close();
    finish();


}