2013-12-20 65 views
1

我有一本書數據庫,用戶可以添加新書,但在添加之前,我想檢查這本書是否在數據庫中,或者不是由isbn編號..如何做到這一點?在添加android之前搜索

這是我的數據庫幫手:

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號碼沒有在存在數據庫然後添加它,如果它存在然後不保存它。

有人可以幫我嗎? 謝謝!

回答

0

寫在你的治療者類上檢查ISBN現有的記錄功能,將返回FALSE TRUE 。 在插入記錄之前調用此函數,如果它的返回值爲true意味着記錄存在,否則不存在。如果它返回false,則插入記錄。

protected void onCreate(Bundle savedInstanceState) { 
     ...... 
     DBHelper dbHelper = new DBHelper(YourActivity.this); 
    } 
數據庫幫手

:在saveData()

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

調用上面的函數,即

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


} 
+0

非常感謝回覆!我嘗試了它,當我點擊保存按鈕時,應用程序會崩潰:( –

+0

這是正確的?>>'DbHelper dbHelper = new DbHelper(getApplicationContext());' –

+0

@LamaTatwany是的,如果問題沒有解決,那麼post logcat error message。 –

0

Initailize的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 
    } 
} 
+0

對不起getSingleEntry沒有可能工作..我用它嘗試,卻忘了刪除我的ROM碼。沒有其他解決方案嗎? –

相關問題