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.
Related
Im using the following code to add values to db.
public void addComplianceAcceptability(ComplianceAcceptability complianceAcceptability) {
SQLiteDatabase sqLiteDatabase = this.getWritableDatabase();
ContentValues contentValues = new ContentValues();
contentValues.put(KEY_ID, complianceAcceptability.getId());
contentValues.put(KEY_COUNTRY_ID, complianceAcceptability.getCountryId());
contentValues.put(KEY_DOMAIN_ID, complianceAcceptability.getDomainId());
contentValues.put(KEY_UNIT_ID, complianceAcceptability.getUnitId());
contentValues.put(KEY_COMPLIANCE_ID, complianceAcceptability.getComplianceId());
contentValues.put(KEY_COMPLIANCE_NAME, complianceAcceptability.getCompliancenName());
contentValues.put(KEY_COMPLIANCE_FREQUENCY, complianceAcceptability.getComplianceFrequency());
contentValues.put(KEY_COMPLIANCE_APPLICABLE, complianceAcceptability.getComplianceApplicable());
contentValues.put(KEY_COMPLIANCE_OPTED, complianceAcceptability.getComplianceOpted());
sqLiteDatabase.insert(TABLE_COMPLIANCE_ACCEPTABILITY, null, contentValues);
sqLiteDatabase.close();
}
Imm trying to access the values using the following code,
public List<ComplianceAcceptability> getAllAcceptability() {
List<ComplianceAcceptability> complianceAcceptabilityList = new ArrayList<>();
String selectQuery = "SELECT * FROM " + TABLE_COMPLIANCE_ACCEPTABILITY;
SQLiteDatabase db = this.getWritableDatabase();
Cursor cursor = db.rawQuery(selectQuery, null);
if (cursor.moveToFirst()) {
do {
ComplianceAcceptability complianceAcceptability = new ComplianceAcceptability();
complianceAcceptability.setId(Integer.parseInt(cursor.getString(0)));
complianceAcceptability.setCountryId(cursor.getString(1));
complianceAcceptability.setDomainId(cursor.getString(2));
complianceAcceptability.setUnitId(cursor.getString(3));
complianceAcceptability.setComplianceId(cursor.getString(4));
complianceAcceptability.setCompliancenName(cursor.getString(5));
complianceAcceptability.setComplianceFrequency(cursor.getString(6));
complianceAcceptability.setComplianceApplicable(cursor.getString(7));
complianceAcceptability.setComplianceOpted(cursor.getString(8));
} while (cursor.moveToNext());
}
return complianceAcceptabilityList;
}
The problem is that it returns an empty array. But when I try to print the contentValues within addComplianceStatus, it of course prints the array of all values.
You never add anything to complianceAcceptabilityList. You just assign it once, then do nothing to it before returning it. Maybe you want to add complianceAcceptability into it at each iteration through the cursor?
Here you are not adding the complianceAcceptability object to the complianceAcceptabilityList, your modified code:
if (cursor.moveToFirst()) {
do {
ComplianceAcceptability complianceAcceptability = new ComplianceAcceptability();
complianceAcceptability.setId(Integer.parseInt(cursor.getString(0)));
complianceAcceptability.setCountryId(cursor.getString(1));
complianceAcceptability.setDomainId(cursor.getString(2));
complianceAcceptability.setUnitId(cursor.getString(3));
complianceAcceptability.setComplianceId(cursor.getString(4));
complianceAcceptability.setCompliancenName(cursor.getString(5));
complianceAcceptability.setComplianceFrequency(cursor.getString(6));
complianceAcceptability.setComplianceApplicable(cursor.getString(7));
complianceAcceptability.setComplianceOpted(cursor.getString(8));
complianceAcceptabilityList.add(complianceAcceptability);
} while (cursor.moveToNext());
}
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'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
I'm having some trouble saving a string with an accent to my database and retrieving it.
This is my function that saves a new location to the database. It gets 'myId' and 'location' and inserts them. The println there shows the item as I expect, with the accent. The example I'm using is Mazzarrà.
public long createLocationRecord(String location, int myId) {
SQLiteDatabase db = this.getWritableDatabase();
ContentValues values = new ContentValues();
values.put(KEY_OWMID, myId);
values.put(KEY_NAME, location);
System.out.println("newItem in DB = "+location);
long callSQL = db.insertWithOnConflict(TABLE_LOCATION, null, values, SQLiteDatabase.CONFLICT_IGNORE);
if(callSQL==-1)
db.update(TABLE_LOCATION, values, KEY_OWMID + '=' + owmId, null);
return callSQL;
}
This is my function to retrieve all location items. The println here prints out Mezzarra, without the accented à. Am I missing something? Do I need to make a change to my database? It's just a regular Android SQLite DB that I'm opening via SQLiteBrowser.
public ArrayList<String> getLocationList() {
ArrayList<String> list = new ArrayList<String>();
SQLiteDatabase db = this.getWritableDatabase();
String selectQuery = "SELECT * "
+ "FROM " + TABLE_LOCATION
+ " ORDER BY _id ASC";
Cursor c = db.rawQuery(selectQuery, null);
if (c != null)
c.moveToFirst();
if (c.moveToFirst()) {
do {
System.out.println("newItem GETTING FROM DB = "+c.getString(c.getColumnIndex(KEY_NAME)));
list.add(c.getString(c.getColumnIndex(KEY_NAME)));
} while (c.moveToNext());
}
c.close();
return list;
}
Thanks for any help anyone can provide.
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