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
Related
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 the row values with 0 or 1. Here is my update method.Where is wrong with my code .
dbhelper.updateStarFlag(actid, strStarFlagMark);
public boolean updateStarFlag(String str_ActivityId, String str_StarFlag)
{
SQLiteDatabase db1 = getReadableDatabase();
ContentValues args = new ContentValues();
args.put(COL_AllPost_PostActivityId, str_ActivityId);
args.put(COL_AllPost_StarFlag, str_StarFlag);
int i = db1.update(Table_AllPost_Table, args, COL_AllPost_PostActivityId + "='"+str_ActivityId+"'", null);
return i > 0;
}
If you want to update something, you need a writable database. I changed the query to look a little more like in the documentation for SQLiteDatabase
public boolean updateStarFlag(String str_ActivityId, String str_StarFlag)
{
SQLiteDatabase db1 = getWriteableDatabase();
ContentValues args = new ContentValues();
args.put(COL_AllPost_PostActivityId, str_ActivityId);
args.put(COL_AllPost_StarFlag, str_StarFlag);
int i = db1.update(Table_AllPost_Table, args, COL_AllPost_PostActivityId + "= ? ", new String[]{str_ActivityId});
return i > 0;
}
To update something in database, you need writeable database, not just readable.
Change below line:
SQLiteDatabase db1 = getReadableDatabase();
to :
SQLiteDatabase db1 = getWriteableDatabase();
Hope, this helps you.
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.
This works to Insert a Row
public long insertRows(Double lat, Double log, String mdate,
String mycomment, String name) {
ContentValues value=new ContentValues();
value.put(COLUMN2,lat);
//System.out.println(COLUMN2+lat);
value.put(COLUMN3, log);
//System.out.println(COLUMN3+log);
value.put(COLUMN4, mdate);
//System.out.println(COLUMN4+mdate);
value.put(COLUMN5, mycomment);
//String mycomment = null;
//System.out.println(COLUMN5+mycomment);
value.put(COLUMN6, name);
return db.insert(TABLENAME,null,value);}
But I am unable to Delete a row programmatically.
I can provide more code or info if needed any help is greatly appreciated
this is what Im useing so far seems intemitant though
and Im still haveing issues updateing the listactivity
public boolean deleteEntry(long Id) {Id = GpsListactivity.rowid;
System.out.println("GPSDATABASE "+ TABLENAME + KEY_ROWID + "=" +Id);
return db.delete(TABLENAME,KEY_ROWID + "="+Id,null) > 0;}
// this is the update method
public boolean update(long Id, Double lat, Double log,
String mdate,String mycomment, String name) {
ContentValues updateValues = createContentValues(
lat,log,mdate,mycomment,name);
return db.update(TABLENAME, updateValues,"locationId=?" +Id,null) > 0;
You can use the method delete(String table, String whereClause, String[] whereArgs). For example:
String whereClause = "COLUMN4 = 'value'";
db.delete(TABLENAME, whereClause, null);
To delete a specific row, you will have to know information about that row which makes it unique (for example the row's primary key or a combination of data in its columns).
You can also reference the SQLiteDatabase documentation for more information.
is there a way to change my function:
public categorie createCategoria(String categoria) {
ContentValues values = new ContentValues();
values.put(MySQLiteHelper.COLUMN_NOME, categoria);
values.put(MySQLiteHelper.COLUMN_PREF, 0);
long insertId = database.insert(MySQLiteHelper.TABLE_CATEGORIE, null,
values);
Cursor cursor = database.query(MySQLiteHelper.TABLE_CATEGORIE,
allCategorieColumns, MySQLiteHelper.COLUMN_ID + " = " + insertId, null,
null, null, null);
cursor.moveToFirst();
categorie newCategoria = cursorToCategorie(cursor);
cursor.close();
return newCategoria;
}
this is a raw insert, i would like to change this function to make it update or insert accordingly. i would like to change this becouse i'm already using this function in some places, but now i need to choose if insert a row or update (or ignoring the insert) a row with the same COLUMN_NOME. can someone help me doing this?
i mean i would like to insert a new row ONLY if there isn't another with the same name (as usual you know).
You can use insertWithOnConflict() if you want to insert or update, depending in whether the record exists or not:
SQLiteDatabase db = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(COLUMN_ID, id);
contentValues.put(COLUMN_VALUE, value);
// this will insert if record is new, update otherwise
db.insertWithOnConflict(TABLE, null, contentValues, SQLiteDatabase.CONFLICT_REPLACE);
you could call int nRowsEffected = database.update(...); if there are no rows effected by the update either the row doesn't exist (or you hosed your update()!) therefore you need to call database.insert(...). of course if nRowsEffected > 0 then you are done.
You can use execSQL and use INSERT OR REPLACE
String[] args = {"1", "newOrOldCategory"}; // where 1 is the category id
getWritableDatabase().execSQL("INSERT OR REPLACE INTO table_name (idColoumn, categoryColumn) VALUES (?, ?)", args);
First of all you have write function which is check whether id is exists in particular Table like:
/**
* #param table_name
* #param server_id
* #return
*/
public boolean isServerIdExist(String table_name, int server_id) {
long line = DatabaseUtils.longForQuery(mDB, "SELECT COUNT(*) FROM " + table_name + " WHERE id=?",
new String[]{Integer.toString(server_id)});
return line > 0;
}
You have to pass table_name and id in that like
/**
* INSERT in TABLE_ACCOUNT_DEVICE
**/
public long insertOrUpdateAccountDevice(int server_id, int account_id,
String device_name, String device_id,
String last_active, String itp,
String utp, int status) {
ContentValues values = new ContentValues();
values.put(ACCOUNT_DEVICE_ACCOUNT_ID, account_id);
values.put(ACCOUNT_DEVICE_DEVICE_NAME, device_name);
values.put(ACCOUNT_DEVICE_DEVICE_ID, device_id);
values.put(ACCOUNT_DEVICE_LAST_ACTIVE, last_active);
values.put(ACCOUNT_DEVICE_ITP, itp);
values.put(ACCOUNT_DEVICE_UTP, utp);
values.put(ACCOUNT_DEVICE_STATUS, status); // 0=pending, 1=active, 2=Inactive, -1=not_found
/**
* isServerIdExists
*/
if (isServerIdExists(TABLE_ACCOUNT_DEVICE, server_id)) {
values.put(ACCOUNT_DEVICE_SERVER_ID, server_id);
return mDB.insert(TABLE_ACCOUNT_DEVICE, null, values);
} else {
return mDB.update(TABLE_ACCOUNT_DEVICE, values, ACCOUNT_DEVICE_SERVER_ID + " =? ",
new String[]{Integer.toString(server_id)});
}
}
Hope it will helps you.