i have an interplay between the MainActivity and the DatabaseAdapter class where I'm trying to access an integer variable from the MainActivity. The code as follows;
in MainActivity...
int clam = 7;
public void onClick_UpdateJ(View v) {
myDb.updateJ();
}
in DbAdapter
public class {
.....
public void updateJ(){
int grape = "coli = ?;
ContentValues arges = new ContentValues();
arges.put(coli, 7);
// Insert it into the database.
db.update(DATABASE_TABLE, arges, grape, new int[]{1});
}
}
As you can see, I'm trying to update the coli data (integer) in the database from the current 1 to 7. How do I do this? I tried shifting everything in DbAdapter to MainActivity but the 'update' method is not recognized.
Thanks in advance.
public void updateJ(int value) {
ContentValues cv = new ContentValues();
cv.put("coli", value);
String where = "coli = ?";
whereArgs = new String[] { Integer.toString(1) };
db.update(DATABASE_TABLE, cv, where, whereArgs);
}
The update method takes 4 arguments:
table (String): the table name
values (ContentValues): the values to insert
selection (String): a SQLite WHERE clause. You already know that the ? character will be replaced by values in selectionArgs.
selectionArgs (String[]): values that replace ?s in the where clause
Related
while updating in database, I'm not getting the id of particular row
here is my code for update Note in database
public int updateNote(int id, String tittle, String content , String update_at)
{
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Note.COLUMN_TITTLE,tittle);
values.put(Note.COLUMN_CONTENT,content);
values.put(Note.COLUMN_TITTLE,update_at);
return db.update(Note.TABLE_NAME,values,Note.COLUMN_ID+"="+id,null);
}
Please help me.
update returns the number of rows affected and not the id of the row.
In your case if successful it would return 1.
About the id you need you already have it.
As mTak outlined, the update method returns the number of rows affected. You can also see that in the documentation.
If you want your method to return the ID of the row you updated, then return the id that you pass in, since that's how you select the row to update in the first place:
public int updateNote(int id, String tittle, String content , String update_at) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Note.COLUMN_TITTLE,tittle);
values.put(Note.COLUMN_CONTENT,content);
values.put(Note.COLUMN_TITTLE,update_at);
db.update(Note.TABLE_NAME,values,Note.COLUMN_ID+"="+id,null);
return id;
}
In your Activity Class:
//*Note is your setterClass
//*notesList is your ArrayList or list
//*position refers your current list position
//*setNote is a method in Note Class
private void updateNote(String note, int position) {
Note n = notesList.get(position);
// updating note text
n.setNote(note);
// updating note in db
db.updateNote(n);
// refreshing the list
notesList.set(position, n);
mAdapter.notifyItemChanged(position);
}
In your database class try below code:
public int updateNote(Note note) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(Note.COLUMN_NOTE, note.getNote());
// updating row
return db.update(Note.TABLE_NAME, values, Note.COLUMN_ID + " = ?",
new String[]{String.valueOf(note.getId())});
}
here i am trying to update single string.i hope this is workig to you.Let me know is this working or not.
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 one row based on multiple row value, After run the application getting error like = ( FATAL EXCEPTION: main
android.database.sqlite.SQLiteException: near "=": syntax error (code 1): , while compiling: UPDATE Tablename SET sFollowed=?,Code=?,Name=? WHERE Vessel_Name= )
Here is my update method in MyDbhelper class
public boolean update_isFollowed(String strVesselName , String strVesselNotationNumber, String strIsFollowed) {
SQLiteDatabase db1 = getReadableDatabase();
ContentValues values=new ContentValues();
values.put(COL_VesselName,strVesselName );
values.put(COL_ClassCodeNumber , strVesselNotationNumber);
values.put(COL_IsFollowedVessels, strIsFollowed);
int id=db1.update(Table_VesselList,values,COL_ClassCodeNumber + "=" +strVesselNotationNumber +" and "+ COL_VesselName +"="+strVesselName,null);
return id > 0;
}
Do something like this
In your activity
private UpdateDataInDB mUpdateDataInDb;
mUpdateDataInDb = new UpdateDataInDB(activity);
// call the update method with updated strings
mUpdateDataInDb.updateCommentDetails(_name, heading, comment);
In Database class
public void updateCommentDetails(String name, String updatedHeading,
String updatedComment) {
// TODO Auto-generated method stub
mDatabase = mDatabase_handler.getReadableDatabase();
ContentValues args = new ContentValues();
args.put("Heading", updatedHeading);
args.put("Comment", updatedComment);
mDatabase.update("AddCommentTable", args,"Name" + "= ?", new String[]{ name });
args.clear();
}
Use proper where condition string with values as argument. like:
String where = COL_ClassCodeNumber+"=? AND "+COL_VesselName="?";
String[] whereArgs = new String[] {String.valueOf(strVesselNotationNumber),
String.valueOf(strVesselName)};
int id=db1.update(Table_VesselList,values,where,whereArgs);
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)});
I'm dealing with a simple app, which basically stores various dates. But when I enter a new date, I have to close the app and open it again. The same thing counts for changing the date values. I can only see the changes by closing the app and reopening it.
Here are the two main functions in my DB class;
public void addBirthday(Person person){ //Add a data to database
SQLiteDatabase db = this.getWritableDatabase();
ContentValues cv = new ContentValues();
cv.put(column_name, person.getName());
cv.put(column_sname, person.getSname());
cv.put(column_day, person.getDay());
cv.put(column_month, person.getMonth());
cv.put(column_year, person.getYear());
db.insert(TABLE_NAME, null, cv);
db.close();
}
public int updateBirthday(Person person) {
// 1. get reference to writable DB
SQLiteDatabase db = this.getWritableDatabase();
// 2. create ContentValues to add key "column"/value
ContentValues cv = new ContentValues();
cv.put(column_name, person.getName());
cv.put(column_sname, person.getSname());
cv.put(column_day, person.getDay());
cv.put(column_month, person.getMonth());
cv.put(column_year, person.getYear());
// 3. updating row
int i = db.update(TABLE_NAME, //table
cv, // column/value
column_id +" = ?", // selections
new String[] { String.valueOf(person.getId()) }); //selection args
// 4. close
db.close();
return i;
}
You will need to refresh the view (probably a ListView) where you display this data.
Get rid of the notifyDataSetChanged() and call requery() on your Cursor