我有一本書數據庫,用戶可以添加新書,但在添加之前,我想檢查這本書是否在數據庫中,或者不是由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號碼沒有在存在數據庫然後添加它,如果它存在然後不保存它。
有人可以幫我嗎? 謝謝!
非常感謝回覆!我嘗試了它,當我點擊保存按鈕時,應用程序會崩潰:( –
這是正確的?>>'DbHelper dbHelper = new DbHelper(getApplicationContext());' –
@LamaTatwany是的,如果問題沒有解決,那麼post logcat error message。 –