I want to update an entry in my database - android

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;
}

Related

How to update a variable number of columns of a sqlite row in Android with edittexts

If I use the following code
public boolean updateContact(Integer id, String name, String surname, byte[] image)throws SQLiteException
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put("MyName", name);
contentValues.put("MySurname", surname);
contentValues.put("Photo", image);
db.update("details", contentValues, "_id = ? ", new String[] { Integer.toString(id) } );
return true;
}
then If I ommit to add one column it saves null values , How I can create an update only with those fields that are not empty?
Is it possible to add
db.update("details", contentValues, "_id = ? OR name=? OR surname=? ", new String[] { Integer.toString(id) } );
thank you
Is there any variable that is always set, like id? If so, you just have to check variables name and surname against null before adding them to contentValues:
if (name != null && !name.isEmpty()) {
contentValues.put("MyName", name);
}

Implementing WHERE in an SQLite INSERT command - Android Studio

I'm familiar with Oracle SQL/PL, but not with SQLite, I have managed to construct my database, with an addition operation. I was however wondering how I could implement a WHERE condition for adding data later on to a specific row.
Here is my addition code:
public void putInformation(DatabaseOperations dop, String name, String pass, String email){
SQLiteDatabase SQ = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.USER_NAME, name);
cv.put(TableData.TableInfo.USER_PASS, pass);
cv.put(TableData.TableInfo.USER_EMAIL, email);
long k = SQ.insert(TableData.TableInfo.TABLE_NAME,null, cv);
Log.d("Database Operations", "One Raw Inserted");
}
I think you are looking for an UPDATE query:
public void updateInformation(DatabaseOperations dop, String name, String pass, String email){
SQLiteDatabase SQ = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.USER_NAME, name);
cv.put(TableData.TableInfo.USER_PASS, pass);
cv.put(TableData.TableInfo.USER_EMAIL, email);
long k = SQ.update(TableData.TableInfo.TABLE_NAME, cv, TableData.TableInfo.USER_NAME+"=?",new String[]{name});
Log.d("Database Operations", k+" Rows Updated");
}
SQLiteDatabase.update docs
WHERE condition for adding data later on to a specific row. If I understand correctly then it will be update for sqlite database. For that you can refer below code.
public void updateInformation(DatabaseOperations dop, String name, String pass, String email){
SQLiteDatabase SQ = dop.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(TableData.TableInfo.USER_NAME, name);
cv.put(TableData.TableInfo.USER_PASS, pass);
cv.put(TableData.TableInfo.USER_EMAIL, email);
long index = SQ.update(TableData.TableInfo.TABLE_NAME, cv, TableData.TableInfo.USER_NAME+"=?", new String[] {name});
}
You can't use WHERE condition on insert query. WHERE condition for adding data later on to a specific row. For this you can use UPDATE query.
LINKS:
http://www.tutorialspoint.com/sqlite/sqlite_update_query.htm
http://android-er.blogspot.in/2011/06/edit-row-in-sqlite-database-using.html

turning SQLite outcome to integer - Android

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;
}

Update Individual field in DB

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.

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