I'm currently developing an Android app. The database has 1 column only named content. Here it is the code:
public long insert(String content){
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_CONTENT, content);
return sqLiteDatabase.insert(MYDATABASE_TABLE, null, contentValues);
}
How to make an update for a specific column (according to its index) in that database?
db.update
(
MYDATABASE_TABLE, contentValues , ID_FIELD_CONST + " = ?",
new String[] { idValue }
);
You can use:
ContentValues cv = new ContentValues();
cv.put(KEY1,value1);
cv.put(KEY2,value2);
//... all the fields you want to update
String where = KEY_CONTENT + " = " + value;
db.update(table_name,cv,where,null);
KEY1,KEY2 ... are the name of the fields you want to update
value1,value2... are the new values of the fields you want to update
the where String is to tell in which row you need to update.
Related
I have a hidden database with 7 columns, one of which is a price and whose values are null. I want to enter some value in the field column for id = 1, then for id = 2 I enter another value etc. and when I fill in the price column I want to update the whole. Is that possible?
My code updates only one value
public boolean updateData(String id,String name, String number_phone, String about, Integer price, String color){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_1, id);
contentValues.put(COL_2, name);
contentValues.put(COL_3, number_phone);
contentValues.put(COL_4, about);
contentValues.put(COL_5, price);
contentValues.put(COL_6, color);
db.update(TABLE_NAME, contentValues, " ID = ?" , new String[]{id});
return true;
}
I don't quite get your question but from what i understand this could might help you out.
If you want only one field values to be updated then you need to use for loop.
In onCreate method call this with the array of prices that you want to be updated in table's each row.
Array of prices should not be greater than no. of rows exist in table.
for(int i=0;i<(no. of rows in table);i++){
updateData(rowId,priceArray[i]);
}
Now the update method will be like.
public boolean updateData(String id, Integer price){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_5, price);
db.update(TABLE_NAME, contentValues, " ID = ?" , new String[]{id});
return true;
}
If you want to update certain rows for example {1,4,5} then you have to update it one by one by calling updateData method with id and prices .
But if you want same value of price field to be updated in certain rows like above {1,4,5} than you can
public boolean updateData(Integer price){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COL_5, price);
db.update(TABLE_NAME, contentValues, " ID = ?" , new String[]{1,4,5});
return true;
}
public long createCountry(String code, String name, String continent,
String region) {
ContentValues initialValues = new ContentValues();
initialValues.put(KEY_PRODUCT_TYPE, code);
initialValues.put(KEY_PRODUCT_NAME, name);
initialValues.put(KEY_PRODUCT_QUANTITY, continent);
initialValues.put(KEY_PRODCUT_COST, region);
return mDb.insertWithOnConflict(SQLITE_TABLE, null, initialValues, SQLiteDatabase.CONFLICT_REPLACE);
}
this is for insert or Update function but its taking new Row if name X is alredy present in table row i want it should Update Like suppose we have 3 Entry whose name column value has X,Y,Z now Suppose i want call again this function with name X then rest of value Should update not its should create another Column please help me where am doing wrong .
There is no update-or-insert function.
You have to do this manually:
public void createCountry(String code, String name, String continent, String region) {
ContentValues values = new ContentValues();
values.put(KEY_PRODUCT_TYPE, code);
values.put(KEY_PRODUCT_QUANTITY, continent);
values.put(KEY_PRODCUT_COST, region);
int updated = mDb.update(SQLITE_TABLE, values,
KEY_PRODUCT_NAME + " = ?",
new String[]{ name });
if (updated == 0) {
values.put(KEY_PRODUCT_NAME, name);
mDb.insert(SQLITE_TABLE, null, values);
}
}
I want to update a field in my database, but the changes not saved to database.
I use The "update" method in database class but not work
Please
This is my Method
public void myupdatedb(int id,String value){
ContentValues cv = new ContentValues();
cv.put("fav", "1");
mydb.update("contents", cv, "id" + "=" + id , null);
}
and in the MainActivity:
db.myupdatedb(1,"1");
From Android documentation
public int update (String table, ContentValues values, String whereClause, String[] whereArgs)
You may include ?s in the where clause, which will be replaced by the values from whereArgs. The values will be bound as Strings.
Try
mydb.update("contents", cv, "id = ?", new String[]{Integer.toString(id)});
Is there an easier way I can insert data in a table in SQLite? What I need is to insert State_ID and State_Name in Table tblState. Since there are a lot of State in US, is there another way I can use instead of this:
String ROW1 = "INSERT INTO tblState VALUES (1,'Alabama')";
String ROW2 = "INSERT INTO tblState VALUES (2,'Alaska')";
String ROW3 = "INSERT INTO tblState VALUES (3,'California')";
db.execSQL(ROW1);
db.execSQL(ROW2);
db.execSQL(ROW3);
Thanks!
Try for this..
String state[] = { "state1" , "state2",.............};
int id=1;
for(int i=0;i<state.length;i++)
{
db.execSQL("INSERT INTO tblState VALUES (id,state[i])");
id++;
}
You can do the following:
String ALLROWS = "INSERT INTO tblState"
+ "SELECT 1 AS 'State_ID', 'Alabama' AS 'State_Name'"
+ "UNION SELECT 2 AS 'State_ID', 'Alaska' AS 'State_Name'"
+ "UNION SELECT 3 AS 'State_ID', 'California' AS 'State_Name'";
db.execSQL(ALLROWS);
You should use ContentValues for each of the rows you want to insert:
ContentValues values = new ContentValues();
values.put('State_ID', 1);
values.put('State_Name', "Alabama");
and then use db.insert("tblState", null, values) to insert the row. You can write a method to construct your ContentValues:
public ContentValues method(int id, String name) {
ContentValues values = new ContentValues();
values.put('State_ID', id);
values.put('State_Name', name);
}
Your code will be:
db.insert("tblState", null, method(1, "Alabama"));
db.insert("tblState", null, method(2, "Alaska"));
db.insert("tblState", null, method(3, "California"));
Or just loop through all the states...
I am new to android , as I am trying to update data using context menu item "EDIT". Using EDIT I will go to EditText layout then I will press update Note, But it's not updating the EditText Data.
My Code Database:
public void updateNote(String filename, String filedata, String duedate){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues updatenote = new ContentValues();
updatenote.put(Col_File_Name, filename);
updatenote.put(Col_File_Data, filedata);
updatenote.put(Col_Due_Date, duedate);
db.update(Table_Notepad, updatenote, Col_ID + "=?", null);
db.close();
}
Activity:
case R.id.Save:
String get_title = Title_Edit.getText().toString();
String get_content = Content_Edit.getText().toString();
String get_duedate = duedatetext.getText().toString();
if(!get_title.equals("") || !get_content.equals(""))
{
if (!isEdit) {
//if it isn't edit mode we just add a new note to db
Database_Notepad db = new Database_Notepad(Add_Task.this);
db.Create_Note(get_title, get_content, get_duedate);
} else {
//if this is edit mode, we just update the old note
Database_Notepad db = new Database_Notepad(Add_Task.this);
db.updateNote(get_title, get_content, get_duedate);
db.close();
}
}
Please Suggest, I think I need to correct database code. Please suggest. Thanks
Your function updateNote need Record ID which you want to update.
Like
public void updateNote(int id, String filename, String filedata, String duedate){
instead
public void updateNote(String filename, String filedata, String duedate){
and compare that id which your entire record, it will check and update your particular record, change your function function something like below:
public void updateNote(int id, String filename, String filedata, String duedate){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues updatenote = new ContentValues();
updatenote.put(Col_File_Name, filename);
updatenote.put(Col_File_Data, filedata);
updatenote.put(Col_Due_Date, duedate);
db.update(Table_Notepad, updatenote, "rowid= "+id,, null);// rowid or name of your id column
db.close();
}
and pass the id too along with others parameter.
Use
db.updateNote(id, get_title, get_content, get_duedate);
instead
db.updateNote(get_title, get_content, get_duedate);
In updateNode(), you need to change this update or insert values:
public void updateNote(long rowId, String filename, String filedata, String duedate){
SQLiteDatabase db = this.getWritableDatabase();
ContentValues updatenote = new ContentValues();
updatenote.put(Col_File_Name, filename);
updatenote.put(Col_File_Data, filedata);
updatenote.put(Col_Due_Date, duedate);
if (rowId >=0)
db.update(Table_Notepad, updatenote, Col_ID + "=?", new String[] {""+rowId});
else
db.insert(Table_Notepad, null, updatenote);
db.close();
}
Then, if you are creating a new note you can call:
updateNote(-1, filename, filedata, duedate);
and if you are updating an existing one, you will have to pass in the ID:
updateNote(noteRowId, filename, filedata, duedate);.
Source: check these Android docs.
Need to pass row id to update in last argument
db.update(Table_Notepad, updatenote, Col_ID + "=?", new String[]{id});
Happy coding