Reputation: 197
I am trying to search one table for all values in rows with the same name
, and then place them into another table.
I have done this successfully in another part of my application, and have modeled this code after the working code, but for some reason it will not work. It only moves one value from table to table.
I have checked that the table it is taking from, DATABASE_FAV_EQUIP_TABLE
, has more than one row in it.
HERE IS THE CODE THAT IS NOT WORKING:
//takes all equip data from saved equip set, and puts into equip table
public String AddSavedEquipToEquipTable(String name) {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_EQUIP, KEY_FAVNAME};
ContentValues cv = new ContentValues();
Cursor c = ourDatabase.query(DATABASE_FAV_EQUIP_TABLE, columns, KEY_FAVNAME + "='" + name + "'", null, null, null, null);
if (c == null){
return null;
}
else if(c != null && c.moveToFirst()) {
do {
int iEquip = c.getColumnIndex(KEY_EQUIP);
String equipment = c.getString(iEquip);
cv.put(KEY_EQUIP, equipment);
ourDatabase.insert(DATABASE_EQUIP_TABLE, null, cv);
} while (c.moveToNext() && KEY_FAVNAME == name);
}
return null;
}
HERE IS THE CODE THAT IS WORKING IN ANOTHER PART OF MY APPLICATION:
//takes all rows in chest table and puts them into the chosen equipment database
public String AddToEquipmentDBFromChest(String equip) {
// TODO Auto-generated method stub
String[] columns = new String[]{KEY_REPS, KEY_EXERCISE, KEY_TIMETYPE};
ContentValues cv = new ContentValues();
Cursor c = ourDatabase.query(DATABASE_CHESTTABLE, columns, KEY_EQUIP + "='" + equip + "'", null, null, null, null);
if (c == null){
return null;
}
else if(c != null && c.moveToFirst()) {
do {
int iReps = c.getColumnIndex(KEY_REPS);
int iExercise = c.getColumnIndex(KEY_EXERCISE);
int iTimeType = c.getColumnIndex(KEY_TIMETYPE);
String reps = c.getString(iReps);
String exercise = c.getString(iExercise);
String time = c.getString(iTimeType);
cv.put(KEY_REPS, reps);
cv.put(KEY_EXERCISE, exercise);
cv.put(KEY_TIMETYPE, time);
ourDatabase.insert(DATABASE_CHOSEN_EQUIP_TABLE, null, cv);
} while (c.moveToNext() && KEY_EQUIP == equip);
}
return null;
}
I have gone over this again and again and I have to be missing something.
Upvotes: 0
Views: 63
Reputation: 60681
Neither KEY_FAVNAME
or name
change inside your function, so comparing them on each iteration of your loop is not going to do anything useful. Given that you're already checking for matching names in the query, I can't see why you are trying to do this check in your loop as well. Just take out the KEY_FAVNAME == name
comparison altogether.
Incidentally:
KEY_FAVNAME == name
This is not how you compare strings in Java. This is better:
KEY_FAVNAME.equals(name)
Even more incidentally: your code is needlessly verbose. You don't need to null-check the cursor, as an exception will be thrown if the query fails. You do, however, need to close the cursor once you're finished. A typical pattern is this:
Cursor c = db.query(....);
try {
// do all your cursor iteration in here
} finally {
c.close();
}
Upvotes: 1
Reputation: 48569
while (c.moveToNext() && KEY_FAVNAME == name);
This will break out of your do while loop as soon as the name isn't right. You don't want to break, you just want to skip that record.
Upvotes: 0