I m trying to delete the row from input for GoalWeight, Goal Date and Current Weight. It is not erroring but the row is not getting delete.
DeleteLayout
Button deleteB;
DatabaseHelper myDB;
EditText goalD;
EditText goalW;
EditText currentW;
Intent j;
protected void onCreate(#Nullable Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.deletelayout);
deleteB = findViewById(R.id.buttonDelete);
myDB = new DatabaseHelper(this);
deleteB.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
goalD = findViewById(R.id.goaldinput2);
goalW = findViewById(R.id.goalwinput3);
currentW = findViewById(R.id.currentWinput2);
if(goalD.length() !=0 && goalW.length() !=0 && currentW.length() !=0){
Toast.makeText(DeleteLayout.this,"Goal Date: " +goalD.getText().toString()+ "Goal Weight: " + goalW.getText().toString()+
"Current Weight: " +currentW.getText().toString()+ " entered.",
Toast.LENGTH_LONG).show();
myDB.deleteContent(goalD.getText().toString(), goalW.getText().toString(), currentW.getText().toString());
j = new Intent(DeleteLayout.this, historyActivity.class);
startActivity(j);
}
else{
Toast.makeText(DeleteLayout.this,"All data not entered.", Toast.LENGTH_LONG).show();
}
}
});
Delete method in DatabaseHelper
public void deleteContent(String goalDate, String goalWeight,String currentWeight){
SQLiteDatabase db = this.getWritableDatabase();
String query = " DELETE FROM " + TABLE_NAME + "WHERE GDATE = "+
goalDate + " AND " + "GWEIGHT = " + goalWeight + " AND " + "CWEIGHT = " + currentWeight ;
}
The main problem (not the only one) is that inside deleteContent(), although you create the SQL statement, you never execute it by say execSQL().
So no attempt is made to delete any row.
However you should change so to use the delete() method, like this:
public int deleteContent(String goalDate, String goalWeight, String currentWeight){
SQLiteDatabase db = this.getWritableDatabase();
String where = "GDATE = ? AND GWEIGHT = ? AND CWEIGHT = ?";
int rows = db.delete(TABLE_NAME, where, new String[] {goalDate, goalWeight, currentWeight});
db.close();
return rows;
}
This is the recommended way, because it is safe and it also returns the number of rows affected/deleted (which you can examine to check if the deletion was successful).
So you can use it like this:
int rows = myDB.deleteContent(
goalD.getText().toString(),
goalW.getText().toString(),
currentW.getText().toString()
);
and in the variable rows you will have the number of the deleted rows.
Related
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 +"=?";
I am trying to repeat certain data from a table in a ListView with one row changing each iteration of the item. I want to have a user input an int of the times to repeat and press a button to update the list.
I am working with dates as the updating item. So if a user inputs '3', I want 3 rows added to the list, and inserted into the table, adding 1 month to the date for the 3 months.
Main Activity
planAheadButton.setOnClickListener(new View.OnClickListener() {
#Override
public void onClick(View v) {
if (planAheadMonths.getText().toString().trim().isEmpty()) {
Toast.makeText(getBaseContext(), "Add months to plan by", Toast.LENGTH_SHORT).show();
} else {
int plan = Integer.parseInt(planAheadMonths.getText().toString());
mDBHelper.updateCheckingPlanner(plan, mChecking.getCheckingId());
planAheadMonths.setText("");
mDBHelper.getTransRepeater(plan, mChecking.getCheckingId());
}
}
});
DBHelper
public void getTransactionRepeater(int repeat, int id) {
SQLiteDatabase db = this.getWritableDatabase();
String whereclause = TRANSACTION_DATE + " BETWEEN " + getCurrentMonth() + " AND " + getFutureMonth() + " AND " +
TRANSACTION_CHECKING_ID + " = " + id + " AND " + TRANSACTION_REPEAT + " != 'NONE' ";
String[] whereargs = new String[1];
Cursor res = db.query(TABLE_TRANSACTIONS, null, whereclause, null, null, null, TRANSACTION_DATE + " ASC, " + TRANSACTION_ID);
db.beginTransaction();
while (res.moveToNext()) {
if (res.getString(res.getColumnIndex(TRANSACTION_REPEAT)).equals("MONTH")) {
for (int i = 0; i < repeat; i++) {
+ i);
ContentValues cv = new ContentValues();
cv.put(TRANSACTION_NAME, res.getString(res.getColumnIndex(TRANSACTION_NAME)));
cv.put(TRANSACTION_TYPE, res.getString(res.getColumnIndex(TRANSACTION_TYPE)));
cv.put(TRANSACTION_DATE, sdf.format(cFuture.getTime()) + res.getString(res.getColumnIndex(TRANSACTION_NAME)) + i);
cv.put(TRANSACTION_AMOUNT, res.getInt(res.getColumnIndex(TRANSACTION_AMOUNT)));
cv.put(TRANSACTION_NOTES, res.getString(res.getColumnIndex(TRANSACTION_NOTES)));
cv.put(TRANSACTION_REPEAT, res.getString(res.getColumnIndex(TRANSACTION_REPEAT)));
cv.put(TRANSACTION_CHECKING_ID, res.getInt(res.getColumnIndex(TRANSACTION_CHECKING_ID)));
cv.put(TRANSACTION_NEW_BALANCE, res.getInt(res.getColumnIndex(TRANSACTION_NEW_BALANCE)));
cv.put(TRANSACTION_CREDIT_ID, res.getString(res.getColumnIndex(TRANSACTION_CREDIT_ID)));
whereargs[0] = res.getString(res.getColumnIndex(TRANSACTION_DATE));
db.insert(TABLE_TRANSACTIONS, null, cv);
}
}
}
res.close();
db.setTransactionSuccessful();
db.endTransaction();
}
I have this code:
{
String SQL_CREATE_BOOKS_TABLE = "CREATE TABLE " + BooksContract.BooksEntry.TABLE_NAME + " ("
+ BooksContract.BooksEntry._ID + " INTEGER PRIMARY KEY AUTOINCREMENT, "
+ BooksContract.BooksEntry.COLUMN_BOOKS_PRODUCT + " TEXT NOT NULL, "
+ BooksContract.BooksEntry.COLUMN_BOOKS_PRICE + " DECIMAL NOT NULL DEFAULT 0, "
+ BooksContract.BooksEntry.COLUMN_BOOKS_QUANTITY + " INTEGER NOT NULL, "
+ BooksContract.BooksEntry.COLUMN_BOOKS_SUPPLIER + " TEXT, "
+ BooksContract.BooksEntry.COLUMN_BOOKS_PHONE + " INTEGER );";
db.execSQL(SQL_CREATE_BOOKS_TABLE);
}
Even though I didn't set the supplier to be not null, if I don't type it in my editText when I click the save button my app crashes. Also even though quantity is set to default 0, if I don't type any quantity it still crashes. why?
EditorActivity:
private void insertBooks() {
String productString = productName.getText().toString().trim();
String priceString = price.getText().toString().trim();
int price = Integer.parseInt(priceString);
String quantityString = quantity.getText().toString().trim();
int quantity = Integer.parseInt(quantityString);
String supplierString = supplier.getText().toString().trim();
String phoneString = supplierPhone.getText().toString().trim();
int phone = Integer.parseInt(phoneString);
BooksDbHelper dbHelper = new BooksDbHelper(this);
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_PRODUCT, productString);
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_PRICE, priceString);
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_QUANTITY, quantityString);
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_SUPPLIER, supplierString);
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_PHONE, phoneString);
long newRowId = db.insert(BooksContract.BooksEntry.TABLE_NAME, null, values);
if (newRowId == -1) {
// If the row ID is -1, then there was an error with insertion.
Toast.makeText(this, "Error with saving book", Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(this, "Book saved with row id: " + newRowId, Toast.LENGTH_SHORT).show();
}
}
CatalogActivity:
private void insertBooks() {
SQLiteDatabase db = dbHelper.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_PRODUCT, "Walks with men");
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_PRICE, 10.00);
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_QUANTITY, 2);
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_SUPPLIER, "Amazon");
values.put(BooksContract.BooksEntry.COLUMN_BOOKS_PHONE, 727213658);
long newRowId = db.insert(BooksContract.BooksEntry.TABLE_NAME, null, values);
}
Use this code in your insertBooks() method to avoid NumberFormatException:
String productString = productName.getText().toString().trim();
String priceString = price.getText().toString().trim();
double price = 0.0;
try {
price = Double.parseDouble(priceString);
} catch (NumberFormatException e) {
e.printStackTrace();
}
String quantityString = quantity.getText().toString().trim();
int quantity = 0;
try {
quantity = Integer.parseInt(quantityString);
} catch (NumberFormatException e) {
e.printStackTrace();
}
String supplierString = supplier.getText().toString().trim();
String phoneString = supplierPhone.getText().toString().trim();
Why is phone INTEGER?
Note:
The create table code you posted is probably part of the onCreate() method in your class that extends SQLiteOpenHelper.
The onCreate() method was executed the 1st time you ran the app and created the table. Since then you may have altered the columns either by type or name, or even inserted or deleted columns, but all these changes were made only in your code and not in the table. The onCreate() method has never again been triggered.
So if any of the above applies to you:
Uninstall the app from the emulator/device so that the database is deleted and run again your app to recreate it as it should be.
I'm trying to load a database column with a cursor result for my quiz app. The reason for this is that i want to populate a list view with the questions in a each category so i set up this:
public void putValues(){
for (int i = 0; i < 18; i++) {
Cursor contentCursor = null;
contentCursor = mDB
.rawQuery("select count(*) from questions_en where used = 0 and category" + " = " + i, null);
if(contentCursor.getCount() >0 )
contentCursor.moveToFirst();
if (contentCursor.isAfterLast()) {
contentCursor.close();
mDB.close();
return;
}
int contentCursorInt = contentCursor.getInt(0);
Cursor upateCursor = null;
upateCursor = mDB.rawQuery("update categories_en set questions_count" + " = " + contentCursorInt + " where " + "_id" + " = " + i, null);
upateCursor.moveToNext();
upateCursor.close();
contentCursor.close();
}
}
so that when the user clicks an answer (on the question screen) used becomes 1(or any non-zero value) the query result changes. The above code works fine the very first time. Because i haven't set up the question screen, i added this query:
public void test(){
Cursor cus = mDB.rawQuery("update questions_en set used = 1 where category = 2 and _id = 146", null);
cus.close();
}
to my DB Adapter and then called this method from my MainActivty
#Override
public void onClick(View v) {
TestAdapter mTest = new TestAdapter(MainActivity.this);
mTest.createDatabase();
mTest.open();
mTest.test();
Log.d(DBHelper.TAG, " Worked ");
mTest.close();
}
});
But when i click on this and go to my ListActivity I expected the value of category 2 to have changed since the query had just been carried out again. But it doesn't reduce. I pulled out my DB from DDMS(file explorer) and i found out that the query to _id = 146 actually didn't change used to 1. Any help on what may be the cause?
Solve the problem with the help of this.
I just changed this:
public void test(){
Cursor cus = mDB.rawQuery("update questions_en set used = 1 where category = 2 and _id = 146", null);
cus.close();
}
to this
public void test(){
int id = 3;
ContentValues data = new ContentValues();
data.put(DBHelper.KEY_USED, "1");
mDB.update(DBHelper.KEY_QUESTIONS_TABLE, data, DBHelper.KEY_ID + " = " + id , null);
}
I am trying, unsucessfully, to query my database to find the maximum 'area number', in my areas table for a certain inspection, so that I can set the text in a form to the next area number.
The database table consists of four columns; _id, inpsection_link, area_number, area-reference.
I have created the following in my database helper class (using this post as a guide: SQLiteDatabase.query method):
public int selectMaxAreaNumber (long inspectionId) {
String inspectionIdString = String.valueOf(inspectionId);
String[] tableColumns = new String[] {
AREA_NUMBER,
"(SELECT max(" + AREA_NUMBER + ") FROM " + AREAS_TABLE + ") AS max"
};
String whereClause = INSPECTION_LINK + " = ?";
String[] whereArgs = new String[] {
inspectionIdString
};
Cursor c = rmDb.query(AREAS_TABLE, tableColumns, whereClause, whereArgs,
null, null, null);
int maxAreaNumber = c.getColumnIndex("max");
return maxAreaNumber;
}
Which I then call in the areaEdit class as follows:
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
rmDbHelper = new RMDbAdapter(this);
rmDbHelper.open();
Intent i = getIntent();
inspectionId = i.getLongExtra("Intent_InspectionID", -1);
areaId = i.getLongExtra("Intent_AreaID", -1);
if (areaId == -1) {
nextAreaNumber = rmDbHelper.selectMaxAreaNumber(inspectionId) + 1;
Toast.makeText(getApplicationContext(), String.valueOf(nextAreaNumber),
Toast.LENGTH_LONG).show();
}
setContentView(R.layout.edit_area);
setUpViews();
populateFields();
setTextChangedListeners();
}
However, it just returns 1 everytime (even if there are numbers higher than that stored in the database).
Confused.com!! Any help much appreciated.
Your issue is here :
int maxAreaNumber = c.getColumnIndex("max");
You're getting the column index of max, which is 1 because you only have one column in your query.
Instead, do something like this :
int maxAreaNumber = 0;
if(c.moveToFirst())
{
maxAreaNumber = c.getInt(1);
// or cleaner
maxAreaNumber = c.getInt(c.getColumnIndex("max"));
}
else
// no data in cursor