I'm trying to update a specific row in my database. I'm trying to update a single data with a button. But when I run the app the database is not updated and Ive not received any error. What did I do wrong?
Here's my code.
In my java class:
public void Favorite1(View view){
loginDataBaseAdapter = new LoginDataBaseAdapter(this);
loginDataBaseAdapter = loginDataBaseAdapter.open();
String favorite_1 = "FIZO MAWAR";
loginDataBaseAdapter.updateEntry(favorite_1);
loginDataBaseAdapter.close();
finish();
}
My database:
public void updateEntry(String favorite_1) {
ContentValues values = new ContentValues();
values.put("FAVORITE1", favorite_1);
db.update("LOGIN", values, "USERNAME = ?", null);
}
You do use a where clause but pass null as its arguments. See the docs for the update method. Assuming a column named "FAVORITE1" exists in the "LOGIN" table, the correct usage will be:
db.update("LOGIN", values, "USERNAME = ?", new String[]{username});
If the favorite_1 parameter already represents a username (which I tend to infer from your code), use it:
db.update("LOGIN", values, "USERNAME = ?", new String[]{favorite_1});
Related
I'm using SQlite in my Android app and the task is - how can I update all the rows in one table?
I have a 1st column with name "cl_id" (integer numbers 1-2-3-4..) and after deleting some rows, I wan't to make a cycle to fill this 1st column with a new values to keep them in right order.
I was trying to execute:
data.put("cl_id",index);
db.update("mdb_table_contactList", data, null, null);
but it's updates all the values in the first column :(
To update the field in specific row, try this;
ContentValues data = new ContentValues();
data.put("cl_id",index);
db.update("mdb_table_contactList", data, "id="+_id, null);
db.close();
It's good practice to have a primary key for each row data.
Here _id would be your primary key.
"just give rowId and type of data that is going to be update in ContentValues."
public void updateStatus(String id , int status){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues data = new ContentValues();
data.put("status", status);
db.update(TableName, data, "columnName" + " = "+id , null);
}
Try this it must work
void updateDatail(String id) {
ContentValues data = new ContentValues();
data.put("cl_id",index);
db.update("mdb_table_contactList", data, "id=?", new String[]{id});
db.close();
}
Hi guys i have code to add data into Database and that code is called on oncreate. but every time fragment is created its saves data into database again and again. I dont want that. how can i prevent that here is my code to get data and save it to database.
public void getcontacts(){
Cursor phones;
phones = getActivity().getContentResolver().query(ContactsContract.CommonDataKinds.Phone.CONTENT_URI, null,null,null, null);
while (phones.moveToNext()){
String name = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.DISPLAY_NAME));
String phoneNumber = phones.getString(phones.getColumnIndex(ContactsContract.CommonDataKinds.Phone.NUMBER));
DataBaseOperations hell = new DataBaseOperations(getActivity());
SQLiteDatabase db = hell.getWritableDatabase();
hell.SaveContacts(name, phoneNumber, db);
}
phones.close();
}
here is code for saving in database.
public void SaveContacts(String Name,String phone,SQLiteDatabase db ){
ContentValues sv = new ContentValues();
sv.put(mDatabase.Tableinfo.Contacts_name, Name);
sv.put(mDatabase.Tableinfo.Contacts_phone, phone);
db.insert(mDatabase.Tableinfo.contacts, null, sv);
}
EDIT- Here is what i have done so far but how do i set a constraint on which it will conflict.I have added an auto increment primary key as unique key but how will they conflict?? but still its saving values again in database.where do i set a primery key as constraint?????
public void SaveContacts(String Name,String phone,SQLiteDatabase db ){
ContentValues sv = new ContentValues();
sv.put(mDatabase.Tableinfo.Contacts_name, Name);
sv.put(mDatabase.Tableinfo.Contacts_phone, phone);
db.insertWithOnConflict(mDatabase.Tableinfo.contacts, null, sv, SQLiteDatabase.CONFLICT_REPLACE);
EDIT- 2 - how would i make unique columns in this kind of query???
private static final String Contacts_Table = "CREATE TABLE "+
mDatabase.Tableinfo.contacts +"("
+mDatabase.Tableinfo.Contacts_name+" TEXT,"
+mDatabase.Tableinfo.Contacts_phone+" TEXT NOT NULL UNIQUE,"
+mDatabase.Tableinfo.isChattris+" TEXT,"
+mDatabase.Tableinfo.status_contact+" TEXT,"
+mDatabase.Tableinfo.Contact_pic+" BLOB"+")";
just Added NOT NULL UNIQUE.
This is the constraint.
Keep the status of the contacts saving in unrelated storage, e.g SharedPreferences. You don't want to rely on any lifecycle triggers, not even Application.onCreate, since that will happen again and again when the app is launched.
Try to update existing entries instead of adding new ones. Decide what the database key should be, and then you can use insertWithOnConflict with the CONFLICT_REPLACE flag to insert if not already in the table or update if it is.
EDIT: to define the constraints so the conflict behavior will trigger, change your CREATE TABLE statement. e.g this will cause duplicate (name,phone) pairs to trigger it. You may want to use some kind of contact ID instead though.
CREATE TABLE mytable (
name TEXT,
phone TEXT,
UNIQUE(name, phone)
)
Hello i have MySQLite database in my android project and Im trying to update one of the coloume and it doesn't work
my code:
db source:
public void updateToken(int id, int tokens)
{
List<User> users = getAllUsers();
dbHelper.updateToken(database, id, users.get(id).getUsername(),users.get(id).getPassword(), 10);
}
SQLite helper:
epublic void updateToken(SQLiteDatabase db,int id, String username,String password,int newToken) {
ContentValues cv = new ContentValues();
cv.put(COLUMN_USERNAME, username);
cv.put(COLUMN_PASSWORD, password);
cv.put(COLUMN_TOKENS, newToken);
db.update(TABLE_USERS,cv,"_id "+"="+id,null);
}
calling the method :
dataSource.updateToken(userID,10);
As see here SQLiteDatabase.update takes whereArgs as last parameter.
So,instead of passing last param null in update method pass id value according to which want to update row value like:
String[] whereArgs = new String[] {String.valueOf(id)};
db.update(TABLE_USERS,cv,"_id=?",whereArgs);
I have the following insert statement in my app which work.
Before the insert I want to first check the database if the value name does not exist in the name column.
If it does not exists I want to continue with the insert, else display an error message.
How do I incorporate it into my existing statement below?
public void insert(HashMap<String, String> queryValues){
SQLiteDatabase database = this.getWritableDatabase();
ContentValues values = new ContentValues();
Cursor c = database.query("SELECT * FROM user_details where" + "name=?", new String[] {values.put("name") });
if (c.getCount() > 0) {
// don't do it
return;
}
else {
values.put("name", queryValues.get("name"));
values.put("age", queryValues.get("age"));
database.insert("user", null, values);
}
database.close();
}
The correct way to do this is to add a unique constraint on the name field, and then use insertWithOnConflict() with a last argument of CONFLICT_FAIL. That's actually the default behavior. Your insert will fail if it would otherwise cause a constraint violation (insert() will return -1).
If you don't want to do that, there's no magic. Query your DB for a row with the given name field. If you are returned >0 rows, don't perform the insert.
Cursor c = db.query(..., "name=?", new String[] {theName}, ...);
if (c.getCount > 0) {
// don't do it
return;
}
I have a settings tab in my application which takes an int value through edittext from user and stores it into the database. When the user now wants to change the setting the old setting should be updated in the database. How do I write the database code for that.
Below is my code to save the value in db:
DatabaseHelper dha = new DatabaseHelper(this);
SQLiteDatabase db = dha.getWritableDatabase();
Log.d("","Done with database Sqlite");
ContentValues cv = new ContentValues();
EditText editText2 = (EditText) this.findViewById(R.id.editText1);
setTime = editText2.getText().toString();
Log.d(setTime, "This is the setTime value");
cv.put(DatabaseHelper.TIME, setTime);
db.insert("finalP", DatabaseHelper.TIME, cv);
db.close();
You should do something like this:
// Create content values that contains the name of the column you want to update and the value you want to assign to it
ContentValues cv = new ContentValues();
cv.put("my_column", "5");
String where = "my_column=?"; // The where clause to identify which columns to update.
String[] value = { "2" }; // The value for the where clause.
// Update the database (all columns in TABLE_NAME where my_column has a value of 2 will be changed to 5)
db.update(TABLE_NAME, cv, where, value);