I am trying to update a row in SQLite on android using the update(String table, ContentValues values, String whereClause, String[] whereArgs) method. I have seen many sources on this cite and else where around the web describing how to do so but it does not seem to be working. My main problem is my lack of understanding of the last two variables that are passed into the method do and how to properly set them up.
String whereClause: What does this do? Passing null, changes all the values in a column but what if you wanted to just switch one row at for example row "x" in the sql database; how would that be done? String[] whereArgs: What does this do?
Code
//how do you edit variable NOTE_TEXT at pos to be editedNote
public void editNotes(int pos, String editedNote){
SQLiteDatabase db = helper.getWritableDatabase();
String postion = Integer.toString(pos);
ContentValues newContentValues = new ContentValues();
newContentValues.put(NoteDBHelper.NOTE_TEXT, editedNote);
db.update( "noteTable", newContentValues, NoteDBHelper.NOTE_TEXT + " = " + postion, null);
}
static class NoteDBHelper extends SQLiteOpenHelper {
private static final String DATABASE_NAME = "noteDB";
private static final int DATABASE_VERSION = 5;
private static final String TABLE_NAME = "noteTable";
private static final String UID = "_id";
private static final String NOTE_TEXT = "note";
private static final String DROP_TABLE = "DROP TABLE IF EXISTS " + TABLE_NAME + "";
/* private static final String CREATE_TABLE = "CREATE TABLE "+ TABLE_NAME +
" (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NOTE_TEXT +
" VARCHAR(255), );";*/
private static final String CREATE_TABLE = "CREATE TABLE " + TABLE_NAME + " (" + UID + " INTEGER PRIMARY KEY AUTOINCREMENT, " + NOTE_TEXT + " VARCHAR(255) );";
public NoteDBHelper(Context context) {
super(context, DATABASE_NAME, null, DATABASE_VERSION);
}
@Override
public void onCreate(SQLiteDatabase db) {
try {
db.execSQL(CREATE_TABLE);
} catch (SQLiteException e) {
e.printStackTrace();
}
}
@Override
public void onUpgrade(SQLiteDatabase db, int oldVersion, int newVersion) {
try {
db.execSQL(DROP_TABLE);
onCreate(db);
} catch (SQLException e) {
e.printStackTrace();
}
}
}
Aucun commentaire:
Enregistrer un commentaire