I am Creating DB with three fields name,id,mark. I created table and inserted values using
StudentDb.addStudentDetail(new StudentDetails(id,Name,0));
I used Update method Like:
public int updateStudentDetail(StudentDetails student) {
SQLiteDatabase db = this.getWritableDatabase();`enter code here`
ContentValues values = new ContentValues();
values.put("id", student.getID());
values.put("name", student.getName());
values.put("mark", student.getMark());
int i = db.update(TABLE_STUDENT, values, KEY_ID + " = ?",
new String[] { String.valueOf(student.getId()) });
db.close();
return i;
}
I call this method Like:
StudentDb.updateStudentDetail(student);
I don't know how to pass a value update method?
its very simple. Lets say you want to update the marks of a student. Implement this in your DatabaseHelper class:
public int udpateStudentMarks(final int studentID,final int marks){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put("mark", marks);
int i = db.update(TABLE_STUDENT, values, KEY_ID + " = ?",
new String[] { String.valueOf(studentID) });
db.close();
return i;
}
now you can use it like this:
db.updateStudentMarks(studID,newMarks);
Hope it helps.
Related
I'm working on a project and in need to use new ID's added to my database as numbers,
How do I turn this:
String query = "SELECT * FROM " + playlistsDBName + " WHERE ID = LAST_INSERT_ROWID";
To an integer (or some other number type) that I can use in my Android code?
you should change this your method from:
public void addNewPlaylist(String DBName, String playlistName) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("PlaylistName", playlistName);
db.insert(DBName, null, values);
db.close();
}
to
public int addNewPlaylist(String DBName, String playlistName) {
SQLiteDatabase db = getWritableDatabase();
ContentValues values = new ContentValues();
values.put("PlaylistName", playlistName);
final int id = db.insert(DBName, null, values);
db.close();
return (int)id;
}
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();
}
This method is supposed to update the friend in the database. But it is not. No error is shown, tried restarting to refresh the list etc still nothing. Log is showing me new values but they are not saved in the database.
public void updateFriend(Friend friend) {
Log.d("UpdateFriendMethod", friend.toString());
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, friend.getName());
values.put(KEY_BIRTHDAY, friend.getBirthday().getTimeInMillis());
db.update(TABLE_Friends,
values,
KEY_NAME + " = ?",
new String[] { String.valueOf(friend.getName()) });
db.close();
}
Here is the way how do I get items from db:
public List<Friend> getAllFriends() {
List<Friend> Friends = new LinkedList<Friend>();
String query = "SELECT * FROM " + TABLE_Friends;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(query, null);
if (cursor.moveToFirst()) {
do {
Friend friend = new Friend();
friend.setName(cursor.getString(0));
Date date = new Date(cursor.getLong(1));
GregorianCalendar cal = new GregorianCalendar();
cal.setTime(date);
friend.setBirthday(cal);
Friends.add(friend);
} while (cursor.moveToNext());
}
Log.d("getAllFriends()", Friends.toString());
return Friends;
}
I bet, problem is somewhere in that SQL statement, I am not familiar with it yet, I am used to SQL language not this.
SOLVED, problem was in SQL statement as I later found out:
public void updateFriend(Friend friend) {
Log.d("UpdateFriendMethod", friend.toString());
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_NAME, friend.getName());
values.put(KEY_BIRTHDAY, friend.getBirthday().getTimeInMillis());
db.update(TABLE_Friends, values, KEY_NAME + "='" + friend.getName()
+ "'", null);
db.close();
}
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 });
}
Hi I want to update an entry in my DB , I am giving my codes for the updateEntry function below..I want to update the password field I have tried something but it is not working
public String updateEntry(String Password) {
// Create object of content values
ContentValues updatedValues = new ContentValues();
// Assign values for each item
// updatedValues.put("USERNAME", User_name);
updatedValues.put("PASSWORD", Password);
String where = "PASSWORD=?";
db.update("LOGINDETAILS", updatedValues, where,
new String[] { Password });
return Password;
}
and this is the code I have written to update the entry :
String Passwordnew =loginDataBaseAdapter.updateEntry(Confirm_password);
Passwordnew=Confirm_password;
where I want update the password in DB with the confirm_password. I need some good suggestions.
public int UpdateContact(int id,String username,String password) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(USERNAME, username);
values.put(PASSWORD, password);
// updating Row
return db.update(LOGINDETAILS, values, KEY_ID + " = ?",
new String[] { id });
}
call this database function to your Activity
db.UpdateContact("1","dhaval","1234");
public int updateProfile(GetSet profile) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(ID, profile.getUniqueID());
values.put(NAME, profile.getName());
values.put(EMAIL, profile.getEmail());
values.put(DOB, profile.getDob());
values.put(PHONE, profile.getPhone());
values.put(PLACE, profile.getPlace());
// db.close();
// updating row
return db.update(TABLE_NAME, values, ID + " = ?",
new String[] { profile.getUniqueID() });
}
In the corresponding activity:
RemoteDatabase remote = new RemoteDatabase(this);
GetSet profile;
profile = new GetSet(setname, seteid, setphone, setplace,
setdob);
profile.setUniqueID(sdumid);
remote.updateProfile(profile);
remote.close();
Your Logic needs correction, Don't use Password for where clause, use field that is the primary key for your database, maybe username or an auto-increment id.
Then you can update using following code:
public boolean update(int id,String username,String password)
{
ContentValues cv = new ContentValues();
String where = id+"=?";
cv.put(USERNAME, username);
cv.put(PASSWORD, password);
return db.update(TABLE_NAME, cv, where,new int[] { id })>0;
}