Database is not updating data from one activity to another - android

I need to pass data from DeleteLayout to update data in historyActivity which will update once I edit the data.It does not throw any error but my data is not updating after editing. What can be done to pass data between activities and update the database.
My updateDatamethod in DatabaseHelper looks like this:
public boolean updateData(String goalDate, String goalWeight, String currentWeight){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
String where = "GDATE = ? AND GWEIGHT = ? AND CWEIGHT = ?";
contentValues.put(Col2, goalDate);
contentValues.put(Col3, goalWeight);
contentValues.put(Col4, currentWeight);
db.update(TABLE_NAME, contentValues, where, new String[]{goalDate,goalWeight,currentWeight});
return true;
}
Deletelayout
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.deletelayout);
GoalDate = findViewById(R.id.goalDate2);
CurrentWeight = findViewById(R.id.weighGoal2);
GoalWeight = findViewById(R.id.weightCurrent2);
goalD = findViewById(R.id.goalDateInput);
goalW = findViewById(R.id.goalWeightInput);
currentW = findViewById(R.id.currentWeightInput);
imageView = findViewById(R.id.imageShow);
data = myDB.getListContents();
deleteB = findViewById(R.id.deleteB);
updateB = findViewById(R.id.updateButton);
// Entered information from the user are set to the respective variables.
int numRows = data.getCount();
j = new Intent(DeleteLayout.this, historyActivity.class);
if (numRows == 0) {
Toast.makeText(DeleteLayout.this, "Nothing in the db", Toast.LENGTH_LONG).show();
} else {
int i = 0;
while (data.moveToNext()) {
user = new User(data.getString(1), data.getString(2), data.getString(3), data.getString(4), data.getString(5));
gWt = user.getGoalWeight();
cWt = user.getCurrentWeight();
gDt = user.getGoalDate();
GoalDate.setText(gDt);
GoalWeight.setText(gWt);
CurrentWeight.setText(cWt);
deleteB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
myDB.deleteContent(GoalDate.getText().toString(), GoalWeight.getText().toString(), CurrentWeight.getText().toString());
startActivity(j);
}
});
updateB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean myUpdate = myDB.updateData(goalD.getText().toString(), goalW.getText().toString(), currentW.getText().toString());
if(myUpdate == true){
Toast.makeText(DeleteLayout.this, "Data updated", Toast.LENGTH_SHORT).show();
}
else{
Toast.makeText(DeleteLayout.this, "Data not updated", Toast.LENGTH_SHORT).show();
}
startActivity(j);
}
});
}
}
}

Issues
One issue you have is that you are not asking for any changes to be made.
Additionally you are always assuming that a row or rows have always been update, i.e. you are not checking to see if any updates have been performed.
The SQliteDatabase update method returns an int that has the number of rows that have been updated. If none are updated then it will return 0.
Explanation
Consider that goalDate is 2019-01-01 and goalWeight is 100 and currentWeight is 200, then you are saying
update
goalDate to 2019-01-01 and
goalWeight to 100 and
currentWeight to 200
for the row(s) where
goalDate is 2019-01-01 and
goalWeight is 100 and
currentWeight is 200
Fix
Instead of :-
public boolean updateData(String goalDate, String goalWeight, String currentWeight){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
String where = "GDATE = ? AND GWEIGHT = ? AND CWEIGHT = ?";
contentValues.put(Col2, goalDate);
contentValues.put(Col3, goalWeight);
contentValues.put(Col4, currentWeight);
db.update(TABLE_NAME, contentValues, where, new String[]{goalDate,goalWeight,currentWeight});
return true;
}
What you want is something along the lines of :-
public boolean updateData(String goalDate, String goalWeight, String currentWeight, String newGoaldate, String newGoalWeight, String newCurrentWeight){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
String where = "GDATE = ? AND GWEIGHT = ? AND CWEIGHT = ?";
contentValues.put(Col2, newGoalDate);
contentValues.put(Col3, newGoalWeight);
contentValues.put(Col4, newCurrentWeight);
if (db.update(TABLE_NAME, contentValues, where, new String[]{goalDate,goalWeight,currentWeight}) > 0) {
return true;
}
return false;
}
The first 3 parameters are the original values (used to locate the row to be updated), the second set are the values to be used to update the located row or rows).
Extra
Ideally you should also change :-
String where = "GDATE = ? AND GWEIGHT = ? AND CWEIGHT = ?";
to :-
String where = Col2 + "=? AND " + Col3 + "=? AND " + Col4 +"=?";

Related

dataBase does not update

my database cant update
this is my updateMethod
public long updateInfo(ModelInsertInfo modelInsertInfo) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_ID,modelInsertInfo.getId());
values.put(KEY_yearWater, modelInsertInfo.getYearWater());
values.put(KEY_MonthWater, modelInsertInfo.getMonthWater());
values.put(KEY_DayWater, modelInsertInfo.getDayWater());
values.put(KEY_HourWater, modelInsertInfo.getHourWater());
values.put(KEY_MinWater, modelInsertInfo.getMinWater());
return db.update(TABLE_Insert_Info, values, "" + KEY_ID + "= " + modelInsertInfo.getId() + "", null);
}
and this codes in activity
ModelInsertInfo modelInsertInfo = new ModelInsertInfo();
ActionInsertInfo actionInsertInfo = new ActionInsertInfo(getApplicationContext());
modelInsertInfo.setYearWater(yearFC);
modelInsertInfo.setMonthWater(monthFC);
modelInsertInfo.setDayWater(day1);
modelInsertInfo.setHourWater(hour);
modelInsertInfo.setMinWater(min);
long check = actionInsertInfo.updateInfo(modelInsertInfo);
if (check > 0) {
Snackbar.make(null, ":)", Snackbar.LENGTH_SHORT).show();
}else
{
Toast.makeText(ActivityDetails.this, ":(((", Toast.LENGTH_SHORT).show();
}
when select button return -1 and i use external database
thanks for help me :)

android - How to update and delete only one row in SQLite Database using where arguments?

So I am trying to delete only one instance of values from my SQLite database using the where args of db.update and db.delete in my app but it seems to delete and update all the matching ones. I only want to update or delete one. How do I do it? I tried using cursors but my implementation seems wrong. Help?
public long updateData(String oldFood, String oldCalorie, String newFood, Float newCalorie, String newDate) {
SQLiteDatabase db = helper.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(helper.FOOD_COLUMN, newFood);
contentValues.put(helper.CALORIE_COLUMN, newCalorie);
contentValues.put(helper.DATE, newDate);
String[] columns = {helper.UID, helper.FOOD_COLUMN, helper.CALORIE_COLUMN, helper.DATE};
//long id = db.update(helper.TABLE_NAME, contentValues, helper.FOOD_COLUMN + " = ? AND " + helper.CALORIE_COLUMN + " = ?", new String[]{oldFood, oldCalorie});
Cursor cursor = db.query(helper.TABLE_NAME, columns, null, null, null, null, null);
Information current = new Information();
int uidIndex = cursor.getColumnIndex(helper.UID);
int cid = cursor.getInt(uidIndex);
int foodIndex = cursor.getColumnIndex(helper.FOOD_COLUMN);
int calorieIndex = cursor.getColumnIndex(helper.CALORIE_COLUMN);
float oldCalorieFloat = Float.parseFloat(oldCalorie);
if (oldFood == cursor.getString(foodIndex) && oldCalorieFloat == cursor.getFloat(calorieIndex)) {
long id = db.update(helper.TABLE_NAME, contentValues, helper.FOOD_COLUMN + " = ? AND " + helper.CALORIE_COLUMN + " = ?", new String[]{oldFood, oldCalorie});
db.close();
return id;
}
long id=0;
return id;
}

How to add data in android sqlite?

I want to add my new values in the previous one at a specific id, but every time when I want to add it created data on a new id rather than added with the previous one.
Here is the method of my databasehelper class:
public boolean addalldata(String value1, String value2, String value3) {
String previous1 = null,previous2 = null,previous3= null;
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
String myquery = "SELECT * FROM student_table WHERE id = 1";
Cursor res = db.rawQuery(myquery, null);
while(res.moveToNext()) {
previous1 = res.getString(1);
previous2 = res.getString(2);
previous3 = res.getString(3);
}
contentValues.put(COL_2, value1 + previous1);
contentValues.put(COL_3, value2 + previous2);
contentValues.put(COL_4, value3 + previous3);
long result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1)
return false;
else
return true;}
here is the method of mainactivity class
private void AddAllData() {
btnaddall.setOnClickListener(
new View.OnClickListener() {
#Override
public void onClick(View v) {
boolean isInserted = myDb.addalldata(edit1.getText().toString(),
edit2.getText().toString(),
edit3.getText().toString());
if (isInserted = true)
Toast.makeText(MainActivity.this, "Data Inserted", Toast.LENGTH_LONG).show();
else
Toast.makeText(MainActivity.this, "Data not Inserted", Toast.LENGTH_LONG).show();
}
}
);
}
try this:
public boolean addalldata(String value1, String value2, String value3) {
String previous1 = "0",previous2 = "0",previous3= "0";
long result = 0;
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
String myquery = "SELECT * FROM student_table WHERE id = 1";
Cursor res = db.rawQuery(myquery, null);
if(res.getCount()>0){
res.moveToFirst();
previous1 = res.getString(1);
previous2 = res.getString(2);
previous3 = res.getString(3);
try{
contentValues.put(COL_2, Integer.parseInt(value1) + Integer.parseInt(previous1));
contentValues.put(COL_3, Integer.parseInt(value2) + Integer.parseInt(previous2));
contentValues.put(COL_4, Integer.parseInt(value3) + Integer.parseInt(previous3));
result = db.update(TABLE_NAME, contentValues, "id = ?", new String[]{"1"});
}catch(Exception e){
result = -1;
}
if (result == -1)
return false;
else
return true;
}else{
contentValues.put(COL_2, value1);
contentValues.put(COL_3, value2);
contentValues.put(COL_4, value3);
result = db.insert(TABLE_NAME, null, contentValues);
if (result == -1)
return false;
else
return true;
}
}
if there is no record in the database then insert a new record using db.insert otherwise update the existing record by using db.update
try res.moveToFirst().This will move your cursor to first position so that furthur checking starts from the first row.

Update Database comment return 1 but there is no update

Well I am updating my database with this code:
ContentValues newValues = new ContentValues();
newValues.put("tamam", true);
SQLiteDatabase db = mContext.openOrCreateDatabase(DB_NAME,
Context.MODE_WORLD_WRITEABLE, null);
return db.update(channel, newValues, "id =" + id , null);
it returns 1;
this is my return code:
if(msoru.isSolved()!= true){
TestAdapter mDbHelper = new TestAdapter(getActivity());
mDbHelper.createDatabase();
mDbHelper.open();
int truefalse = mDbHelper.updatecheck(mNum2);
boolean truefalse2 = msoru.isSolved();
but if I try to read new value it shows old value. I think there is no change
At the end of the database update method I should update also the my arrayclass with this
myArrayclass.get(mContext).setsolved(id);
db.close();
at last it look like this:
public void updatecheck(int id) {
ContentValues newValues = new ContentValues();
int ok = 1;
String sutun = "tamam";
newValues.put(sutun, ok);
SQLiteDatabase db = mContext.openOrCreateDatabase(DB_NAME,
Context.MODE_PRIVATE, null);
db.update(tablename, newValues, "id = ?",
new String[] { String.valueOf(id) });
myArrayclass.get(mContext).setsolved(id);
db.close();
}

Android db.update SQLite

I have a small problem with the method db.update. I need to change a string that corresponds to that received by the query. For example from the query I get the string "hello", if the change in "hello1" must change all the strings "hello".
In my Cursor I have name_s = c.getString(3);
And this is my update:
cv.put(Table1.ABC, Ecia.getText().toString());
db.update(Table1.TABLE_NAME, cv, Table1.ABC+ " = ?", new String[] { name_s});
try this :
String newval=Ecia.getText().toString();
String name_s = c.getString(3);
setMyField(name_s , newval);
public int setMyField(String currvalue , String newvalue) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Table1.ABC, newvalue);
// updating row
return db.update(Table1.TABLE_NAME, values, Table1.ABC + " = ?",
new String[] { currvalue });
}

Categories

Resources