im having some problems with my sqlite database, as part of my application i have to save the users favourite deals so i am inserting them into the sqlite database. what happens is that a new row would be created with the details of the new Favorited deal but it also updates all existing rows with the same details of this newly added deal.
i've looked on many sites but cannot seem to fix it. could you please help me?
below is my databasehelper class:
public class DatabaseHelper extends SQLiteOpenHelper{
// Database Version
private static final int DATABASE_VERSION = 1;
// Database Name
private static final String DATABASE_NAME = "unilife";
// Login table name
private static final String TABLE_LOGIN = "login";
// Login Table Columns names
private static final String KEY_ID = "id";
private static final String KEY_NAME = "name";
private static final String KEY_EMAIL = "email";
private static final String KEY_USERNAME = "username";
private static final String KEY_USER_ID = "userid";
public static final String TABLE_DEAL= "deal";
public static final String KEY_DEAL_ID = "dealid";
public static final String KEY_DEAL_TITLE = "dealtitle";
public static final String KEY_START_DATE = "startdate";
public static final String KEY_EXPIRY_DATE = "expirydate";
public static final String KEY_DETAILS = "details";
public static final String KEY_PHOTO = "photo";
public static final String KEY_DEAL_VENDOR_ID = "vendorid";
public DatabaseHelper(Context context){
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
// create the database tables
@Override
public void onCreate(SQLiteDatabase db){
String loginSQL = "CREATE TABLE " + TABLE_LOGIN + "("
+ KEY_ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ KEY_USER_ID + " TEXT, "
+ KEY_NAME + " TEXT,"
+ KEY_USERNAME + " TEXT,"
+ KEY_EMAIL + " TEXT UNIQUE )";
db.execSQL(loginSQL);
String dealSQL = "CREATE TABLE " + TABLE_DEAL + " ("
+ KEY_ID + " INTEGER PRIMARY KEY, "
+ KEY_DEAL_ID + " TEXT, "
+ KEY_DEAL_TITLE + " TEXT, "
+ KEY_START_DATE + " TEXT, "
+ KEY_EXPIRY_DATE + " TEXT, "
+ KEY_DETAILS + " TEXT, "
+ KEY_PHOTO + " TEXT, "
+ KEY_DEAL_VENDOR_ID + " TEXT )";
Log.i("DBHELPER", dealSQL);
db.execSQL(dealSQL);
}
// upgrading database
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion){
// delete the old table
db.execSQL("DROP TABLE IF EXISTS " + TABLE_LOGIN);
db.execSQL("DROP TABLE IF EXISTS " + TABLE_DEAL);
// create tables again
onCreate(db);
}
public void addFavourite(String dealid, String dealtitle, String startdate,
String expirydate, String details, String photo , `enter code here`String vendorid){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(KEY_DEAL_ID, dealid);
cv.put(KEY_DEAL_TITLE, dealtitle);
cv.put(KEY_START_DATE, startdate);
cv.put(KEY_EXPIRY_DATE, expirydate);
cv.put(KEY_DETAILS, details);
cv.put(KEY_PHOTO, photo);
cv.put(KEY_DEAL_VENDOR_ID, vendorid);
Log.i("DBHELPER", dealtitle);
db.insert(TABLE_DEAL, null, cv);
db.close();
}
public ArrayList<HashMap<String, String>> getFavourites() {
ArrayList<HashMap<String, String>> allfavourites = new ArrayList<HashMap<String, String>>();
HashMap<String, String> favourite = new HashMap<String, String>();
String query = "SELECT * FROM " + TABLE_DEAL;
SQLiteDatabase db = this.getReadableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()){
do {
favourite.put("dealid", cursor.getString(1));
favourite.put("dealtitle", cursor.getString(2));
favourite.put("startdate", cursor.getString(3));
favourite.put("expirydate", cursor.getString(4));
favourite.put("details", cursor.getString(5));
favourite.put("photo", cursor.getString(6));
favourite.put("vendorid", cursor.getString(7));
allfavourites.add(favourite);
} while (cursor.moveToNext());
}
cursor.close();
db.close();
return allfavourites;
}
/*public void removeFromFavourites(String dealid){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_DEAL, KEY_DEAL_ID + " = " + dealid , null);
db.close();
}*/
public void removeFromFavourites(Deal deal){
SQLiteDatabase db = this.getWritableDatabase();
db.delete(TABLE_DEAL, KEY_ID + " = ?" , new String[]{String.valueOf(deal.getId())});
db.close();
}
}
Below is activity that has a imagebutton that would be used to add the deal to the database. the deals original come from a json request.
public class DealsDetailActivity extends Activity {
String dealid ;
String dealtitle ;
String startdate;
String expirydate ;
String details ;
String dealvendorid ;
String vendorphoto;
public String sDealid;
private DatabaseHelper helper;
private SQLiteDatabase db;
private ImageButton favouritesBtn;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.detail_deals);
Intent i = getIntent();
sDealid = i.getStringExtra(TAG_DEAL_ID);
getPostsForVendor();
dealid = i.getStringExtra(TAG_DEAL_ID);
dealtitle = i.getStringExtra(TAG_DEAL_TITLE);
startdate = i.getStringExtra(TAG_START_DATE);
expirydate = i.getStringExtra(TAG_EXPIRY_DATE);
details = i.getStringExtra(TAG_DEAL_DESC);
dealvendorid = i.getStringExtra(TAG_DEAL_VENDOR_ID);
vendorphoto = i.getStringExtra(TAG_VENDOR_PHOTO);
TextView title = (TextView)findViewById(R.id.deal_details_deal_name);
title.setText(dealtitle);
TextView date = (TextView)findViewById(R.id.deal_details_expiry_date);
date.setText(startdate + " - " + expirydate);
TextView desc = (TextView)findViewById(R.id.deal_details_deal_desc);
desc.setText(details);
final ImageView photo = (ImageView)findViewById(R.id.deal_details_place_pic);
Bitmap bitmap = ListViewAdapter.getBitmapFromURL(vendorphoto);
photo.setImageBitmap(bitmap);
postbtn = (Button)findViewById(R.id.post_btn);
postbtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
Intent i = new Intent(DealsDetailActivity.this, AddPostActivity.class);
i.putExtra(TAG_DEAL_ID, dealid);
i.putExtra(TAG_DEAL_VENDOR_ID, dealvendorid);
i.putExtra(TAG_DEAL_TITLE, dealtitle);
i.putExtra(TAG_VENDOR_PHOTO, vendorphoto);
startActivity(i);
}
});
///////////////////////////////////////////////////////////////////////
// this is where the deals would be added to the database so that if //
// the favourite button is clicked then it would be inserted //
// into the database //
// i tried to insert to the database directly here as well as //
// previously trying to insert with the database method addfavourites//
///////////////////////////////////////////////////////////////////////
favouritesBtn = (ImageButton)findViewById(R.id.deals_details_fav);
favouritesBtn.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
//Set the button's appearance
v.setSelected(!v.isSelected());
if (v.isSelected()) {
//Handle selected state change
helper = new DatabaseHelper(getApplicationContext());
db = helper.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(DatabaseHelper.KEY_DEAL_ID, dealid);
cv.put(DatabaseHelper.KEY_DEAL_TITLE, dealtitle);
cv.put(DatabaseHelper.KEY_START_DATE, startdate);
cv.put(DatabaseHelper.KEY_EXPIRY_DATE, expirydate);
cv.put(DatabaseHelper.KEY_DETAILS, details);
cv.put(DatabaseHelper.KEY_PHOTO, vendorphoto);
cv.put(DatabaseHelper.KEY_DEAL_VENDOR_ID, dealvendorid);
db.insert(DatabaseHelper.TABLE_DEAL, null, cv);
Log.e(HomeActivity.TAG, "THE ARRAYLIST OUTPUT: " + cv);
helper.close();
db.close();
Toast.makeText(getApplicationContext(), "Deal added to Favourites", Toast.LENGTH_LONG).show();
} else {
//Handle de-select state change
helper = new DatabaseHelper(getApplicationContext());
helper.getReadableDatabase();
// helper.removeFromFavourites(deal);
Toast.makeText(getApplicationContext(), "Deal Removed from Favourites", Toast.LENGTH_LONG).show();
}
}
});
}
could you please help because i can't seem to figure out where im going wrong. i used databases before in the same way and this has never happened before.
thank you in advance :)
Aucun commentaire:
Enregistrer un commentaire