Click here to Skip to main content
15,902,189 members
Please Sign up or sign in to vote.
0.00/5 (No votes)
See more:
I am developing database in Sqlite. I created the table the schema are Sms_Address,Sms_Body,Status_Msg. The initial status of message is 0. I am updating the status to 1 by using setStatus() function in the database. But the status of all rows are updated. I want to update only one field named as Status_Msg checking by checkbox.

The setStatus() function is given below:

Java
public String setStatus(){
        SQLiteDatabase sqldb = this.getWritableDatabase();
        ContentValues values = new ContentValues();
        if (Status_Msg.equals(Status_Msg)) {
            values.put(Status_Msg, 1);
            sqldb.update(DB_Table1, values, "status = 0", null);
        }
        System.out.println("" + Status_Msg);
        sqldb.close();
        return Status_Msg;
    }


I am displaying data in the database by using showStatusChangedData() function. The control moves properly in this function. This function does not show messages in listview Please check these two functions.

The showStatusChangedData() function is given below:

Java
public List<Message> showStatusChangedData(){
       SQLiteDatabase sqldb = getWritableDatabase();
       List<Message> list = new ArrayList<>();
       String select_query = "SELECT * FROM " + DB_Table1 + " where status = 1";

       Cursor cursor = sqldb.rawQuery(select_query,null);
           if (cursor.moveToFirst()) {
               do {
                   Message msg = new Message();
                   msg.setmAddress(cursor.getString(0));
                   msg.setmBody(cursor.getString(1));
                   msg.setmStatus(cursor.getString(2));
                   list.add(msg);
               } while (cursor.moveToNext());
           }
       return list;
   }


What I have tried:

I tried the following query
Java
sqldb.update(DB_Table1, values,Sms_Address +"="+ Status_Msg,null);

But this not works.
Posted
Updated 13-Apr-18 2:31am
v6
Comments
Richard MacCutchan 24-Feb-16 7:43am    
What does "it not works" mean? And what result do you expect from the statement if (Status_Msg.equals(Status_Msg))?
Member 12299159 24-Feb-16 8:31am    
The status of message is equal to 1 then update the status of message.
Member 12299159 25-Feb-16 2:27am    
I solved the yesterdays problem. This is a new problem I face. Please help me for solving this problem. I updated the question.
Richard MacCutchan 25-Feb-16 3:57am    
Add some code to check the values that get returned when you call some method. Do not assume that you will get a good result each time. The cursor returned from your SELECT statement may not contain any items. You can quickly check by using your debugger.
Member 12299159 25-Feb-16 4:05am    
I checks by using debugger the update query updates value 0 to 1 and
Select query not contains any item.

To update the field in specific row, try this;

ContentValues cv = new ContentValues();
cv.put(Status_Msg, "1");
sqldb.update(DB_Table1, cv, "id="+_id, null);
sqldb.close();

It's good practice to have a primary key for each row data.
Here _id would be your primary key.

One more suggestion, when you done with iterating all rows in the table, close the cursor object (cursor.close()).
 
Share this answer
 
v2
Comments
Member 12299159 29-Feb-16 2:25am    
I want to update Status_Msg to value 1.
Your code update Sms_Address to status.
Please give me another solution.
See the updated solution above.
 
Share this answer
 
Comments
Member 12299159 29-Feb-16 3:16am    
By using your code whole column of status updated.
I want to update specific field selected by check box.
Please give me solution.
GVS13 29-Feb-16 3:59am    
Do you have primary key for each row as I mentioned? If you use primary key for the respective row to update, it will work only for that row field.
Member 12299159 29-Feb-16 4:10am    
Yes I have primary key as Sms_Id
GVS13 29-Feb-16 4:18am    
Can you post the updated SetStatus function and the code snippet you are using to update the value.
Member 12299159 29-Feb-16 5:00am    
yes
sqldb.update(DB_Table1, values,Sms_Address +"=?",new String[]{Status_Msg;});
 
Share this answer
 

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900